只有特定角色才能使用此命令 [英] Only specific roles can use this commands

查看:108
本文介绍了只有特定角色才能使用此命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using discord.js and added the ban command but all users in my server can use it; I want to restrict the usage to a few specified roles.

这是我的禁止命令代码:

Here is my ban command code:

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");
}

有什么方法可以添加可以使用此命令的角色,而无需从头开始重新输入内容?

Is there any way I can add the role that can use this command without retyping everything from the beginning?

推荐答案

您可以使用 Collection.has() Collection.some() ,或 Collections.every( )

You can use Collection.has(), Collection.some(), or 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');

这篇关于只有特定角色才能使用此命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆