【问题标题】:AttributeError when making a discord bot制作不和谐机器人时出现AttributeError
【发布时间】:2021-10-02 18:26:36
【问题描述】:

我正在使用 discord.py 库为我的 Discord 服务器创建一个机器人。目前我正在尝试处理当用户从消息中删除他们的反应时会发生什么。我已将 Discord 更新到最新版本,允许使用 Developer Portal 和通过如下代码的意图:

intents = discord.Intents.all()
bot = commands.Bot(command_prefix=('$', '?'), intents=intents)

这里是 on_raw_reaction_remove 函数的代码:

    channel = 867854256647176252
    message = 867855236861132860
    if payload.channel_id == channel and payload.message_id == message:
        guild = bot.get_guild(payload.guild_id)
        member = guild.get_member(payload.user_id)
        emoji = payload.emoji
        global channels
        channels = str(channels)
        channels = channels.split()
        if str(emoji.name) == '????' :
            role = discord.utils.get(guild.roles, name='Offtopic')
            for index in range(len(channels) - 1, -1, -1):
                if channels[index] == 'Offtopic':
                    channels.pop(index)  
            await payload.member.remove_roles(role)

一旦反应被移除,它产生的错误:

Ignoring exception in on_raw_reaction_remove
Traceback (most recent call last):
  File "/home/meri/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "bot.py", line 420, in on_raw_reaction_remove
    await payload.member.remove_roles(role)
AttributeError: 'NoneType' object has no attribute 'remove_roles'

没有任何类似问题的答案能够帮助解决这种情况,感谢任何帮助。

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    NoneType 表示您正在访问的值 (payload.member) 设置为 None,因此没有 remove_roles() 函数。您将上面的成员定义为它自己的独立变量,所以您打算改为 member.remove_roles() 吗?

    【讨论】:

      猜你喜欢
      • 2021-11-08
      • 2020-12-20
      • 2020-08-29
      • 2021-01-17
      • 2018-03-20
      • 2020-12-18
      • 2021-09-20
      • 2020-01-29
      • 2021-05-03
      相关资源
      最近更新 更多