为什么 bulkDelete 不能正常工作?

Why does bulkDelete not work with no error?

我已经尝试查找拼写错误和其他不准确之处,并尝试为 prune 命令添加权限要求,但是当我输入金额。

详细信息:我正在尝试制作一个可以根据输入进行修剪的 Discord 机器人。我使用 DJS v12 并遵循(编辑)本指南 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!');
   });
  }
});

你的 prune 命令不起作用的原因是你的命令解析。 args 总是 nullcmd 总是包含整个字符串。

因此,如果您输入 $prune 3,您的 args 将为空,而 cmd 包含 prune 3。这就是为什么你的 if 在这里:

else if (cmd === `prune`)

不匹配(如果您指定了参数)并且您的修剪命令永远不会执行。

为了解决这个问题,您更改了命令解析:

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

注意:另外你的问题好像有错别字:

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

所以将该行更改为

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