guild.members.fetch will still fetch those who have left the server

Hey everyone, I am trying to make a simple function that checks if a user is in a server.
export async function userIsInGuild(userId: string, guildId: string) {
try {
const guild = await client.guilds.fetch(guildId)
if (!guild) {
return false
}

const member = await guild.members.fetch(userId)
if (!member) {
return false
}

return true
} catch {
return false
}
}
export async function userIsInGuild(userId: string, guildId: string) {
try {
const guild = await client.guilds.fetch(guildId)
if (!guild) {
return false
}

const member = await guild.members.fetch(userId)
if (!member) {
return false
}

return true
} catch {
return false
}
}
The problem is that when a user leaves the server, guild.members.fetch does not seem to be up to date. It will return a member as if they were still in the server. But it does work fine the other way around, if the client is already initialized and a player joins the server, it will be up to date with the fact that they joined. It just doesn't seem to work someone leaves the server. It will throw an error however if the client was initialized while the user was not in the server, which is good. It just can't tell who leaves the server during runtime. Is this normal behavior? Is there a way to make it so guild.members.fetch will always be accurate?
9 Replies
d.js toolkit
d.js toolkit6mo 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! - Marked as resolved by OP
Kevinnnn
Kevinnnn6mo ago
What intents and partials do you have enabled?
trevdev
trevdevOP6mo ago
const client: ExtendedClient = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] })
const client: ExtendedClient = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] })
hello, here are my intents, i do not know what partials means
Kevinnnn
Kevinnnn6mo ago
ah, you don't have the GuildMembers intent you won't receive guild member events, hence why your members cache is out of date I don't remember the behavior of fetch() without the GuildMembers intent, but I'm pretty sure that method won't work unless you have the intent enabled
trevdev
trevdevOP6mo ago
thank you, i will test it out when i can. its a bit weird, because it actually seems to work at least a little bit without the guild members intent
Kevinnnn
Kevinnnn6mo ago
You're able to fetch members individually, and any event you receive that has a member object will update the guild member in cache most notably, your GuildMessages intent is letting you receive message create events, which contains a partial member object
trevdev
trevdevOP6mo ago
thanks you're a big help @Qjuh can you tell me how to do that?
d.js docs
d.js docs6mo ago
:method: GuildMemberManager#fetch @14.15.3 Fetches member(s) from a guild.
// Fetch all members from a guild
guild.members.fetch()
.then(console.log)
.catch(console.error);
// Fetch all members from a guild
guild.members.fetch()
.then(console.log)
.catch(console.error);
Amgelo
Amgelo6mo ago
you can also just add the intent and your cache won't be unreliable anymore at least not in terms of members leaving/joining oh, forgot it was privileged
Want results from more Discord servers?
Add your server