为什么我的角色权限代码不起作用? (discord.js)

时间:2020-09-06 11:32:15

标签: javascript node.js discord discord.js

因此,我尝试编写代码,以便仅当用户具有特定角色时才运行特定命令,但此命令不起作用。我不确定是否是因为我的角色太多,但这是代码:

case "test":
  if (
    !message.member.roles.cache.find(
      (r) => r.name === "test1",
      "test2",
      "test3",
      "test4",
      "test5",
      "test6"
    )
  )
    return message.reply(
      'You don\'t have permission to execute that command! The role "test1", "test2", "test3", "test4", "test5", or "test6" is needed to execute that command.'
    );
  message.channel.send("this is a test");

3 个答案:

答案 0 :(得分:0)

r.name === "test1", "test2", "test3", "test4", "test5", "test6"正在使用comma operator,所以它等效于"test6",这可能不是您的意思。

尝试类似["test1", "test2", "test3", "test4", "test5", "test6"].includes(r.name)之类的东西。

答案 1 :(得分:0)

如托马斯所说:

r.name === "test1", "test2", "test3", "test4", "test5", "test6"正在使用comma operator,所以它等效于"test6",这可能不是您的意思。

相反,您应该使用Array.prototype.includes

if (
 !message.member.roles.cache.some((r) =>
  ['test1', 'test2', 'test3', 'test4', 'test5', 'test'].includes(r.name)
 )
)
 return message.reply(
  'You don\'t have permission to execute that command! The role "test1", "test2", "test3", "test4", "test5", or "test6" is needed to execute that command.'
 );

下面是实际使用的.includes()方法的代码段示例:

const word = 'hello'

// returns 'false', as none of the words in this array match `hello`
console.log(['greetings', 'salutations', 'good morning'].includes(word));

// but, if we add `hello` to the array...
console.log(['greetings', 'salutations', 'good morning', 'hello'].includes(word));

答案 2 :(得分:0)

if(!message.member.hasPermission("ADMINISTRATOR")) return message.channel.send('You dont have the right permissions to do that.')

您可以使用此代码行。您也可以将“管理员”更改为其他名称,例如“ BAN_MEMBERS”或“ KICK_MEMBERS”等。