I am running into an "DiscordAPIError: Missing Permissions " error when trying to add a member to a role. My code is:
const role: Role = await this.getRole(requestingUser, roleId);
const member: GuildMember = await this.getGuildMember(memberId);
await member.roles.add(role);
The client should have the intents it needs. This is the code we use for getting the client instance for the calls:
async getClient () {
if (!this.client) {
// eslint-disable-next-line no-async-promise-executor
this.client = await new Promise(async (resolve, reject) => {
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS,
Intents.FLAGS.GUILD_MESSAGES
]
});
client.on('error', async (error) => {
reject(error);
});
client.on('ready', async () => {
resolve(client);
});
await client.login(this.botToken);
});
}
return this.client;
}
The bot does have have 'manage roles' permission:
The stack trace once it hits discord.js code:
DiscordAPIError: Missing Permissions
at RequestHandler.execute (/usr/src/app/node_modules/discord.js/src/rest/RequestHandler.js:350:13)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (/usr/src/app/node_modules/discord.js/src/rest/RequestHandler.js:51:14)
at async GuildMemberRoleManager.add (/usr/src/app/node_modules/discord.js/src/managers/GuildMemberRoleManager.js:124:7)
This is using discord.js 13.6.0 and node.js 16.6.0
Any ideas?
CodePudding user response:
If a role is higher than a user or bot's highest role, it's basically read-only for you, even if you have the ADMINISTRATOR
permission. The only thing that bypasses this is being the owner. Either move the bot's role up, the role being given down, or do a check and send an error message if it's higher
if (
role.position >= guild.me.roles.highest.position ||
!guild.me.permissions.has("MANAGE_ROLES")
) return interaction.reply({
content: "I don't have permissions to grant this role!",
ephemeral: true
})