問題描述
所以我一直試圖找到一種更好的方法來檢查我的不和諧機器人上的命令的多個角色到目前為止,我能做的最好的就是}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.*');
我想知道是否有某種方法可以將其放入數組中并使用數組中的任何內容作為角色檢查器有什么方法可以解決這個問題嗎?好的,所以我嘗試添加一個數組,但我無法讓它發(fā)揮作用
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 多角色檢查的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!