首页 > 解决方案 > AttributeError:'NoneType' 对象没有属性 'id' - on_raw_reaction_add 中的错误

问题描述

所以我最近在我的机器人中遇到了这个我以前没有遇到过的问题......

错误 -

Traceback (most recent call last):
  File "/home/container/.local/lib/python3.9/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "/home/container/cogs/mod.py", line 100, in on_raw_reaction_add
    await payload.member.add_roles(role, reason="Reaction Roles", atomic=True)
  File "/home/container/.local/lib/python3.9/site-packages/discord/member.py", line 764, in add_roles
    await req(guild_id, user_id, role.id, reason=reason)
AttributeError: 'NoneType' object has no attribute 'id'

我的代码是:

    @commands.Cog.listener()
    async def on_raw_reaction_add(self, payload):

        if payload.member.bot:
            pass

        else:
            with open('reactrole.json') as react_file:
                data = json.load(react_file)
                for x in data:
                    if x['emoji'] == payload.emoji.name:
                        role = discord.utils.get(self.client.get_guild(
                            payload.guild_id).roles, id=x['role_id'])

                        await payload.member.add_roles(role)


    @commands.Cog.listener()
    async def on_raw_reaction_remove(self, payload):

        with open('reactrole.json') as react_file:
            data = json.load(react_file)
            for x in data:
                if x['emoji'] == payload.emoji.name:
                    role = discord.utils.get(self.client.get_guild(
                        payload.guild_id).roles, id=x['role_id'])

                    
                    await self.client.get_guild(payload.guild_id).get_member(payload.user_id).remove_roles(role)

我刚刚检查了文档并添加了这些额外的参数,例如原因和原子。

如果您知道解决此问题的方法,请回答。提前致谢。

标签: python-3.xdiscord.pyattributeerror

解决方案


discord.utils.get不知何故找不到您获得的角色,这导致discord.utils.get返回 None 对象。因此,问题在于reactrole.json文件中的数据。

如果没有其他信息(如 reactrole.json 文件和服务器上的角色),这一切都可以得到帮助。


推荐阅读