【问题标题】:Discord.py how to add a role to a memberDiscord.py 如何为成员添加角色
【发布时间】:2021-07-26 11:28:55
【问题描述】:

我有这个代码女巫检查当有人写“发送@user到法庭”,然后得到提到的人ID和写“发送到法庭”,但我也试图让它添加角色“法庭”,但它在这里不起作用代码:

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')

【问题讨论】:

  • wrights? 权利?

标签: python discord discord.py


【解决方案1】:
  1. 你也可以直接在len()re.search()中使用message.content(如果以后不再需要的话)

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

  3. 你没有变量server,所以你不能在server.roles中使用它,因为python无法从无到有获取服务器

  4. (主要问题)不是client.add_roles,而是member.add_roles,您必须使用Member 对象

修订:

import discord
from discord.ext import commands
import ctx
import re

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

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

client = MyClient()
client.run('TOKEN')

【讨论】:

  • 仍然不起作用它说“AttributeError:'NoneType'对象没有属性'add_roles'”
  • 所以要么你没有提到一个人,要么你做错了,不能用另一种方式解释。或者也许只是更新 discord.py?找不到版本,因为这会在,但也许这就是问题??‍♂️ 只是为了解释;此属性错误意味着,它没有在消息中找到任何用户/提及
  • 我改变了一些东西并修复了它,谢谢
  • 不客气,只是不要忘记将其标记为正确,或者如果您更改太多,请写下您自己的答案并将其标记为正确
猜你喜欢
  • 2021-03-26
  • 2021-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-19
  • 1970-01-01
  • 1970-01-01
  • 2019-08-11
相关资源
最近更新 更多