我正在尝试为Discord机器人做出具有角色权限的明确命令。 (Discord.js)

时间:2020-08-21 18:45:11

标签: discord discord.js

我已经有一些代码,但是我将如何实现一些其他代码以使该命令仅由具有MANAGE_MESSAGES权限的用户访问?

我自己尝试做的事情:

    else if (message.content.startsWith(`${prefix}clear`)) {
        const amount = parseInt(args[0]);;
        
        if (isNaN(amount)) {
            return message.reply('that doesn\'t seem to be a valid number.');
        } else if (amount <= 0 || amount > 100) {
                return message.reply('you need to input a number between 1 and 100.');
                }
            
        message.channel.bulkDelete(amount, true).catch(err => {
            console.error(err);
            message.channel.send('Uh oh! Something went wrong!');

        }).catch(() => {
            if (!message.member.hasPermission(['MANAGE_MESSAGES'])) {
                message.reply("you do not have permission to use this command!");
            
            }
        });
    }

最后没有多余的地方:

    else if (message.content.startsWith(`${prefix}clear`)) {
        const amount = parseInt(args[0]);;
        
        if (isNaN(amount)) {
            return message.reply('that doesn\'t seem to be a valid number.');
        } else if (amount <= 0 || amount > 100) {
                return message.reply('you need to input a number between 1 and 100.');
                }
            
        message.channel.bulkDelete(amount, true).catch(err => {
            console.error(err);
            message.channel.send('Uh oh! Something went wrong!');

        });
    }

1 个答案:

答案 0 :(得分:0)

尝试一下:

else if (message.content.startsWith(`${prefix}clear`)) {
  // put this at the very top
  if (!message.member.hasPermission("MANAGE_MESSAGES")) {
    return message.reply("you do not have permission to use this command!");

    const amount = parseInt(args[0]);

    if (isNaN(amount))
      return message.reply("that doesn't seem to be a valid number.");
    if (amount <= 0 || amount > 100)
      return message.reply("you need to input a number between 1 and 100.");

    message.channel
      .bulkDelete(amount, true)
      .catch((err) => {
        console.error(err);
        message.channel.send("Uh oh! Something went wrong!");
      })
      .catch((err) => console.log(err));
  }
}

我认为问题在于,如果该成员没有所需的权限,您就无法return,因此您的代码只能继续正常运行。