Home > Mobile >  How do I get all members with a role?
How do I get all members with a role?

Time:07-12

I'm trying to send a DM to all members with a specific role. I've tried that to get members with a role:

guild.roles.cache.get(ROLE_ID).members

and

guild.roles.fetch(ROLE_ID)

My problem is that the bot only returns my user.

I know someone has asked a question like this before where it was solved using the correct intents, but it's not the case for me.

CodePudding user response:

I think the problem is that Role#members returns a Collection of the cached guild members only that have this role. Try to fetch all members first and then try again:

let members = await guild.members.fetch()
let membersWithRole = guild.roles.cache.get(ROLE_ID).members

Or you can just filter the members by their role:

let members = await guild.members.fetch()
let membersWithRole = members.filter(member => member.roles.cache.has(ROLE_ID));
  • Related