Unable to fetch all members from role

Hello. For my app, i need to get all members of a predefined role id to fill a const Unfortunately, when i fetch members from role, i get only me in member, other member is not present in map. I have enabled Guild and Guild_Members intents.
4 Replies
d.js toolkit
d.js toolkit8mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
Loris 🐐
Loris 🐐OP8mo ago
Here is my code:
const guildRole = await (interaction.guild as Guild).roles.fetch(teamRoleId, { force: true });
console.log(guildRole?.members)
const guildRole = await (interaction.guild as Guild).roles.fetch(teamRoleId, { force: true });
console.log(guildRole?.members)
Mark
Mark8mo ago
You would need to fetch all members, then access guildRole.members Usually, you don't need to fetch the role, as it's cached on ready if you have the guilds intent. The members getter just filters the guild.members.cache collection, which is why you need to fetch them first
Loris 🐐
Loris 🐐OP8mo ago
Okayy ty, it works ! My new code, for other people:
const guildRole = (interaction.guild as Guild).roles.cache.get(teamRoleId);

if (guildRole) {
await guildRole.guild.members.fetch()
console.log(guildRole.members.size) // returns number of members who have the role
}
const guildRole = (interaction.guild as Guild).roles.cache.get(teamRoleId);

if (guildRole) {
await guildRole.guild.members.fetch()
console.log(guildRole.members.size) // returns number of members who have the role
}

Did you find this page helpful?