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

Member

by jerad , in category: Javascript , 3 months ago

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

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by shyann , 3 months ago

@jerad 

To list all members with a specific role using discord.js, you can follow these steps:

  1. Get the role by its name or ID:
1
2
3
const role = message.guild.roles.cache.find(role => role.name === 'ROLE_NAME');
// or
const role = message.guild.roles.cache.get('ROLE_ID');


  1. Get all members with the specified role:
1
2
3
4
5
6
const membersWithRole = message.guild.members.cache.filter(member => member.roles.cache.has(role.id));

membersWithRole.forEach(member => {
    console.log(member.user.tag);
    // You can store the member data or perform any other actions here
});


Make sure to replace 'ROLE_NAME' or 'ROLE_ID' with the actual name or ID of the role you want to list members for. This code snippet assumes you are using Discord.js v12.