Sakura Kaslana
Sakura Kaslana

Reputation: 65

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

I am trying to make a command that is only available for certain roles only. I tried checking them in an array, but it doesn't seem to work.

the command:

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"],
}

Upvotes: 2

Views: 467

Answers (2)

Thomas Elston-King
Thomas Elston-King

Reputation: 401

You could use roles.cache.some() as listed on the Collection docs: https://discord.js.org/#/docs/collection/main/class/Collection

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

Upvotes: 4

Kaspr
Kaspr

Reputation: 1616

There are several ways you can do this:

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")
        }
    }
});

Upvotes: 1

Related Questions