🐛 Issue: Permission Error (50013) When Deleting a Role with Discord Bot

Description: I am encountering a 50013 Permission error when my Discord bot tries to delete a role from a server. The role is confirmed to be below the bot's role in the hierarchy. The bot does have Admin permissions in the server. Expected Behavior: The role should be deleted without any permission errors. Actual Behavior: The bot throws a 50013 Permission error. Code Snippets: Role Removal Function:
export const removeAllMembersFromStartupRole = async (guild: Guild) => {
try {
const role: Role | null = guild.roles.cache.find(role => role.name === ROLE_NAME) ?? null;
if (role) {
await role.delete(`Removing bot from guild ${guild.id}`);
}
} catch (error) {
logger.error(error, `Failed to remove role ${ROLE_NAME} from members in guild ${guild.id}`);
}
}
export const removeAllMembersFromStartupRole = async (guild: Guild) => {
try {
const role: Role | null = guild.roles.cache.find(role => role.name === ROLE_NAME) ?? null;
if (role) {
await role.delete(`Removing bot from guild ${guild.id}`);
}
} catch (error) {
logger.error(error, `Failed to remove role ${ROLE_NAME} from members in guild ${guild.id}`);
}
}
Bot Initialization (Index File):
const client = new Client({
intents: [GatewayIntentBits.GuildMembers,GatewayIntentBits.DirectMessageTyping,GatewayIntentBits.DirectMessageReactions,GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages, GatewayIntentBits.DirectMessages],
partials: [Partials.Message, Partials.Channel]
});

for (const [event, handler] of Object.entries(eventHandlers)) {
if (handler) {
client.on(event, handler);
}
}
const client = new Client({
intents: [GatewayIntentBits.GuildMembers,GatewayIntentBits.DirectMessageTyping,GatewayIntentBits.DirectMessageReactions,GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages, GatewayIntentBits.DirectMessages],
partials: [Partials.Message, Partials.Channel]
});

for (const [event, handler] of Object.entries(eventHandlers)) {
if (handler) {
client.on(event, handler);
}
}
Any insights or solutions to this issue would be greatly appreciated.
8 Replies
d.js toolkit
d.js toolkit13mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
chewie
chewie13mo ago
can you log role.managed and role.comparePositionTo(guild.members.me.roles.highest)
LunarRage | DNA
LunarRage | DNAOP13mo ago
INFO [14:17:04.263] (99644): Managed Roles: @everyone, admin, botify, Mavis Bot Admin, Mavis Oracle
INFO [14:17:04.263] (99644): Role Position: 1 Highest Role Position: 3
INFO [14:17:04.263] (99644): Found 3 members with the "Mavis Bot Admin" role.
INFO [14:17:04.263] (99644): Managed Roles: @everyone, admin, botify, Mavis Bot Admin, Mavis Oracle
INFO [14:17:04.263] (99644): Role Position: 1 Highest Role Position: 3
INFO [14:17:04.263] (99644): Found 3 members with the "Mavis Bot Admin" role.
chewie
chewie13mo ago
can you please log exactly what I asked
LunarRage | DNA
LunarRage | DNAOP13mo ago
INFO [14:58:40.420] (170105): Managed Role Managed: false
INFO [14:58:40.420] (170105): Role Compare Position To: -2
INFO [14:58:40.420] (170105): Managed Role Managed: false
INFO [14:58:40.420] (170105): Role Compare Position To: -2
When I run the role.delete in my interaction command it will remove without any problems but if I run it in my guildDelete Event handler it has permission issues.
treble/luna
treble/luna13mo ago
Are you deleting the role from the guild that fired the guildDelete event?
chewie
chewie13mo ago
well, how do you expect the bot to remove a role on a guild the bot is not on?
LunarRage | DNA
LunarRage | DNAOP13mo ago
No need for the condescending tone. I apologize if I misunderstood something. Thank you for helping me understand the situation. I'm uncertain about the best event to use for the 'Clean up'. I had assumed I could proceed with this particular event before the bot was removed.
Want results from more Discord servers?
Add your server