首页 > 解决方案 > payload.member returns none on on_raw_reaction_remove() but works with on_raw_reaction_add()

问题描述

so im making a discord bot and I have an error:

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

on_raw_reaction_remove() code:

@client.event
async def on_raw_reaction_remove(payload):
  if payload.message_id == 865402961585111111:
    if str(payload.emoji) == "✅":
      guild = client.get_guild(payload.guild_id)
      role = discord.utils.get(guild.roles, name='Announcements')
      await payload.member.remove_roles(role)

on_raw_reaction_add() code:

@client.event
async def on_raw_reaction_add(payload):
  if payload.message_id == 865402961585111111:
    if str(payload.emoji) == "✅":
      guild = client.get_guild(payload.guild_id)
      role = discord.utils.get(guild.roles, name='Announcements')
      await payload.member.add_roles(role)

The strange thing is, with on_raw_reaction_add() it returns a valid user.

标签: discord.py

解决方案


I changed your event a bit and added some checks to make sure everything is correct.

There were always problems with finding the role, so you need to do things a little differently.

Take a look at the following code:

@client.event
async def on_raw_reaction_remove(payload):
    guild = client.get_guild(payload.guild_id)
    print("Guild checked.")
    member = discord.utils.get(guild.members, id=payload.user_id)
    print("Member checked.")
    if payload.message_id == 865402961585111111:
        print("Got message.")
        if str(payload.emoji) == "✅":
            print("Checked for the reaction.")
            role = discord.utils.get(guild.roles, name='Announcements')
            print("Got the role.")
        else:
            role = discord.utils.get(guild.roles, name=payload.emoji)

        if role is not None:
            await member.remove_roles(role)
            print("Removed the role")

If the code is not self-explanatory, I'll be happy to add some explanations.

EDIT: After looking at the full code the problem seems to be with the Intents. You named them correctly but did not import them.

To import them you use:

client = commands.Bot(command_prefix="YourPrefix", intents=intents1)

推荐阅读