Search code examples
pythondiscord

Reaction Role with Discord.py


I want to make my bot creating an embed and add some emoji that when you press, them give you a role. I also want to do that when the bot restart, I don't need to restart the embed too, you know.

If you don't really understand, please ask in the comment


Solution

  • This is how you can add reaction to a message:

    msg = await ctx.send(embed=embed)
        await msg.add_reaction("🧠")
    

    And this is how you handle reaction roles:

    @bot.event
    async def on_raw_reaction_add(payload):
        if payload.message_id == THE_ID_OF_YOUR_MESSAGE:
            guild = bot.get_guild(payload.guild_id)
            member = guild.get_member(payload.user_id)
            if str(payload.emoji) == "🧠":
                role = discord.utils.get(guild.roles, name=ROLE_NAME)
                await member.add_roles(role)
    
    @bot.event
    async def on_raw_reaction_remove(payload):
        if payload.message_id == THE_ID_OF_YOUR_MESSAGE:
            guild = bot.get_guild(payload.guild_id)
            member = guild.get_member(payload.user_id)
            if str(payload.emoji) == "🧠":
                role = discord.utils.get(guild.roles, name=ROLE_NAME)
                await member.remove_roles(role)
    

    This one works even after the bot was restarted.

    Paste the role's name that you want to give on reaction instead of ROLE_NAME

    Change THE_ID_OF_YOUR_MESSAGE to the id of the message that you reacted to with the bot before.