找出某人是否有角色 [英] Find out if someone has a role
问题描述
我为服务器制作了一个简单的引用机器人,但管理员只希望mod +人能够添加引号以避免垃圾邮件。我去了文档并做了一切,但我不能让它工作。这就是我所拥有的:
I made a simple quote bot for a server, but the admin only wants mod+ people to be able to add quotes to avoid spam. I went to the documentation and did everything, but I can't get this to work. Here's what I have:
//other code
else if (command === "addquote" && arg) {
let adminRole = message.guild.roles.find("name", "Admin");
let modRole = message.guild.roles.find("name", "Mod");
if(message.member.roles.has(adminRole) || message.member.roles.has(modRole)){
const hasArr = arr.some((el) => {
return el.toLowerCase().replace(/\s/g, '') === arg.toLowerCase().replace(/\s/g, '');
});
if(hasArr){
message.channel.send(arg.replace(/\s+/g,' ').trim() + " is already a Quote");
} else {
fs.appendFileSync('./Quotes.txt', '\r\n' + arg);
message.channel.send("Quote added: " + arg);
arr.push(arg);
}
}
}
这非常挑剔。有时,如果用户具有mod角色,它将起作用,大多数时候它不会。如果我这样做
It's very finicky. Sometimes it will work if the user has the mod role, most of the times it wont. If I do
console.log(message.memeber.roles.has(adminRole));
console.log(message.memeber.roles.has(modRole));
两者都会输出为false,但是会有效吗?
老实说,我现在还不知道。
both will output to false, but will work? Honestly, I have no idea at this point.
推荐答案
discord.js api已更新,有一个更好的方法,因为 .exists()
已被弃用。
The discord.js api has been updated and there is a better way since .exists()
has been deprecated.
if(message。 member.roles.some(role => role.name ==='Whatever')){}
这比 .find()
因为 .find()
返回角色对象(或未定义),然后将其转换为布尔值。 .some()
方法默认返回一个布尔值。
This is better than .find()
because .find()
returns the role object (or undefined) which is then converted into a boolean. The .some()
method returns a boolean by default.
这篇关于找出某人是否有角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!