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 toolkit15mo 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
Mac15mo ago
If needed, node version is 19.4.0
MrMythical
MrMythical15mo ago
Set the makeCache option in the client's options.
d.js docs
d.js docs15mo ago
interface ClientOptions Options for a client.
Mac
Mac15mo ago
I saw it but I don't know what the best optimisation were
MrMythical
MrMythical15mo ago
You can use cacheWithLimits to limit objects in the cache
Mac
Mac15mo ago
If I don't mind, can you show me how it's done?
MrMythical
MrMythical15mo ago
makeCache: Options.cacheWithLimits(...)
d.js docs
d.js docs15mo ago
method (static) Options.cacheWithLimits() Create a cache factory using predefined settings to sweep or limit.
Mac
Mac15mo 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
MrMythical14mo ago
You could also set up sweepers To filter out voice channels and stage channels
Mac
Mac14mo 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
MrMythical14mo ago
They would, yes
Mac
Mac14mo 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
MrMythical14mo ago
Depends on what you need to do with the command ig
Mac
Mac14mo ago
Send an image that's about it
MrMythical
MrMythical14mo ago
You shouldn't need it cached If it's the interaction reply
Mac
Mac14mo 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
MrMythical14mo ago
Yeah then it doesn't need to be cached
Mac
Mac14mo 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
MrMythical14mo ago
Oh wait
Mac
Mac14mo ago
but that's why I wanna speak like this instead of actually doing anything myself and breaking everything
MrMythical
MrMythical14mo ago
Actually, you can't even override channel cache I just remembered
Mac
Mac14mo ago
wdym?
MrMythical
MrMythical14mo ago
Djs doesn't let you
Mac
Mac14mo ago
I only use guild cache for guild count
MrMythical
MrMythical14mo ago
So does your bot do anything that requires a bot user?
Mac
Mac14mo ago
requires a bot user?
MrMythical
MrMythical14mo ago
Or is it just interaction replies and follow ups
Mac
Mac14mo ago
pretty much yes or sending messages to webhooks
MrMythical
MrMythical14mo 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
Mac14mo ago
oh? What is counted as the bot scope then?
MrMythical
MrMythical14mo ago
Well you would need the bot scope for actions that actually need permission Like sending a message or adding a role
Mac
Mac14mo ago
yeah that the bot don't add roles either only replying to interactions
MrMythical
MrMythical14mo ago
Does it need to access channels ever?
Mac
Mac14mo ago
Though future plans is to sending messages to channels and update it automatically
MrMythical
MrMythical14mo ago
or members, roles, messages?
Mac
Mac14mo 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
MrMythical14mo ago
if it's from an interaction option, it's all good yeah
Mac
Mac14mo 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
MrMythical14mo ago
you may need some refactors
Mac
Mac14mo ago
and djs might be just growing in memory but I doubt since no one seems to mention it
MrMythical
MrMythical14mo ago
some djs structures will become API structures
Mac
Mac14mo ago
so I don't know if it's djs but I have snapshots and that image has a large size for "client"
MrMythical
MrMythical14mo ago
example GuildMember -> APIGuildMember
Mac
Mac14mo ago
ah is it normal for djs to use that much memory though?
MrMythical
MrMythical14mo ago
Idk Depends on your intents and bot size
Mac
Mac14mo ago
very small bot about 2k servers only have guilds intent bc of guilds create and guilds delete
MrMythical
MrMythical14mo ago
You need those events?
Mac
Mac14mo ago
Yeah I log guilds created and deleted so I definitely don't wanna give up on that
MrMythical
MrMythical14mo ago
Yeah I'm pretty sure the bot would need to be invited with the bot scope for that
Mac
Mac14mo 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 User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo 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 User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo 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 User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo ago
I'd say around 2 days to hit 1 gb
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo 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 User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo ago
like why cache stage channels when my bot cannot even join them?
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo ago
What is "messages" If a bot doesn't have message content, how does that work via @ mention ?
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo ago
nope
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo ago
actually, i think so I have "Guilds" so yeah I guess it would include that
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo ago
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo ago
That's just for guild create & delete tho yeah pretty much, my bot barely needs anything
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo ago
works fully based on interactions
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo ago
wdym?
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo ago
yeah thats just the index.js, i only create the client once
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo ago
when u say odd, is it meant to be higher or lower
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo 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 User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo ago
and for some reason 2 clients?
Mac
Mac14mo 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 User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo 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 User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo 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 User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo 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 User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo 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 User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo ago
oh I just hit 2k like today
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo 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 User14mo ago
Message Not Public
Sign In & Join Server To View
Mac
Mac14mo 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