how does discordjs populate cache

like im trying to implement minimal cache using core libraries, wondering how does main lib populate all guilds/channels/roles on gateway connect? does it idk what, and is in any way request guild members used or such?
19 Replies
d.js toolkit
d.js toolkit4w ago
Samtino
Samtino4w ago
DiscordJS (main lib) caches all Guilds, Channels, and Roles on startup by default and keeps them up to date with the Gateway Events and then keeps it updated with the Guilds intent (if you have that defined on your Client) You can read about customizing that cache here: https://discordjs.guide/miscellaneous/cache-customization.html#limiting-caches
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
Whimsy
WhimsyOP4w ago
im quite sure that has 0 things to do with what i asked about, sorry
Samtino
Samtino4w ago
Then you just want to be a little more specific on what you're asking. Are you wondering how exactly it makes it's cache? Where it intercepts gateway events to keep it updated? I don't really understand your question...but it might be something that needs to wait for a maintainer to answer or look at the source code yourself
Whimsy
WhimsyOP4w ago
more about Where it intercepts gateway events to keep it updated? or instead of "where", which for which cache type
TÆMBØØ
TÆMBØØ4w ago
Discord sends GUILD_CREATE events when your client is identifying. d.js then parses the data in these events and populates the respective caches. The data includes the available guilds the app is in, the channels in that guild (excluding threads), roles, the client itself as a guild member, etc.
Whimsy
WhimsyOP4w ago
how does djs differentiate from guild create of existing guilds and one that it re-emits?
TÆMBØØ
TÆMBØØ4w ago
Because of the READY event that Discord sends after all of the guilds
Whimsy
WhimsyOP4w ago
oh that makes sense
TÆMBØØ
TÆMBØØ4w ago
If d.js receives that event, then any subsequent GUILD_CREATE events must be actual guild creations/guilds the app is added to
Whimsy
WhimsyOP4w ago
i know this is more of a docs question, but i see that there are unavailable guild ids in ready event data, what happens when it becomes available? does gateway send anything or, i dont see "available" as prop on GUILD_UPDATE
TÆMBØØ
TÆMBØØ4w ago
You get a GUILD_CREATE event for that now-available guild
Whimsy
WhimsyOP4w ago
..with unavailable set to true, right..? to know it isnt just regular join oh to false, and when its regular its not even provided, because that's an "extra" field, yes?
TÆMBØØ
TÆMBØØ4w ago
d.js will emit its own custom guildAvailable event if the raw GUILD_CREATE was for a now-available guild. Else it will emit the more standard guildCreate event https://github.com/discordjs/discord.js/tree/14.22.1/packages/discord.js/src/client/websocket/handlers/GUILD_CREATE.js#L13
Whimsy
WhimsyOP4w ago
well yeah but im trying to make it work using only @discordjs packages so there's no that oh that code ref helps
TÆMBØØ
TÆMBØØ4w ago
Then you’d need to store those unavailable guild IDs somewhere and when you get a GUILD_CREATE event, check whether that guild’s ID is in the list of unavailable IDs. If it’s not in it, then it must be a new guild your app has joined
Whimsy
WhimsyOP4w ago
got it, and just one last, does discord.js anywhere use requestGuildMembers for gateway on its own, without user doing anything or that triggers only on .fetch or such
TÆMBØØ
TÆMBØØ4w ago
Only triggered by the developer explicitly fetching members
Whimsy
WhimsyOP4w ago
ah hey quick q, how does discord.js figure out bot's highest role from gateway stuff? like i see its always cached but cant find how it does that huh where? only place i see is members array? GatewayGuildCreateDispatchData type and discord docs for guild create event yes but client data? so i said correctly? it will be in members arrary? always? got it thanks, you never know ngl my brain skipped your bot part, "will return users in voice" is what i sow oops i realize there is no ping property at all anywhere, nor access to heartbeat stuff to calculate one oh thanks

Did you find this page helpful?