Python Discord.py如何向成员添加角色

Python Discord.py如何向成员添加角色,python,discord,discord.py,Python,Discord,Discord.py,我有这个代码,当有人写下“send@user to court”,然后得到提到的个人id,写下“sending to court”,我会检查,但我也尝试让它添加角色“court”,但它不起作用。代码如下: import discord from discord.ext import commands import ctx class MyClient(discord.Client): async def on_ready(self): print('Logged on

我有这个代码,当有人写下“send@user to court”,然后得到提到的个人id,写下“sending to court”,我会检查,但我也尝试让它添加角色“court”,但它不起作用。代码如下:

import discord
from discord.ext import commands
import ctx

class MyClient(discord.Client):

    async def on_ready(self):
       print('Logged on as', self.user)

    async def on_message(self, message):
        if message.author == self.user:
            return

        messageContent = message.content
        if len(messageContent) > 0:
            if re.search("^send.*court$", messageContent):
                user_id = message.mentions[0].id
                await message.channel.send('sending to court!')
                role = discord.utils.get(server.roles, name="court")
                await client.add_roles(user_id, role)

client = MyClient()
client.run('TOKEN')
  • 您也可以直接在
    len()
    re.search()
    中使用
    message.content
    (如果以后不再需要)

  • 对于
    re.search()
    您必须导入
    re

  • 您没有变量
    服务器
    ,因此无法在
    服务器中使用它。角色
    ,因为python无法从零开始获取服务器

  • (主要问题)它不是
    客户端。添加角色
    ,而是您必须使用对象的地方

  • 修订:

    导入不一致
    从discord.ext导入命令
    进口ctx
    进口稀土
    类MyClient(discord.Client):
    异步def on_就绪(自):
    打印('登录身份',self.user)
    _消息上的异步定义(self,message):
    如果message.author==self.user:
    返回
    如果len(message.content)>0:
    如果重新搜索(“^send.*court$”,message.content):
    用户=消息。提及[0]
    等待message.channel.send('sending to court!')
    role=discord.utils.get(message.guild.roles,name=“court”)
    等待用户。添加_角色(角色)
    client=MyClient()
    client.run('TOKEN')
    
    wrights
    ?权利?写?仍然不起作用,它说“AttributeError:'NoneType'对象没有属性'add_roles'”,所以要么你没有提到一个人,要么你做得不对,不能用另一种方式解释。。或者只是更新discord.py?找不到版本,因为这将在中,但可能是问题所在。IV更改了一些内容并修复了它。谢谢。不客气,只是不要忘记将其标记为正确,或者如果更改太多,请写下自己的答案并将其标记为正确