只有特定角色才能使用此命令

时间:2020-10-03 23:54:15

标签: javascript node.js discord.js

我正在使用discord.js并添加了ban命令,但是服务器中的所有用户都可以使用它;我想将用法限制为几个指定角色。

这是我的禁令命令代码:

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 个答案:

答案 0 :(得分:2)

您可以使用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');