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

时间:2019-03-20 05:52:39

标签: 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.");

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

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;

这就是我想要发生的事情:
当用户运行具有所需权限的命令时,如果用户仅满足这些权限之一,则该命令将运行。机器人也是如此。它还检查所有角色,并查看这些角色是否也符合条件。 (对于漫游器和用户均适用)

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

2 个答案:

答案 0 :(得分:0)

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

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

||运算符表示OR,因此您可以使用它,并且如果用户具有管理员OR管理消息,它将触发。如果两者同时存在,也会触发。

答案 1 :(得分:0)

    var list = ["ADMINISTRATOR","MANAGE_MESSAGES","..etc"];
    function isHavePermission(msg,ty = "any")
    {
     let arr = [];
     if (ty == "any" ){
      list.forEach(val=>{
       if(msg.member.guild.me.hasPermission(val)){
        return true;
       }
      });
      return false;
     }else if(ty == "all"){
     list.forEach(val=>{
       if(msg.member.guild.me.hasPermission(val)){
        arr.push(true);
       }else{arr.push(false);}
      });
      return !arr.includes(false);
      }
    }
// in on event just do that
if(isHavePermission(message)){
that's it your code here
}