How to list all members with a role in discord.js?

Member

by lottie , in category: Javascript , 3 months ago

How to list all members with a role in discord.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cali_green , 3 months ago

@lottie 

To list all members with a specific role in discord.js, you can do the following:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
const Discord = require('discord.js');
const client = new Discord.Client();

client.once('ready', () => {
    const guild = client.guilds.cache.get('YOUR_SERVER_ID');
    const role = guild.roles.cache.find(role => role.name === 'ROLE_NAME');

    const membersWithRole = role.members.map(member => member.user.tag);

    console.log(membersWithRole);
});

client.login('YOUR_BOT_TOKEN');


Replace 'YOUR_SERVER_ID' with the ID of your server and 'ROLE_NAME' with the name of the role you want to list members with. This code will log an array of the tags of all members with the specified role in the console.