Creating a Discord bot to assign roles based on message reactions

I’m working on a Discord bot that gives users roles when they react to a specific message with certain emojis. However, the bot isn’t assigning roles when I react to the message. Here’s what I’ve tried so far:

import discord

class RoleBot(discord.Client):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.target_message_id = 9876543210987654321
        self.emoji_role_map = {
            discord.PartialEmoji(name='👍'): 1234567890123456789,
            discord.PartialEmoji(name='🔵'): 2345678901234567890,
            discord.PartialEmoji(name='star', id=1): 3456789012345678901,
        }

    async def on_raw_reaction_add(self, payload):
        if payload.message_id != self.target_message_id:
            return
        
        server = self.get_guild(payload.guild_id)
        if not server:
            return

        try:
            role_id = self.emoji_role_map[payload.emoji]
            role = server.get_role(role_id)
            if role:
                await payload.member.add_roles(role)
        except KeyError:
            pass

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

bot = RoleBot(intents=intents)
bot.run('bot_token_here')

When I react with :+1:, I only see these console outputs:
target_message
server_found

Any ideas why the roles aren’t being assigned?

I’ve encountered similar issues before. One crucial aspect you might want to check is the hierarchy of roles in your server. The bot’s role needs to be higher than the roles it’s trying to assign. This is a common oversight.

Also, have you verified that the role IDs in your emoji_role_map are correct? Sometimes, these can change if roles are deleted and recreated. You can double-check by right-clicking on the role in Discord (with Developer Mode enabled) and selecting ‘Copy ID’.

Lastly, consider adding more detailed logging. Instead of just printing ‘server_found’, log the specific role being attempted and any errors encountered. This can provide more insight into where exactly the process is failing.

If these don’t help, you might want to look into using discord.py’s error handling to catch and log any exceptions that might be occurring silently.

As someone who’s worked with Discord bots before, I can share a few insights that might help you out. First off, make sure your bot’s role in the server hierarchy is above the roles it’s trying to assign. This is a common gotcha that’s easy to overlook.

Another thing to check is the bot’s permissions. Even if you’ve set up the intents correctly in your code, the bot needs the ‘Manage Roles’ permission in the server settings. Without this, it won’t be able to assign roles no matter what your code does.

Also, I’ve found that logging can be a lifesaver when debugging these issues. Instead of just printing ‘server_found’, try logging more detailed information. For example, log the specific role being attempted, the user it’s being assigned to, and any errors that occur. This can give you a much clearer picture of where things are going wrong.

Lastly, double-check your emoji handling. For custom emojis, you need both the name and ID, while for default emojis like :+1:, just the name is enough. Make sure your emoji_role_map is set up correctly for each type of emoji you’re using.

Hope this helps you get your bot working!

hey there! have u checked if ur bot has the right permissions in the server? sometimes discord can be picky about that. also, make sure the role IDs in ur code match the actual roles on the server. if those look good, try adding some print statements in ur on_raw_reaction_add function to see whats happening. Good luck!