0
votes

Keep getting an error for the reaction_remove, just copy-pasted my whole code(minus the client id) cause it might help, I have no idea what's wrong as the first half works perfectly fine. Error in console

Also, trying to add "if member is not None: await member.remove_roles(role) else: Print(error)" at the very end and itgave me errors every time I tried removing roles, just in case it helps.

import discord

client = discord.Client()

@client.event 
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event 
async def on_raw_reaction_add(payload):
    message_id = payload.message_id
    
    if message_id == 806406876593127464:
        guild_id = payload.guild_id
        guild = discord.utils.find(lambda g : g.id == guild_id, client.guilds)
        if payload.emoji.name == "049":
            role = discord.utils.get(guild.roles, name = 'SCP-049')
        elif payload.emoji.name == "0492":
            role = discord.utils.get(guild.roles, name = 'SCP-049-2')
        elif payload.emoji.name == "096":
            role = discord.utils.get(guild.roles, name = 'SCP-096')
        elif payload.emoji.name == "106":
            role = discord.utils.get(guild.roles, name = 'SCP-106')
        elif payload.emoji.name == "173":
            role = discord.utils.get(guild.roles, name = 'SCP-173')
        elif payload.emoji.name == "682":
            role = discord.utils.get(guild.roles, name = 'SCP-682')
        elif payload.emoji.name == "939":
            role = discord.utils.get(guild.roles, name = 'SCP-939')
        elif payload.emoji.name == "999":
            role = discord.utils.get(guild.roles, name = 'SCP-999')
        elif payload.emoji.name == "Chaos":
            role = discord.utils.get(guild.roles, name = 'Chaos Insurgency')
        elif payload.emoji.name == "DClass":
            role = discord.utils.get(guild.roles, name = 'D-Class')
        elif payload.emoji.name == "MTF":
            role = discord.utils.get(guild.roles, name = 'MTF')
        elif payload.emoji.name == "Scientist":
            role = discord.utils.get(guild.roles, name = 'Scientist')

        member = payload.member
        await member.add_roles(role)

@client.event 
async def on_raw_reaction_remove(payload):
    message_id = payload.message_id
    
    if message_id == 806406876593127464:
        guild_id = payload.guild_id
        guild = discord.utils.find(lambda g : g.id == guild_id, client.guilds)
        if payload.emoji.name == "049":
            role = discord.utils.get(guild.roles, name = 'SCP-049')
        elif payload.emoji.name == "0492":
            role = discord.utils.get(guild.roles, name = 'SCP-049-2')
        elif payload.emoji.name == "096":
            role = discord.utils.get(guild.roles, name = 'SCP-096')
        elif payload.emoji.name == "106":
            role = discord.utils.get(guild.roles, name = 'SCP-106')
        elif payload.emoji.name == "173":
            role = discord.utils.get(guild.roles, name = 'SCP-173')
        elif payload.emoji.name == "682":
            role = discord.utils.get(guild.roles, name = 'SCP-682')
        elif payload.emoji.name == "939":
            role = discord.utils.get(guild.roles, name = 'SCP-939')
        elif payload.emoji.name == "999":
            role = discord.utils.get(guild.roles, name = 'SCP-999')
        elif payload.emoji.name == "Chaos":
            role = discord.utils.get(guild.roles, name = 'Chaos Insurgency')
        elif payload.emoji.name == "DClass":
            role = discord.utils.get(guild.roles, name = 'D-Class')
        elif payload.emoji.name == "MTF":
            role = discord.utils.get(guild.roles, name = 'MTF')
        elif payload.emoji.name == "Scientist":
            role = discord.utils.get(guild.roles, name = 'Scientist')

        member = payload.member
        await member.remove_roles(role)
client.run()

It was able to give me a role after adding a reaction, but has not been able to remove the role after removing that reaction here

4
Is it getting a role object? Try print(role) above your error lineKelo
Just added a picture of the console with both print(role) and print(member) just before the member.add/remove_rolesPwuurple

4 Answers

1
votes

If you try printing the payload.member the output will be None - it's because you didn't enable intents.members. To enable them:

intents = discord.Intents.default()
intents.members = True 

client = discord.Client(intents=intents)

Also make sure to enable intents in the developer portal

How to enable privileged intents

1
votes

The below answer is one part of the solution to make this code work so implement the provided answer. I found out in another issue that in the documentation payload.member is only available if the event_type is REACTION_ADD so i fixed it by adding:

intents = discord.Intents.default()
intents.members = True 

client = discord.Client(intents=intents)     

After you have added this, change the

member = payload.member 

to

member = guild.get_member(payload.user_id)

That is what worked for me let me know if this helps.

0
votes

payload.member will always be None when the event is on_raw_reaction_remove. As such, you will have to use guild.get_member() and the payload's user_id to obtain the member from cache, which also requires having the members intent enabled.

0
votes

I dont know if this is still an active problem, but I had the same problem and solved it right now after some time and wanted to share with you how I have done this.

I hope i could help other peoble with this.

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

↑ Add this at the top of your code right under imports



@bot.event
async def on_raw_reaction_add(payload):
    guild = bot.get_guild(payload.guild_id)
    member = payload.member
    emoji = payload.emoji.name
    channel = discord.utils.get(guild.channels, name='regeln')
    channel_id = channel.id
    message_id = 874741218523873370

    if message_id == payload.message_id:
        if payload.channel_id == channel_id:
            if emoji == '1️⃣':
                role = discord.utils.get(guild.roles, name='News1')
            if emoji == '2️⃣':
                role = discord.utils.get(guild.roles, name='News2')
            if emoji == '3️⃣':
                role = discord.utils.get(guild.roles, name='News3')
            if emoji == '4️⃣':
                role = discord.utils.get(guild.roles, name='News4')
        await member.add_roles(role)



@bot.event
async def on_raw_reaction_remove(payload):
    guild = bot.get_guild(payload.guild_id)
    member = guild.get_member(payload.user_id)
    emoji = payload.emoji.name
    channel = discord.utils.get(guild.channels, name='regeln')
    channel_id = channel.id
    message_id = 874741218523873370

    if message_id == payload.message_id:
        if payload.channel_id == channel_id:
            if emoji == '1️⃣':
                role = discord.utils.get(guild.roles, name='News1')
            if emoji == '2️⃣':
                role = discord.utils.get(guild.roles, name='News2')
            if emoji == '3️⃣':
                role = discord.utils.get(guild.roles, name='News3')
            if emoji == '4️⃣':
                role = discord.utils.get(guild.roles, name='News4')
        await member.remove_roles(role)