#Get all users who have a role
57 messages · Page 1 of 1 (latest)
• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.
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
I don't understand
Is guild.members.fetch the whole line of code or is it a variable I then have to keep using?
@gentle gazelle
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
Thanks
When doing this I get this error Cannot read properties of undefined (reading 'members')
Oh sorry, probably message.guild in this context
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.');``` and my error ```Cannot read properties of undefined (reading 'members')```
@gentle gazelle
Your original example used message so I assumed, my bad
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
This should work if you just swap the two message with interaction
Code memberIds = interaction.guild.roles.cache.get(camps.CodeCamp.Roles.Bootcamper).members;
Error Cannot read properties of undefined (reading 'members')
I did
didn’t find a role with that key
what is Roles.Bootcamper?
From JSON
"camps": {
"CodeCamp": {
...
"Roles": {
...
"Bootcamper": integer
}```
ids are strings
is the value “integer” or something else?
like is it a role id or
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
"Bootcamper": "1031273912450953281",
That looks correct
if it had been broken as a number format it usually ends in 00
How did you get that value?
Copied from Discord
Where in Discord, the roles menu?
Right clicked the role in the roles menu
Yeah should be fine then
Still not working
@gentle gazelle
whats the current error? members of undefined?
here?
yes
Can you show your intents?
Just GuildMembers
Add Guilds
no such role then, log what messages didnt load camps.CodeCamp.Roles.Bootcamper is and make sure it is what you want it to be
guild.members.cache.fetch
yea this isn’t a thing
damn