Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

discord ban command not proceeding

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading