Skip to content
Advertisement

Discord Bot unexpectedly exits with error when successfully banning someone

The problem that I have is with the case “ban”. When I go “+ban” and then mention the user, it works. The user is banned and the message is sent but then it exits with a message error about the Discord API and permission, even though I have admin permission for the bot.

And when I don’t mention anyone, it does what it’s supposed to do and just gives out the “There is no one to ban.” message but it then exits with an error (Error [BAN_RESOLVE_ID]: Couldn't resolve the user ID to ban.). I need to rerun the code to get the bot started again.

Do you know how to to keep the bot running with no issues?

JavaScript

Advertisement

Answer

The first problem is that you try to ban someone even if there is no member mentioned or if there was no ban command. You try to define a banMember variable using let banMember = msg.guild.members.ban(user) but it calls the ban() method before you check if the command is “ban”. You need to move this ban() method inside the switch statement.

Second, you try to ban a User. msg.mentions.users.first() returns a User if there’s someone mentioned. Users don’t have a ban() method, only GuildMembers have.

Instead of msg.mentions.users you should use msg.mentions.members.

Your code should look something like this:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement