šŸ› 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.
DT
d.js toolkitā€¢198d 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
C
chewie šŸŒˆā€¢198d ago
can you log role.managed and role.comparePositionTo(guild.members.me.roles.highest)
LD
LunarRage | DNAā€¢198d 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.
C
chewie šŸŒˆā€¢198d ago
can you please log exactly what I asked
LD
LunarRage | DNAā€¢198d 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.
T
treble/lunaā€¢198d ago
Are you deleting the role from the guild that fired the guildDelete event?
C
chewie šŸŒˆā€¢198d ago
well, how do you expect the bot to remove a role on a guild the bot is not on?
LD
LunarRage | DNAā€¢198d 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
More Posts
get all mesages after a specific timeIm making a purge command but when i try to delete messages it says it can only delete messages thatusing rest API to update server bannerI'm trying to use the REST api to update my server banner. The problem is that it seems to work - aboort erroron latest djsInvalid bitfield in audit log channel permissions eventHey, I currently have the issue that errors like these are sent multiple times a minute into my bot'StatusHello Everyone. I am currently having trouble on setting my bots status. Would anyone have a basic errors in my codehey so im trying to make a streaming selfbot but ive been getting loads of errors whenever i try to TTSi want to create a simple discord bot, that will read user messages in specific channel and bot willBot Crashing After a non permission user runds commandclient.on('messageCreate', (message) => { // Check if the message starts with "!announce" and thExtending Guild, Channel, Member etc - TypeScriptIs it possible to extend upon Guild, Channel, Member etc objects? I have tried extending them but Does anyone have a https request bot infrastructure??djs versionsWhat version would be the best to create a discord bot in ? have any suggestions?Anyone can help me to fix old code ?Hi there! Actually i'm stuck on an old code. ```ts const fullPermissions = commandsCol.reduce< Problems in developing discord botHello everyone, I'm new here , sorry, if i do something wrong. I'm trying to develop my own bot, I'mHow would you accomplish this?I've got the following situation. My bot is attempting to collect information from a user. At first,Is it best to stay away from batch functionality?I have a use case where I have a website interface for my bot. I can track users, edit them, accept How to filter MemberUpdates for TimoutsHey, i want do Log Timeouts but there is no AuditLogEvent for example "Member.Timouts". I need to fiEmojis in buttons doesn't workCode: https://sourceb.in/dCkWGPGOOj Error is that it says it is a invalid emoji, but it literally wopermission | bitfield invalid```js if (!permissions.has("VIEW_CHANNEL")) { interaction.reply("Channel has to be viewable toTermux - npm install discord.js > error 128Could anyone help me? I'm having this issue on Termux and I can't install Discord.jsUser.client.presence is nullHI! I want to detect if user is on discord mobile version or on desktop/ web one. In documentation