检查用户是否具有任何列出的所需权限

Som*_*son 5 javascript discord.js

我想检查用户是否满足命令列出的任何权限。例如,如果所需的权限是AdministratorManage_Webhooks,并且如果用户仅满足这些权限之一,则该命令将运行。

这是一些代码

if (message.member.guild.me.hasPermission("MANAGE_WEBHOOKS", "ADMINISTRATOR"))
  return message.channel.send("I don't have the permissions to make webhooks, please contact an admin or change my permissions!")
if (!message.member.hasPermission("MANAGE_WEBHOOKS", "ADMINISTRATOR")) return message.channel.send("You need to be an admin or webhook manager to use this command.");
Run Code Online (Sandbox Code Playgroud)

如果需要其余代码,就在这里。

const Discord = require('discord.js');
const commando = require('discord.js-commando');

class pingy extends commando.Command {
  constructor(client) {
    super(client, {
      name: 'pinghook',
      group: 'help',
      memberName: 'pinghook',
      description: 'This is where you can set the pinghook.',
      aliases: ['ph', 'pingh', 'phook', 'hook'],
    })
  }

  async run(message, args) {
    if (message.member.guild.me.hasPermission("MANAGE_WEBHOOKS", "ADMINISTRATOR"))
      return message.channel.send("I don't have the permissions to make webhooks, please contact an admin or change my permissions!")
    if (!message.member.hasPermission("MANAGE_WEBHOOKS", "ADMINISTRATOR")) return message.channel.send("You need to be an admin or webhook manager to use this command.");

    const avatar = `https://cdn.discordapp.com/attachments/515307677656678420/557050444954992673/Generic5.png`;
    const name2 = "PingBot";
    const hook = await message.channel.createWebhook(name2, avatar).catch(error => console.log(error))
    await hook.edit(name2, avatar).catch(error => console.log(error))
    message.channel.send("Your webhook is now created! You can delete it at any time and can be re-added by using this command! You can also edit the webhook's name or avatar.")

    setInterval(() => {
      hook.send("@everyone you've been pinged.")
    }, 500);
  }
};
module.exports = pingy;
Run Code Online (Sandbox Code Playgroud)

这就是我想要发生的情况:
当用户运行具有所需权限的命令时,如果用户仅满足这些权限之一,则该命令将运行。这同样适用于机器人。它还检查所有角色并查看其中是否有任何角色也符合标准。(机器人和用户均适用)

实际发生的情况:
机器人要求用户满足其自身和用户的权限。在两个用户都满足代码中所需的所有权限之前,机器人不会运行该命令。该机器人仅检查用户的最高角色,不会检查其余角色以查看其中是否有任何角色满足所需的权限。

小智 6

只需使用 OR 语句检查两次权限即可。

if(message.member.guild.me.hasPermission('ADMINISTRATOR') || message.member.guild.me.hasPermmission('MANAGE_MESSAGES'))
Run Code Online (Sandbox Code Playgroud)

运算||符代表 OR,因此您可以使用它,如果用户有管理员或管理消息,它将触发。如果两者都有的话也会触发。

  • 对于discord js 13,将`hasPermission`更改为`permissions.has` (3认同)