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 User2y ago
Message Not Public
Sign In & Join Server To View
monbrey
monbrey2y 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 ♿2y 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
monbrey2y 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 ♿2y ago
Thanks When doing this I get this error Cannot read properties of undefined (reading 'members')
monbrey
monbrey2y ago
Oh sorry, probably message.guild in this context
sproj003 ♿
sproj003 ♿2y 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 User2y ago
Message Not Public
Sign In & Join Server To View
monbrey
monbrey2y ago
Your original example used message so I assumed, my bad
sproj003 ♿
sproj003 ♿2y 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
monbrey2y ago
This should work if you just swap the two message with interaction
sproj003 ♿
sproj003 ♿2y 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 User2y ago
Message Not Public
Sign In & Join Server To View
sproj003 ♿
sproj003 ♿2y ago
From JSON
{
"camps": {
"CodeCamp": {
...
"Roles": {
...
"Bootcamper": integer
}
{
"camps": {
"CodeCamp": {
...
"Roles": {
...
"Bootcamper": integer
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
sproj003 ♿
sproj003 ♿2y ago
Ah ok Nope, maybe not Same issue
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
sproj003 ♿
sproj003 ♿2y 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
monbrey2y ago
it needs to be a string ids arent really sensitive, can you show it to us?
sproj003 ♿
sproj003 ♿2y ago
"Bootcamper": "1031273912450953281",
monbrey
monbrey2y 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 ♿2y ago
Copied from Discord
monbrey
monbrey2y ago
Where in Discord, the roles menu?
sproj003 ♿
sproj003 ♿2y ago
Right clicked the role in the roles menu
monbrey
monbrey2y ago
Yeah should be fine then
sproj003 ♿
sproj003 ♿2y ago
Still not working @monbrey
monbrey
monbrey2y ago
whats the current error? members of undefined? here?
sproj003 ♿
sproj003 ♿2y ago
yes
monbrey
monbrey2y ago
Can you show your intents?
sproj003 ♿
sproj003 ♿2y ago
Just GuildMembers
monbrey
monbrey2y ago
Add Guilds
sproj003 ♿
sproj003 ♿2y ago
Ah finally Thanks so much
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View