Search code examples
javascriptdiscorddiscord.jsbotsbulk

Why does bulkDelete not work with no error?


I've tried to look for typo and other inaccuracies and tried to add permission requirement for the prune command but still, the ping pong and the "not a valid number" replies work but not the prune when I enter the amount.

Details: I'm trying to make a Discord bot that can prune based on input. I use DJS v12 and follow(ed) this guide https://v12.discordjs.guide/creating-your-bot/commands-with-user-input.html#number-ranges

if (!msg.content.startsWith(prefix) || msg.author.bot) return;
if (!msg.member.hasPermission("BAN_MEMBERS")) {
  msg.channel.send("You don\'t have permission.");
}
const args = msg.content.slice(prefix.length).trim().split('/ +/');
const cmd = args.shift().toLowerCase();
  if (cmd === `ping`) {
    msg.reply('pong.');
  } else if (cmd === `prune`) {
    if (!msg.guild.me.hasPermission("MANAGE_MESSAGES")) return;
    const amount = parseInt(args[0]) + 1;
    
    if (isNaN(amount)) {
      return msg.reply('Not a valid number.');
    } else if (amount <= 1 || amount > 100) {
      return msg.reply('Please input a number between 1 and 99.');
    }
  msg.channel.bulkDelete(amount, true).catch(err => {
        console.error(err);
        msg.channel.send('Error!');
   });
  }
});

Solution

  • The reason why your prune command doesn't work, is because of your command parsing. args is always null whereas cmd always contains the whole string.

    So if you enter $prune 3, your args will be empty and cmd contains prune 3. That's why your if here:

    else if (cmd === `prune`)
    

    doesn't match (if you specified arguments) and your prune command never gets executed.

    To fix that, you have change your command parsing:

    const cmd = msg.content.split(" ")[0].slice(prefix.length);
    const args = msg.content.split(" ").slice(1);
    

    Note: Also you seem to have a typo in your question:

    if (!msg.guild.me.hasPermission("MANAGE_MESSAGES") return;
    //                           Missing ")" here ----^
    

    So change that line to

    if (!msg.guild.me.hasPermission("MANAGE_MESSAGES")) return;