DJS Caching Support

So I've been having a few memory issues lately and I've been taking memory snapshots. I saw that some of that data is just taken over by "Stage Channels". I was told about extra caching options but I rather speak to someone here that can assist me more than me having to deal with something I don't know. Can I get rid of stage channels from cache because my bot cannot even join them or is there a reason to keep it? This is what I see in memory snapshots but I don't really know how this stuff works so if someone could let me know on what I could do, it would help a ton Thanks
99 Replies
d.js toolkit
d.js toolkit2y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Mac
MacOP2y ago
If needed, node version is 19.4.0
MrMythical
MrMythical2y ago
Set the makeCache option in the client's options.
d.js docs
d.js docs2y ago
interface ClientOptions Options for a client.
Mac
MacOP2y ago
I saw it but I don't know what the best optimisation were
MrMythical
MrMythical2y ago
You can use cacheWithLimits to limit objects in the cache
Mac
MacOP2y ago
If I don't mind, can you show me how it's done?
MrMythical
MrMythical2y ago
makeCache: Options.cacheWithLimits(...)
d.js docs
d.js docs2y ago
method (static) Options.cacheWithLimits() Create a cache factory using predefined settings to sweep or limit.
Mac
MacOP2y ago
Isn't that bad tho, I don't know what I should cache or what I shouldn't Caching voice channels and stage channels I guess is not needed since my bot does nothing with it I guess? I'll just going as per this image I don't know why djs ends up building on memory but it could be something on my end too
MrMythical
MrMythical2y ago
You could also set up sweepers To filter out voice channels and stage channels
Mac
MacOP2y ago
Would I need voice channels or stage to begin with though? would the text channels inside a voice channel count as a voice channel?
MrMythical
MrMythical2y ago
They would, yes
Mac
MacOP2y ago
because caching stage channels for me has no use as my bot won't ever join them so that's not needed I guess? What happens if commands were ran there but they bot didn't cache that? it would be fetched from the api directly isn't it?
MrMythical
MrMythical2y ago
Depends on what you need to do with the command ig
Mac
MacOP2y ago
Send an image that's about it
MrMythical
MrMythical2y ago
You shouldn't need it cached If it's the interaction reply
Mac
MacOP2y ago
command is ran and images or embeds are sent yes it's an interaction reply nothing to do with managing channels same with users, my whole bot just replies to interactions
MrMythical
MrMythical2y ago
Yeah then it doesn't need to be cached
Mac
MacOP2y ago
So I don't know if I need users cached I need guilds bc of guild count I don't wanna not cache something I should but I also don't wanna cache things I don't need
MrMythical
MrMythical2y ago
Oh wait
Mac
MacOP2y ago
but that's why I wanna speak like this instead of actually doing anything myself and breaking everything
MrMythical
MrMythical2y ago
Actually, you can't even override channel cache I just remembered
Mac
MacOP2y ago
wdym?
MrMythical
MrMythical2y ago
Djs doesn't let you
Mac
MacOP2y ago
I only use guild cache for guild count
MrMythical
MrMythical2y ago
So does your bot do anything that requires a bot user?
Mac
MacOP2y ago
requires a bot user?
MrMythical
MrMythical2y ago
Or is it just interaction replies and follow ups
Mac
MacOP2y ago
pretty much yes or sending messages to webhooks
MrMythical
MrMythical2y ago
hmm I don't think you even need to invite with bot scope That will also prevent any channels or guilds from caching
Mac
MacOP2y ago
oh? What is counted as the bot scope then?
MrMythical
MrMythical2y ago
Well you would need the bot scope for actions that actually need permission Like sending a message or adding a role
Mac
MacOP2y ago
yeah that the bot don't add roles either only replying to interactions
MrMythical
MrMythical2y ago
Does it need to access channels ever?
Mac
MacOP2y ago
Though future plans is to sending messages to channels and update it automatically
MrMythical
MrMythical2y ago
or members, roles, messages?
Mac
MacOP2y ago
so that would require it not really no I mean we need access to member data but that's inside the interaction so technically we don't
MrMythical
MrMythical2y ago
if it's from an interaction option, it's all good yeah
Mac
MacOP2y ago
yeah then no we just do statistics so just send images & embeds no roles or nothing bc atm everything works but I surely have some memory leak
MrMythical
MrMythical2y ago
you may need some refactors
Mac
MacOP2y ago
and djs might be just growing in memory but I doubt since no one seems to mention it
MrMythical
MrMythical2y ago
some djs structures will become API structures
Mac
MacOP2y ago
so I don't know if it's djs but I have snapshots and that image has a large size for "client"
MrMythical
MrMythical2y ago
example GuildMember -> APIGuildMember
Mac
MacOP2y ago
ah is it normal for djs to use that much memory though?
MrMythical
MrMythical2y ago
Idk Depends on your intents and bot size
Mac
MacOP2y ago
very small bot about 2k servers only have guilds intent bc of guilds create and guilds delete
MrMythical
MrMythical2y ago
You need those events?
Mac
MacOP2y ago
Yeah I log guilds created and deleted so I definitely don't wanna give up on that
MrMythical
MrMythical2y ago
Yeah I'm pretty sure the bot would need to be invited with the bot scope for that
Mac
MacOP2y ago
yeah that's fine I don't know what causes the memory overload, do you know what those snapshots mean? from what I can understand, client takes 100mb of memory? the array takes like 77mb but I'm unsure, I've never used that before Any idea on what I could do here? My memory just gradually goes up and it's so hard to understand where the memory leak is coming from Upon looking at memory snapshots, discord.js was just using a ton of it which is why I wanted to reduce it but off topic, any tips to find memory leaks? For a bot with 2k guilds, I'd look at the memory usage to be around 400mb or so however I sit at ~250-300mb upon startup which is fine but then it gradually increases
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
I mean yeah I do see it but then wouldn't it mean that with due time my bot would just crash due to OOM?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
if it doesn’t skyrocket and/or keep going up by large amounts over a long period then you should be fine
Yeah it doesn't go up by large amounts, its a very slow increase if anything yeah
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
I'd say around 2 days to hit 1 gb
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
although even on day 9, it never really hits 1 gb, its just stuck at like 980mb or smth Yeah but caching things that I dont need feels unnecessary as well
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
like why cache stage channels when my bot cannot even join them?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
What is "messages" If a bot doesn't have message content, how does that work via @ mention ?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
nope
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
actually, i think so I have "Guilds" so yeah I guess it would include that
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
That's just for guild create & delete tho yeah pretty much, my bot barely needs anything
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
works fully based on interactions
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
wdym?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
yeah thats just the index.js, i only create the client once
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
when u say odd, is it meant to be higher or lower
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
I don't know It could just not be djs either but memory snapshots legit just show arrays of stage channels
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
and for some reason 2 clients?
Mac
MacOP2y ago
not all 900mb is djs obv I do image stats so we do cache some stuff in memory to load up quicker
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
but thats nothing close to taking up 900mb anyhow + thats all done on startup djs is discord.js... 😅 now idk if this snapshot is accurate because
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
I don't know, not something I can track I used this npm that did profiling every 6 hours but this npm just made my 300mb fly to 900mb in 30s however i didnt care much about that because without any profiling, it still goes up to 900mb over time
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
I just wanted to know what is taking up so much memory or atleast, whats piling up that I should be aware of because if things flow in and out, the memory cannot just gradually increase I don't cache images fully, just backgrounds and thats on startup
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
it doesn't stop but I restart the bot at around 1 gb usage everytime but it has been online for 7d or so and been at around 900+mb without crashing which is not something I mind but more of just whats the cause for it This is a snapshot that was taken like 1 day into the bot being online
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
oh I just hit 2k like today
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
this has been an issue for a few months now but I've never bothered much until now ty :) I think its 2.5k and whats broadcastEvals?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
MacOP2y ago
I mean I don't even mind the usage It's just I've seen other bots with higher guilds use lower memory but then again, diff purpose bots and stuff its not pleasing knowing ur bot's memory just gradually goes up because it removes that peace of mind that I do have to monitor it regardless of the fact that it would auto start, its just not ideal i guess gaining 600mb over time but for what is pretty much what I want to know Right, I'll look into that
Want results from more Discord servers?
Add your server