简体   繁体   中英

How to find a member role in a specific server

I have these two lines of code which checks if the message author have a specific role into the main server of the bot:

const main = client.guilds.cache.get(698725823464734852);

// Blacklist Checker :
if (message.author.main.roles.cache.find(r => r.name === "BlackListed.")) message.reply("you are blacklisted...")

I have tried to connect the functions at message.author.main.roles.cache.find however, it didn't work.

There are two issues with the code you have provided us with;
Firstly the guild ID must be a string and secondly message.author.main will give you an error.

Please see the working code I have provided below.

const Discord = require('discord.js'); //Define discord
const client = new Discord.Client(); //Define client

client.on('message', message => {
    const guild = client.guilds.cache.get('698725823464734852'); // Define guild (guild ID must be a string)
    const member = guild.members.cache.get(message.author.id); //Find the message author in the guild
    if (!member) return console.log('Member is not blacklisted'); //If the member is notin the guild
    if (member.roles.cache.find(r => r.name.toLowerCase() === 'blacklisted')) return message.reply('Unfortunately, you have been blacklisted.'); //Let the user know they have been blacklisted.
});

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