discord ban command not proceeding

Advertisements

I’m trying to implement a ban command in my Discord.js bot, but it doesn’t seem to work. What could be the issue? No errors are in console.

client.on('message', message => {
  if (message.content.startsWith(prefix) && message.content.startsWith('ban')) {
    const args = message.content.split(' ');
    const user = message.mentions.users.first();
    
    if (!args[1]) {
      return message.channel.send('Please mention the user to ban!');
    }
    
    message.guild.members.ban(user)
      .then(() => {
        message.channel.send(`Successfully banned ${user.tag}!`);
      })
      .catch(err => {
        message.channel.send('An error occurred while trying to ban the user.');
        console.error(err);
      });
  }
});

I tried logging user but nothing works.

>Solution :

In your conditional statement you are checking if it starts with a prefix and also the string ban, unless your prefix is ban this will never be true. Consider the following fix:

if (!message.content.startsWith(prefix)) return;

// Split the message content into an array of words
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();

if(command !== "ban") return;

Leave a ReplyCancel reply