Discord.js 多角色检查

时间:2023-02-10
本文介绍了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 多角色检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:如何使机器人状态显示为“从移动设备在线&quo 下一篇:Discord Music bot - 我播放的歌曲在结束前 10-20 秒停

相关文章

最新文章