0
votes
@client.event
async def on_raw_reaction_add(payload):
    message_id = payload.message_id
    if message_id == 728213540858757152:
        guild_id = payload.guild_id
        guild = discord.utils.find(lambda g : g.id == guild_id, client.guilds)
        if payload.emoji.name == "loud_sound":
            role = discord.utils.get(message_id.guild.roles, name="DUYURU")
        elif payload.emoji.name == "bee":
            role = discord.utils.get(guild.roles, name="REKLAM")
        else:
            role = discord.utils.get(guild.roles, name=payload.emoji.name)

        if role is not None:
            member = discord.utils.find(lambda m : m.id == payload.user_id, guild.members)
            if member is not None:
                await member.add_roles(role)
                print(f"{member} {role} aldı!")
            else:
                print("Member not found")
        else:
            print("Role not found")

@client.event
async def on_raw_reaction_remove(payload):
    message_id = payload.message_id
    if message_id == 728213540858757152:
        guild_id = payload.guild_id
        guild = discord.utils.find(lambda g : g.id == guild_id, client.guilds)
        if payload.emoji.name == "loud_sound":
            role = discord.utils.get(guild.roles, name="DUYURU")
        elif payload.emoji.name == "bee":
            role = discord.utils.get(guild.roles, name="REKLAM")
        else:
            role = discord.utils.get(guild.roles, name=payload.emoji.name)

        if role is not None:
            member = discord.utils.find(lambda m : m.id == payload.user_id, guild.members)
            if member is not None:
                await member.remove_roles(role)
                print(f"{member} {role} aldı!")
            else:
                print("Member not found")
        else:
            print("Role not found")

I am trying to put reaction roles in my discord.py bot, but it doesn't work. It gives me the error: Role not found. But I already have the role in my roles list. I tried to use an another role-related code I made that works perfectly, but that didn't work either. I have tried to change message_id to channel but that didn't work either

1
It's obvious that one of your discord.utils.get failed and you get a "Role not found"lincr

1 Answers

0
votes

A couple of changes, according to the docs on_reaction_add has 2 parameters, reaction, user, which would change your event listener to async def on_reaction_add(reaction, user)

Secondly getting the id of an object is done by doing object.id, so you could be doing guild = reaction.message.guild.id.

Thirdly, you can use bot.get_guild(id) to fetch the guild, but you do not need to do that since you can get it from reaction.message.guild.