Discord.js bot assigning roles to users throws permission denied error

Viewed 26

I'm trying to make a discord bot (with js) that can assign roles to users. Discord version: 14.3.0 Nodejs version: 16.17.0

Im using this function to assign role.

const client = new Discord.Client({
    intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.GuildMembers,
    GatewayIntentBits.DirectMessages
  ]
});
....

var role = message.guild.roles.cache.find(role => role.name == "Role_name")
guildMember.roles.add(role) // this line is causing the error

This is the exact error im getting

/node_modules/@discordjs/rest/dist/lib/handlers/SequentialHandler.cjs:293
    throw new DiscordAPIError.DiscordAPIError(data, "code" in data ? data.code : 
data.error, status, method, url, requestData);
          ^

DiscordAPIError[50013]: Missing Permissions
    at SequentialHandler.runRequest (    /media/pranav/Storage/ie/programming/nodejs/project-l    isabey/node_modules/@discordjs/rest/dist/lib/handlers/SequentialHandler.cjs:293:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async SequentialHandler.queueRequest (    /media/pranav/Storage/ie/programming/nodejs/project-l    isabey/node_modules/@discordjs/rest/dist/lib/handlers/SequentialHandler.cjs:99:14)
    at async REST.request (/media/pranav/Storage/ie/programming/nodejs/project-l    isabey/node_modules/@discordjs/rest/dist/lib/REST.cjs:52:22)
    at async GuildMemberRoleManager.add (    /media/pranav/Storage/ie/programming/nodejs/project-l    isabey/node_modules/discord.js/src/managers/GuildMemberRoleManager.js:129:7) {
  rawError: { message: 'Missing Permissions', code: 50013 },
  code: 50013,
  status: 403,
  method: 'PUT',
  url: '    https://discord.com/api/v10/guilds/1011546412011507823/members/1021733308457041970/roles/1    021714244846223360',
  requestBody: { files: undefined, json: undefined }
}
  1. I tried giving the bot Mod permissions in the server
  2. Invited bot to server with "discord.com/api/oauth2/authorize?client_id=xxxxxxxxxx&permissions=8&scope=bot%20applications.commands"
  3. In developer portal all 3 "privileged gateway intents" are checked.

Still i'm getting the same error. How can I fix this ?

Thanks!

1 Answers

Make sure that the role you're trying to assign is lower to your highest bot role.

In server settings, literally drag the bots role above the roles it will be assigning.

Related