0
votes

So far I have a message that a new user can react to in a certain channel in discord that will assign them a role based on the reaction they choose (this part is working). I also want the role to be removed from the user if they remove their reaction to that message (this is what's not working). I get an error message saying: line 23, in on_raw_reaction_remove role = discord.utils.get(payload.member.guild.roles, name='War Thunder') AttributeError: 'NoneType' object has no attribute 'guild'

@client.event
# this works to assign a role
async def on_raw_reaction_add(payload):
    # channel and message IDs should be integer:
    if payload.channel_id == 700895165665247325 and payload.message_id == 756577133165543555:
        if str(payload.emoji) == "<:WarThunder:745425772944162907>":
            role = discord.utils.get(payload.member.guild.roles, name='War Thunder')
            await payload.member.add_roles(role)

# this doesn't work in removing the role
async def on_raw_reaction_remove(self, payload):
    if payload.channel_id == 700895165665247325 and payload.message_id == 756577133165543555:
        if str(payload.emoji) != "<:WarThunder:745425772944162907>":
            role = discord.utils.get(payload.member.guild.roles, name='War Thunder')
            await payload.member.remove_roles(role)
1

1 Answers

1
votes

As the documentation says, payload.member is only available if the event_type is REACTION_ADD. So, to get the guild, you must use payload.guild_id and either:

  • Use client.fetch_guild():
    async def on_raw_reaction_remove(payload):
        if payload.channel_id == 700895165665247325 and payload.message_id == 756577133165543555:
            if str(payload.emoji) == "<:WarThunder:745425772944162907>":
                guild = await client.fetch_guild(payload.guild_id)
                role = discord.utils.get(guild.roles, name='War Thunder')
                await payload.member.remove_roles(role)
    
  • Use discord.utils.get():
    async def on_raw_reaction_remove(payload):
        if payload.channel_id == 700895165665247325 and payload.message_id == 756577133165543555:
            if str(payload.emoji) == "<:WarThunder:745425772944162907>":
                guild = discord.utils.get(client.guilds, id=payload.guild_id)
                role = discord.utils.get(guild.roles, name='War Thunder')
                await payload.member.remove_roles(role)
    

PS: Instead of writing discord.utils.get() everytime, you can write from discord.utils import get in your imports and write get(iterable, **attrs).