How does CachedManager work?
How does CachedManager<Snowflake, GuildMember, GuildMemberResolvable> (specifically guild.members.cache) work in discord.js v14?
When and how are members added to the cache?
Is it only populated after explicitly calling guild.members.fetch() or guild.members.list()?
Does it persist for the lifetime of the bot client, or is it tied to individual events like receiving an interaction or message?
I'm trying to understand the caching behavior and in what situations I can rely on guild.members.cache.get(id) returning a result.
Thanks!
14 Replies
- 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!You should never rely on it returning a result. All members should be fetched as they arent guaranteed to be cached. Guilds, channels, guild emotes, roles and permissionOverwrites are always cached (if you have the Guilds intent)
I understand. But I do not know how they are cached.
If I do
instead of
then in the first case the data is taken directly from discord without cache? Or not?
you can pass the id into the fetch directly
Fetching a single member checks cache first
i see it)
thx a lot

Does the cache live while the Client object lives?
what do you mean?
If you mean if cache gets cleared if you restart, yes
For example, if an Interaction event occurred with a Slash command being called or a button was pressed, where does the guild in interaction come from? Is this the new GuildManager, with new сache, or is it taken from the one saved in the Client.guilds?
It's is taken from client.guilds.cache
If you are curious about these stuff, you can always check the source code
A strange answer. In other words, I will never need to ask questions here at all, and there will be no point in talking to support. I ask a question expecting to get a relatively quick answer from someone who already knows the answer, so as not to waste time on a long study. Thanks
Sorry about this text. I think it's a little rude.
I'm not sure how my answer is strange. I pointed out where you can get your answers in the future directly instead of waiting on someone who may or may not know the answer. But ok
I know where to get the answer, but thanks.
Above is my screenshot from discord.js repository that I'm viewing)
But the question remains.
For example, if an Interaction event occurred with a Slash command being called or a button was pressed, where does the guild in interaction come from? Is this the new GuildManager, with new сache, or is it taken from the one saved in the Client.guilds?
To answer your question, no it's not a new manager, all guilds are cached with guilds intent in the GuildManager (which is instantiated with the Client constructor) and guilds are taken from this manger upon interaction
There's only one GuildManager, which is
client.guilds
Thanks a lot for you))
You confirmed my guess.
❤️