简体   繁体   中英

How do I check for a role in a specific server?

Here is my code:

client.on('message', message => {
    if (message.content === '+sdrive') {
        if (message.channel.type === "dm") {
            if (message.member.roles.cache.some(role => role.name === 'Nitro Booster')) {
            }
        }
    }
})

I need it so that it will check if the person has a role in a specific server.

Since this command is sent via DM, you need to tell the Bot which guild it should check.
I'm not sure if there is a shorter version, but this should do the trick:

if (client.guilds.cache.get("YOUR_GUILD_ID").members.cache.get(message.author.id).roles.cache.some(role => role.name === 'Nitro Booster')){
    // Do something...
}

Note: The User and the Bot have to be on the same server!


Less noisy version:

const hasRole = client.guilds.cache
    .get("YOUR_GUILD_ID").members.cache
    .get(message.author.id).roles.cache
    .some(role => role.name === 'Nitro Booster');

if (hasRole){
    // Do something...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM