检查用户是否具有任何列出的必需权限
Check if user has any of listed required permissions
我想检查用户是否满足命令列出的任何权限。一个例子是,如果所需的权限是 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;
这是我想要发生的事情:
当用户 运行s 具有所需权限的命令时,如果用户仅满足其中一个权限,则命令 运行s。这同样适用于机器人。它还会检查所有角色,看看是否有任何角色也符合标准。 (机器人和用户)
实际发生了什么:
该机器人要求用户同时满足其自身和用户的权限。在两个用户都满足代码中要求的所有权限之前,机器人不会 运行 命令。机器人只检查用户的最高角色,不检查其余角色以查看是否有任何角色满足要求的权限。
只需使用 OR 语句检查两次权限。
if(message.member.guild.me.hasPermission('ADMINISTRATOR') || message.member.guild.me.hasPermmission('MANAGE_MESSAGES'))
||
运算符代表 OR,因此您可以使用它,如果用户有管理员或管理消息,它将触发。如果两者都有,它也会触发。
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
}
我想检查用户是否满足命令列出的任何权限。一个例子是,如果所需的权限是 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;
这是我想要发生的事情:
当用户 运行s 具有所需权限的命令时,如果用户仅满足其中一个权限,则命令 运行s。这同样适用于机器人。它还会检查所有角色,看看是否有任何角色也符合标准。 (机器人和用户)
实际发生了什么:
该机器人要求用户同时满足其自身和用户的权限。在两个用户都满足代码中要求的所有权限之前,机器人不会 运行 命令。机器人只检查用户的最高角色,不检查其余角色以查看是否有任何角色满足要求的权限。
只需使用 OR 语句检查两次权限。
if(message.member.guild.me.hasPermission('ADMINISTRATOR') || message.member.guild.me.hasPermmission('MANAGE_MESSAGES'))
||
运算符代表 OR,因此您可以使用它,如果用户有管理员或管理消息,它将触发。如果两者都有,它也会触发。
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
}