Только определенные роли могут использовать эти команды

#javascript #node.js #discord.js

#javascript #node.js #discord.js

Вопрос:

Я использую discord.js и добавил команду запрета, но все пользователи на моем сервере могут ее использовать; я хочу ограничить использование несколькими указанными ролями.

Вот мой код команды запрета:

 const user = msg.mentions.users.first();
if (user) {
  var cringeboat = bot.channels.cache.get("752943625268232212");
  const member = msg.guild.member(user);
  var date = new Date();
  const BanEmbed = new MessageEmbed()
    .setTitle("CRIIIIIIIIINGE BOAT")
    .setColor("#060200")
    .setImage("https://i.postimg.cc/1X0n5kPT/cringe-boat.jpg")
    .setDescription("description")
    .setFooter(
      date.getFullYear()  
        "/"  
        date.getMonth()  
        "/"  
        date.getDate()  
        "  "  
        date.getHours()  
        ":"  
        date.getMinutes()  
        ":"  
        date.getSeconds()
    );

  if (member) {
    member.ban({ ression: "bad person" }).then(() => {
      cringeboat.send(BanEmbed);
    });
  } else {
    msg.channel.send("that user is not in the guild");
  }
} else {
  msg.channel.send("you need to specify a person");
}
  

Могу ли я каким-либо образом добавить роль, которая может использовать эту команду, без повторного ввода всего с самого начала?

Ответ №1:

Вы можете использовать Collection.has() , Collection.some() , или Collections.every()

 // get all of the message author's roles
const roles = message.member.roles.cache

// `Collection.has()` requires a key. In this case, the role ID
if (!roles.has('Role ID Here'))
  return message.channel.send('You do not have the required roles');

// `Collection.some()` will return true if, after running the giving function
// through every element in the collection, at least one element returned true
if (!roles.some((role) => role.name === 'Some Role Name'))
  return message.channel.send('You do not have the required roles');

// you could also create an array of role IDs
const arr = ['Role ID', 'Role ID', 'Role ID'];

if (!arr.some((id) => roles.has(id)))
   return message.channel.send('You do not have any of the required roles');

// `Collection.every()` only return true if every element pass the given test
// i.e., they would have to have all the roles
if (!arr.every((id) => roles.has(id)))
  return message.channel.send('You do not have all of the required roles');
  

Комментарии:

1. Спасибо, сэр! Хорошего дня <3