Discord.js 多角色检查

2022-01-10 00:00:00 bots javascript 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
    }

相关文章