Home > database >  How to fix Ban command not working Discord.js v12?
How to fix Ban command not working Discord.js v12?

Time:07-28

The ping command works, but when I try to use the ban command, it doesn't do anything. It won't even log to the console. I've tried [email protected] and [email protected]. I've also tried running my code on another computer and it still won't work.

Here is my code:

const Discord = require("discord.js");
const client = new Discord.Client();



client.on('message', message => {
 if (message.content === '!ping') {
    message.channel.send('Pong!');
 } else if (message.content === '!getBanned') {
        const member = message.mentions.members.first();

        if (!member) {
            console.log('member not found');
        } else {
            member.ban();
        }
    }
});```

CodePudding user response:

That's because in the part of the code where you check if the message.content equals '!getBanned', you used the equal operator, ===. So now the entire content of the message has to be exactly !getBanned, so when you use, e.g. !getBanned @Wumpus, the content of the message isn't equal. There are many ways to fix this, but for now you should just go with the string.startsWith() function:

if (message.content.startsWith("!getBanned") {
    // put all the code that gets the member mention and bans the member here
}
  • Related