Discord bot reaction role setup: Permissions error troubleshooting

Hey everyone, I’m stuck with a permissions issue while setting up reaction roles for my Discord bot. Here’s the error I’m getting:

DiscordAPIError: Missing Permissions
    at RequestHandler.execute (C:\Users\botdev\projects\discord-bot\node_modules\discord.js\src\rest\RequestHandler.js:350:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async RequestHandler.push (C:\Users\botdev\projects\discord-bot\node_modules\discord.js\src\rest\RequestHandler.js:51:14)
    at async GuildMemberRoleManager.add (C:\Users\botdev\projects\discord-bot\node_modules\discord.js\src\managers\GuildMemberRoleManager.js:124:7) {
  method: 'put',
  path: '/guilds/1234567890123456789/members/9876543210987654321/roles/1111222233334444555',  
  code: 50013,
  httpStatus: 403,
  requestData: { json: undefined, files: [] }
}

Here’s my code:

const Discord = require('discord.js');
const bot = new Discord.Client({ intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_MESSAGE_REACTIONS'] });

bot.on('ready', () => {
  console.log(`Bot is online as ${bot.user.tag}`);
});

bot.on('messageCreate', async (msg) => {
  if (msg.content === '!add_role') {
    const roleMsg = await msg.channel.send('Click the checkmark to get the role!');
    roleMsg.react('✅');
  }
});

bot.on('messageReactionAdd', async (reaction, user) => {
  if (reaction.emoji.name === '✅') {
    const server = reaction.message.guild;
    const member = server.members.cache.get(user.id);
    let role = server.roles.cache.find(r => r.name === 'NewRole');
    member.roles.add(role.id);
  }
});

bot.login('your_token_here');

I’ve tried everything I can think of. The bot has admin permissions and its role is at the top of the list. Any ideas what I’m doing wrong?

I’ve dealt with similar permission hiccups before. One often overlooked aspect is the OAuth2 scope when adding the bot to the server. Make sure you’ve included the ‘bot’ and ‘applications.commands’ scopes in your OAuth2 URL.

Also, consider the possibility of rate limiting. If you’re assigning roles frequently, you might hit Discord’s rate limits. Implement a queue system for role assignments to space them out.

Another thing to check: Is the ‘NewRole’ being created dynamically? If so, ensure it’s fully created and cached before trying to assign it. You might need to add a slight delay or use await on the role creation process.

Lastly, verify your intents. You might need ‘GUILD_MEMBERS’ intent for certain operations. Add it to your intents array and enable it in the Discord Developer Portal if necessary.

I’ve encountered this issue before, and it can be frustrating. The error suggests the bot lacks permissions, but there might be a few other factors at play.

First, double-check that your bot’s role is indeed at the top of the hierarchy and has the ‘Manage Roles’ permission. Sometimes, Discord’s caching can cause issues, so try kicking and reinviting the bot.

Another possibility is that you’re trying to assign a role that’s higher in the hierarchy than the bot’s role. Ensure the ‘NewRole’ is below the bot’s role in the server settings.

If those don’t work, try using the role ID instead of searching by name:

let role = server.roles.cache.get('actual_role_id_here');

Lastly, wrap the role assignment in a try-catch block to get more detailed error information:

try {
  await member.roles.add(role.id);
} catch (error) {
  console.error('Error adding role:', error);
}

This should help pinpoint the exact issue. Let me know if you need more help!

yo man, i had this same issue last week. make sure ur bot has the right perms in the server settings, not just in the code. also, check if the role ur tryin to add is above the bot’s role in the hierarchy. that messed me up before. if none of that works, try using the role ID instead of the name. good luck!