#Get all users who have a role

57 messages · Page 1 of 1 (latest)

frank igloo
#

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);
Camps is a json file

hallow ocean
#

• 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.

gentle gazelle
#

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

frank igloo
#

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

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

frank igloo
#

Thanks

frank igloo
gentle gazelle
#

Oh sorry, probably message.guild in this context

frank igloo
#

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

crude dune
#

oh it’s an interaction

#

interaction.guild

gentle gazelle
#

Your original example used message so I assumed, my bad

frank igloo
#

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

gentle gazelle
#

This should work if you just swap the two message with interaction

frank igloo
#

Code memberIds = interaction.guild.roles.cache.get(camps.CodeCamp.Roles.Bootcamper).members;
Error Cannot read properties of undefined (reading 'members')

crude dune
#

what is Roles.Bootcamper?

frank igloo
#

From JSON

    "camps": {
        "CodeCamp": {
            ...
            "Roles": {
...
                "Bootcamper": integer
            }```
crude dune
#

ids are strings

frank igloo
#

Ah

#

ok

#

Nope, maybe not

#

Same issue

crude dune
#

like is it a role id or

frank igloo
#

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

gentle gazelle
#

it needs to be a string

#

ids arent really sensitive, can you show it to us?

frank igloo
#

"Bootcamper": "1031273912450953281",

gentle gazelle
#

That looks correct

#

if it had been broken as a number format it usually ends in 00

#

How did you get that value?

frank igloo
#

Copied from Discord

gentle gazelle
#

Where in Discord, the roles menu?

frank igloo
#

Right clicked the role in the roles menu

gentle gazelle
#

Yeah should be fine then

frank igloo
#

Still not working

frank igloo
#

@gentle gazelle

gentle gazelle
#

whats the current error? members of undefined?

frank igloo
#

yes

gentle gazelle
#

Can you show your intents?

frank igloo
#

Just GuildMembers

gentle gazelle
#

Add Guilds

frank igloo
#

Ah finally

#

Thanks so much

hallow ocean
#

no such role then, log what camps.CodeCamp.Roles.Bootcamper is and make sure it is what you want it to be messages didnt load RIP

crude dune
remote girder
#

damn