ViridianZe
ViridianZe

Reputation: 121

Discord.js multi role check

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.*');
}



}
}

Upvotes: 1

Views: 754

Answers (1)

hernik
hernik

Reputation: 216

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

Example:

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
    }

Upvotes: 1

Related Questions