首页 > 解决方案 > 检查用户是否具有任何列出的所需权限

问题描述

我想检查用户是否满足命令列出的任何权限。例如,如果所需的权限是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.");

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

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;

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

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

标签: javascriptdiscord.js

解决方案


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

if(message.member.guild.me.hasPermission('ADMINISTRATOR') || message.member.guild.me.hasPermmission('MANAGE_MESSAGES'))

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


推荐阅读