Я пытаюсь создать четкую команду с правами роли для моего бота Discord. (Discord.js )

#discord #discord.js

#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. Вы получаете сообщение об ошибке?

2. @Lioness100 Нет, просто каждый, у кого нет прав, может использовать команду.

Ответ №1:

Попробуйте это:

 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 редактировали, если у участника не было требуемых разрешений, поэтому ваш код просто продолжался в обычном режиме.