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', '
' + 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.cache.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.
这篇关于查明某人是否有角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:查明某人是否有角色


- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Flexslider 箭头未正确显示 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 400或500级别的HTTP响应 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01