检查用户是否在角色数组中具有特定角色

Checking if a user have a certain role in an array of role

我正在尝试创建一个仅适用于特定角色的命令。 我试着在一个数组中检查它们,但它似乎不起作用。

命令:

const Command = require("../structures/Command.js");
const config = require("../data/config.json");

module.exports = new Command({
    name: "test",
    description: "test",

    async run(message, args, client) {
        if (message.author.bot) return;
        if (message.member.roles.cache.some(role => role.id === config.adminRoles)) {return message.reply("You have the admin role")} else {message.reply("No")}
    }
});

config.json

{
"token": "token_go_here",
"prefix": "ab!",
"adminRoles": ["931469781234769940","931462231969910794"],
}

您可以通过多种方式执行此操作:

config.json

{
"token": "token_go_here",
"prefix": "ab!",
"admin1": "931469781234769940",
"admin2": "931462231969910794"
}

command.js

const Command = require("../structures/Command.js");
const { admin1, admin2 } = require("../data/config.json")

module.exports = new Command({
    name: "test",
    description: "test",

    async run(message, args, client) {
        if (message.author.bot) return;
        if (message.member.roles.cache.has(admin1) || message.member.roles.cache.has(admin2)) {
            return message.reply("You have the admin role")
        }
    }
});

您可以使用集合文档中列出的 roles.cache.some()https://discord.js.org/#/docs/collection/main/class/Collection

if (roles.cache.some(role => config.adminRoles.includes(role.id))) {
 // ...
}