Search code examples
javascriptdiscord.jscommando

Ban command is not being recognized


The command requires a reason to work, however, it still does not ban even if I mention someone and give a reason. It's like the command isn't recognized!

bot.on('message', async message => {
  if (message.content == prefix + "ban") {
    if (!message.member.roles.some(r => ["Administrator", "Co-owner"].includes(r.name)))
      return message.reply("Sorry, you don't have permissions to use this!");

    let member = message.mentions.members.first();
    if (!member)
      return message.reply("Please mention a valid member of this server");
    if (!member.bannable)
      return message.reply("I cannot ban this user! Do they have a higher role? Do I have ban permissions?");

    var reason = args.slice(1).join(' ');
    if (!reason) reason = "No reason provided";
    await member.ban(reason);

  }
});

Solution

  • Finally got it to work! This was my code at the end:

    bot.on('message', message => {
      let member = message.mentions.members.first();
      if (message.content.startsWith(prefix + "ban")) {
        if (!message.member.hasPermission('BAN_MEMBERS'))
          return message.reply("Sorry, you don't have permissions to use this!");
        if (!member)
          return message.reply("Please mention a valid member of this server");
        if (!member.bannable)
          return message.reply("I cannot ban this user! Do they have a higher role? Do I have ban permissions?");
    
        // V This line has been changed V
        var reason = message.content.split(' ').slice(2).join(' ');
        if (!reason) return message.reply("Please specify a reason!");
        member.ban(reason);
      }
    });
    

    It was all because of the reason! Thanks to everyone who helped me, this opened doors for a lot more commands for me.