Javascript 找出某人是否有角色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45317305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find out if someone has a role
提问by R. Gillie
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:
我为服务器制作了一个简单的报价机器人,但管理员只希望 mod+ 人员能够添加报价以避免垃圾邮件。我去了文档并做了所有事情,但我无法让它工作。这是我所拥有的:
//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);
}
}
}
It's very finicky. Sometimes it will work if the user has the mod role, most of the times it wont. If I do
它非常挑剔。如果用户具有 mod 角色,有时它会起作用,大多数时候它不会。如果我做
console.log(message.memeber.roles.has(adminRole));
console.log(message.memeber.roles.has(modRole));
both will output to false, but will work? Honestly, I have no idea at this point.
两者都会输出为false,但会起作用吗?老实说,我现在不知道。
采纳答案by R. Gillie
The discord.js api has been updated and there is a better way since .exists()has been deprecated.
discord.js api 已更新,并且有更好的方法,因为.exists()已被弃用。
if (message.member.roles.some(role => role.name === 'Whatever')) {}
if (message.member.roles.some(role => role.name === 'Whatever')) {}
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.
这比.find()因为.find()返回角色对象(或未定义),然后将其转换为布尔值要好。该.some()方法默认返回一个布尔值。
回答by Wright
message.member.rolesis a collection. Instead of getting the roles object, then looking for it, just look for the role directly in the collection. Try this:
message.member.roles是一个集合。而不是获取角色对象,然后寻找它,只需直接在集合中寻找角色。尝试这个:
else if (command === "addquote" && arg) {
if(message.member.roles.find(r => r.name === "Admin") || message.member.roles.find(r => rname === "Mod")){
//Rest of your code
}
Note, the role name must be the name you put in the find including any emojis if there's any in the role name.
请注意,角色名称必须是您在查找中输入的名称,如果角色名称中有任何表情符号,则包括任何表情符号。
回答by bubmet
回答by Kugel Blitz
if(message.guild.roles.find(role => role.name === "VIP"))
if(message.guild.roles.find(role => role.name === "VIP"))
回答by HYC Media
This worked for me with version 12.2.0
这对我的 12.2.0 版有用
if(message.member.roles.cache.find(r => r.name === "Admin")) {
// Your code
}
You can also use r.idto check with the role id
您还可以使用r.id来检查角色 ID

