#Guild Member cache doesn't contain all users

16 messages · Page 1 of 1 (latest)

dry wedge
#

Hey, I'm not too sure why but when I check the member cache of a specific guild, I can only see 1 user (My bot account itself) but not other users.
My guild has two users in it, one is the bot and one is myself

const guild = await client.guilds.cache.get(process.env.DISCORD_GUILD_ID);
    console.log(guild); // Correctly finds guild
    const member = await guild.members.cache.get(discordID);
    console.log(guild.members.cache) // Shows Collection(1) with my bot only

The discord account has access to view the members because I use this same bot account to do this in another script I run.
My intents also match the working bot script I have:

    const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildIntegrations] });
rich crownBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
hexed moat
#

Discord does not guarantee you all members in the initial guild data.
You might (read: probably always) need to fetch them manually, if you care about having them all synced.
You can do so through GuildMemberManager#fetch.

dry wedge
#

If I understand correctly, I can fetch() to get all users, then the cache should be up to date enough that I can trust it to contain all the data present at the time of the fetch?

hexed moat
#

Yeah, it should stay updated, fetching again will short-circuit though (as all members are cached).

dry wedge
#

It throws an error if there's nothing new fetched?

hexed moat
#

No, it will just resolve if all members are fetched.

dry wedge
#

Ah okay

#

Thanks I'll give it a go

hexed moat
#

Looks like the behaviour changed (probably with v14) and it will no longer short-circuit.

dry wedge
#

Hmm now I have a new issue

    const guild = await client.guilds.cache.get(process.env.DISCORD_GUILD_ID);
    await guild.members.fetch()
    const member = await guild.members.cache.get(discordID);
    console.log(guild.members.cache) // Shows Collection(2) with my bot twice

After doing a fetch, my cache contains two users. But both of them are the bot itself and it still doesn't contain my user

hexed moat
#

They should have different keys though, as otherwise you'd only have one member.
This seems odd.

Can you try it in a minimal and clean environment?
So no other dependencies aside from discord.js and just the code to start the bot and fetch members along with logging stuff.

dry wedge
#

Sorry... False alarm 😅

#

I don't even want to explain how I managed to confuse that

#

Looks like it's all working, thanks