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
d.js toolkit
d.js toolkit3d 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!
treble/luna
treble/luna3d ago
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)
ReactiveTurtle🌸🥀
I understand. But I do not know how they are cached. If I do
(await guild.members.fetch()).get(memberId)
(await guild.members.fetch()).get(memberId)
instead of
guild.members.cache.get(memberId)
guild.members.cache.get(memberId)
then in the first case the data is taken directly from discord without cache? Or not?
treble/luna
treble/luna3d ago
you can pass the id into the fetch directly Fetching a single member checks cache first
ReactiveTurtle🌸🥀
i see it) thx a lot
No description
ReactiveTurtle🌸🥀
Does the cache live while the Client object lives?
treble/luna
treble/luna3d ago
what do you mean? If you mean if cache gets cleared if you restart, yes
ReactiveTurtle🌸🥀
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?
NyR
NyR3d ago
It's is taken from client.guilds.cache If you are curious about these stuff, you can always check the source code
ReactiveTurtle🌸🥀
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.
NyR
NyR3d ago
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
ReactiveTurtle🌸🥀
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?
NyR
NyR3d ago
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
ReactiveTurtle🌸🥀
Thanks a lot for you)) You confirmed my guess. ❤️

Did you find this page helpful?