javascript - 检查用户是否具有任何列出的所需权限
问题描述
我想检查用户是否满足命令列出的任何权限。例如,如果所需的权限是Administrator
或Manage_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 权限。
解决方案
只需使用 OR 语句检查权限两次。
if(message.member.guild.me.hasPermission('ADMINISTRATOR') || message.member.guild.me.hasPermmission('MANAGE_MESSAGES'))
运算符代表 OR,因此||
您可以使用它,如果用户具有管理员或管理消息,它将触发。如果两者都有,它也会触发。
推荐阅读
- http - 为什么 _Layout.cshtml 中导致无处的操作链接报告 500 错误而不是 404 未找到文件?
- bash - 监视连续的文本文件并在命中某行时执行某些操作
- ios - 如何根据不同的屏幕尺寸(高度和宽度)调整子视图?
- r - 在R中的tidyverse(dplyr)中选择性地行求和
- javascript - Wordpress 中的脚本化多文件上传
- docker - 从 iso 和 apt 重复安装 docker
- javascript - 为什么我在 ajax post 调用后得到 json 响应页面?
- java - 我无法从使用 Java JLink 工具创建的应用程序映像中读取外部文件
- google-apps-script - 将 JSON 加载到 BigQuery 时出现列(字段)顺序问题
- .net - 如何加密 appsettings.js 字符串并且仍然可用?