Get all users who have a role

I'm trying to get a list of User IDs for everyone in a server who has a particular role. Here is my code.
memberIds = message.guild.roles.cache.get(camps.CodeCamp.Roles.Bootcamper).members.map(m => m.user.id);
memberIds = message.guild.roles.cache.get(camps.CodeCamp.Roles.Bootcamper).members.map(m => m.user.id);
Camps is a json file
33 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
monbrey
monbrey3y ago
You can use role.members for this, it does this filtering for you, but you first need to fully populate the guild member cache using guild.members.fetch
sproj003 ♿
sproj003 ♿OP3y ago
I don't understand Is guild.members.fetch the whole line of code or is it a variable I then have to keep using? @monbrey
monbrey
monbrey3y ago
You can just call it, await guild.members.fetch() and that will populate guild.members That way when you access role.members it can correctly filter all of them
sproj003 ♿
sproj003 ♿OP3y ago
Thanks When doing this I get this error Cannot read properties of undefined (reading 'members')
monbrey
monbrey3y ago
Oh sorry, probably message.guild in this context
sproj003 ♿
sproj003 ♿OP3y ago
Now I'm just getting Message not defined Here's more of my code
await message.guild.members.fetch()
var memberIds;
var data = [];
// get all members with role 'Bootcamper'
console.log('Getting all members with role \'Bootcamper\'...');
console.log(camps.CodeCamp.Roles.Bootcamper);
memberIds = message.guild.roles.cache.get(camps.CodeCamp.Roles.Bootcamper).members.map(m => m.user.id);
console.log(memberIds);

// send message
await interaction.reply('Roles received.');
await message.guild.members.fetch()
var memberIds;
var data = [];
// get all members with role 'Bootcamper'
console.log('Getting all members with role \'Bootcamper\'...');
console.log(camps.CodeCamp.Roles.Bootcamper);
memberIds = message.guild.roles.cache.get(camps.CodeCamp.Roles.Bootcamper).members.map(m => m.user.id);
console.log(memberIds);

// send message
await interaction.reply('Roles received.');
and my error
Cannot read properties of undefined (reading 'members')
Cannot read properties of undefined (reading 'members')
@monbrey
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
monbrey
monbrey3y ago
Your original example used message so I assumed, my bad
sproj003 ♿
sproj003 ♿OP3y ago
I was trying various methods and none worked. I've sorted part of it but it still isn't recognising the members of the roles
monbrey
monbrey3y ago
This should work if you just swap the two message with interaction
sproj003 ♿
sproj003 ♿OP3y ago
Code
memberIds = interaction.guild.roles.cache.get(camps.CodeCamp.Roles.Bootcamper).members;
memberIds = interaction.guild.roles.cache.get(camps.CodeCamp.Roles.Bootcamper).members;
Error
Cannot read properties of undefined (reading 'members')
Cannot read properties of undefined (reading 'members')
I did
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
sproj003 ♿
sproj003 ♿OP3y ago
From JSON
{
"camps": {
"CodeCamp": {
...
"Roles": {
...
"Bootcamper": integer
}
{
"camps": {
"CodeCamp": {
...
"Roles": {
...
"Bootcamper": integer
}
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
sproj003 ♿
sproj003 ♿OP3y ago
Ah ok Nope, maybe not Same issue
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
sproj003 ♿
sproj003 ♿OP3y ago
it's the role ID. I didn't want to share it so I replaced it with the word when I copied and pasted here. It's now a string with the Role ID
monbrey
monbrey3y ago
it needs to be a string ids arent really sensitive, can you show it to us?
sproj003 ♿
sproj003 ♿OP3y ago
"Bootcamper": "1031273912450953281",
monbrey
monbrey3y ago
That looks correct if it had been broken as a number format it usually ends in 00 How did you get that value?
sproj003 ♿
sproj003 ♿OP3y ago
Copied from Discord
monbrey
monbrey3y ago
Where in Discord, the roles menu?
sproj003 ♿
sproj003 ♿OP3y ago
Right clicked the role in the roles menu
monbrey
monbrey3y ago
Yeah should be fine then
sproj003 ♿
sproj003 ♿OP3y ago
Still not working @monbrey
monbrey
monbrey3y ago
whats the current error? members of undefined? here?
sproj003 ♿
sproj003 ♿OP3y ago
yes
monbrey
monbrey3y ago
Can you show your intents?
sproj003 ♿
sproj003 ♿OP3y ago
Just GuildMembers
monbrey
monbrey3y ago
Add Guilds
sproj003 ♿
sproj003 ♿OP3y ago
Ah finally Thanks so much
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server