Discord Py无法弄清楚如何添加角色

时间:2018-05-05 17:55:14

标签: python roles discord

我到处寻找,人们只是告诉我使用文档并且没有帮助,我已经定义了on_message(消息)所以我不能使用会员参数和我希望人们在说出关键字时设置为某个角色 这是我得到的,但它不起作用:

if message.content.lower() == "!changeadmin"
role = discord.utils.get(server.roles, name="Administrator")
await client.add_roles(member, role)

我总是带着

回来
    Ignoring exception in on_message
    Traceback (most recent call last):
      File "C:\Users\josep\AppData\Local\Programs\Python\Python36-32\lib\site- 
   packages\discord\client.py", line 307, in _run_event
        yield from getattr(self, event)(*args, **kwargs)
      File "C:\Users\josep\OneDrive\Documents\New folder\New folder\mybot1.py", 
    line 58, in on_message
        role = discord.utils.get(server.roles, name="admin")
    AttributeError: 'str' object has no attribute 'roles'

编辑: 这是我的完整代码(不包括底部的代码):

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import random
import ctx

Client = discord.Client()
client = commands.Bot(command_prefix = "!")

chat_filter = ["CUNT", "NIGGER", "NIGGA", "FUCK", "BITCH", "DICK", "WANKER"]
bypass_list = ["227007454775476224"]
possible_responses =  [
            'That is a resounding no',
            'It is not looking likely',
            'Too hard to tell',
            'It is quit possible',
            'Definitely',
        ]
server = '242547220765868033'
roles = ['442351132615114772']

@client.event
async def on_ready():
    print("Bot is online and connected to Discord")

bot = commands.Bot(command_prefix='!', description='A bot that greets the user back.')

@client.event
async def on_message(message):
    contents = message.content.split(" ") #contents is a list type
    for word in contents:
        if word.upper() in chat_filter:
            if not message.author.id in bypass_list:
                try:
                    await client.delete_message(message)
                    await client.send_message(channel.message, "**Hey!** You're not allowed to use that word here!")
                except discord.errors.NotFound:
                    return    
    if message.content.upper().startswith('!PING'):
        userID = message.author.id
        await client.send_message((discord.Object(id='442333293539622913')), "<@%s> Pong!" % (userID))
    if message.content.upper().startswith('!SAY'):
        if message.author.id == "227007454775476224" or message.author.id == "399959323591180288":
            args = message.content.split(" ")
            await client.send_message(message.channel, "%s" % (" ".join(args[1:])))
        else:
            await client.send_message(message.channel, "Sorry only the bot owner has permission to use this command")
    if message.content.lower() == "cookie":
        await client.send_message(message.channel, ":cookie:") #responds with Cookie emoji when someone says "cookie"
    if message.content.lower() == "!website":
        await client.send_message(message.channel, "habbo.com")

    if message.content.upper().startswith('!EIGHTBALL'):
        await client.send_message(message.channel, random.choice(possible_responses))
    if message.content.lower() == '!changeadmin':
        role = discord.utils.get(server.roles, name="admin")
        await client.add_roles(member, role)

1 个答案:

答案 0 :(得分:0)

对不起,如果这是一个迟到的回复:) 因此,如果您想向成员添加角色,请尝试使用以下代码:

    @client.command()
     @commands.has_permissions(administrator=True)
    async def giverole(ctx,member :discord.Member,role):
        try:
            add_role= discord.utils.get(ctx.guild.roles, name=role)
            await member.add_roles(add_role)      
            await ctx.send('Updated')
        except:
            await ctx.send("role not found!")

所以代码会像这样工作:让我们说 prefix="?"

?giverole @member 示例

机器人将从公会角色中搜索角色名称“example”并将其提供给@member