Discord.js multi role check(Discord.js 多角色检查)
问题描述
所以我一直试图找到一种更好的方法来检查我的不和谐机器人上的命令的多个角色到目前为止,我能做的最好的就是}else{
so ive been trying to find a better way to check multiple roles for commands on my discord bot so far the best i can do is }else{
run(message) {
if(message.member.roles.find("name", "Pheonix")){
return message.say('These are dark times. . .', {files: ["./resources/videos/darktimes.mp4"]});
}else{
if(message.member.roles.find("name", "Renowned Wizard")){
return message.say('These are dark times. . .', {files: ["./resources/videos/darktimes.mp4"]});
}else{
return message.say('*Patreon restricted.*');
我想知道是否有某种方法可以将其放入数组中并使用数组中的任何内容作为角色检查器有什么方法可以解决这个问题吗?好的,所以我尝试添加一个数组,但我无法让它发挥作用
And i was wondering is there some way i could chuck this in an array and use whatever is in the array as a checker for roles is there any ways to go about this? okay so i tried to add onto with an array but i cant get that to function
var morsmordreRoles = [
'Dev',
'Renowned Wizard',
'Pheonix',
'Moderator'
]
if(message.member.roles.find("name", morsemordreRoles)){
return message.say('*You mark the sky with the presence of The Dark Lord* ', {files: ["./resources/gifs/morsmordre.gif"] });
}else{
return message.say('*Death Eater restricted.*');
}
}
}
推荐答案
如果要使用数组,可以使用 foreach 和 bool 检查用户是否有 any数组中的角色,您也应该在 discord.js v12 中使用 .cache.some
而不是 find
,如 这里
If you want to use an array, you can use a foreach and a bool check to check if the user has any of the roles in the array, also you should use .cache.some
instead of find
in discord.js v12 as seen here
例子:
var morsmordreRoles = [
'Dev',
'Renowned Wizard',
'Pheonix',
'Moderator'
]
var hasRole = false;
morsmordreRoles.forEach(findrole =>{
if(message.member.roles.cache.some(role => role.name === findrole)) hasRole = true; //if user has role, sets bool to true
})
if(hasRole === true){
//code when has role
}
else{
//code when has no role
}
这篇关于Discord.js 多角色检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Discord.js 多角色检查


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