Guild Member cache doesn't contain all users

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
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] });
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildIntegrations] });
12 Replies
d.js toolkit
d.js toolkit•10mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - 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!
space
space•10mo ago
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.
GoatSniff
GoatSniff•10mo ago
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?
space
space•10mo ago
Yeah, it should stay updated, fetching again will short-circuit though (as all members are cached).
GoatSniff
GoatSniff•10mo ago
It throws an error if there's nothing new fetched?
space
space•10mo ago
No, it will just resolve if all members are fetched.
GoatSniff
GoatSniff•10mo ago
Ah okay Thanks I'll give it a go
space
space•10mo ago
Looks like the behaviour changed (probably with v14) and it will no longer short-circuit.
GoatSniff
GoatSniff•10mo ago
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
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
space
space•10mo ago
confusedBecel 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.
GoatSniff
GoatSniff•10mo ago
Sorry... False alarm 😅 I don't even want to explain how I managed to confuse that Looks like it's all working, thanks
space
space•10mo ago
That's good to hear