Optimizing Ram Usage

okay so i just launched a bot which is currently in about 50 servers but it is already taking up a LOT of ram, about 600MB. Is there anything i can do to optimize this? My bot is a gamebot with features like turn based battle, inventory, economy etc. It also has a questing system that basically uses an if-else ladder to send a response based on what part of quest the user is at. It uses a lot of componentslike buttons and select menus for many of it's commands but i always ensure to stop the collectors after use and im certain there is no memory leak. I also have a function running in background in my main script to give energy to all users every 20 minutes.
5 Replies
d.js toolkit
d.js toolkit•2w 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!
Dante
Dante•2w ago
setInterval(give_energy,1000*1200);

new Bot({ intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageReactions],

partials: [Partials.Channel],

sweepers: {
messages: {
// check every 1 minutes
interval: 1 * 60,
// delete messages older than 10 minutes
filter: () => msg => Date.now() - msg.createdTimestamp > 1 * 1_000,
},
threads: {
// check every 1 minutes
interval: 1 * 60,
// delete archived threads
filter: () => thread => thread.archived,
},
reactions: {
// check every 5 minutes
interval: 1 * 60,
// delete reactions older than 10 minutes
filter: () => reaction => reaction.message.createdTimestamp > 1 * 1_000,
},
users:{
interval: 1*60,
filter: () => msg => Date.now() - msg.createdTimestamp > 1 * 1_000,
}
},
makeCache: Options.cacheWithLimits({
...Options.DefaultMakeCacheSettings,
// store 20 messages per channel
MessageManager: 0,
GuildMemberManager: 0,
GuildInviteManager: 0,
VoiceStateManager: 0,
ReactionManager: 0,
GuildEmojiManager: 0,
ReactionUserManager: 0,
}),





}).run()

async function give_energy(){
await profileModel.updateMany({energy:{$lt:25}},{$inc:{energy:1}})
}
setInterval(give_energy,1000*1200);

new Bot({ intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageReactions],

partials: [Partials.Channel],

sweepers: {
messages: {
// check every 1 minutes
interval: 1 * 60,
// delete messages older than 10 minutes
filter: () => msg => Date.now() - msg.createdTimestamp > 1 * 1_000,
},
threads: {
// check every 1 minutes
interval: 1 * 60,
// delete archived threads
filter: () => thread => thread.archived,
},
reactions: {
// check every 5 minutes
interval: 1 * 60,
// delete reactions older than 10 minutes
filter: () => reaction => reaction.message.createdTimestamp > 1 * 1_000,
},
users:{
interval: 1*60,
filter: () => msg => Date.now() - msg.createdTimestamp > 1 * 1_000,
}
},
makeCache: Options.cacheWithLimits({
...Options.DefaultMakeCacheSettings,
// store 20 messages per channel
MessageManager: 0,
GuildMemberManager: 0,
GuildInviteManager: 0,
VoiceStateManager: 0,
ReactionManager: 0,
GuildEmojiManager: 0,
ReactionUserManager: 0,
}),





}).run()

async function give_energy(){
await profileModel.updateMany({energy:{$lt:25}},{$inc:{energy:1}})
}
here is my cache manager i dont need to cache any message because the bot doesn't really need to use older messages, it just needs the current interaction can you recommend a good cache manager then? and tbh i recently changed it to this my initial caching was something like this:-
import { Client, GatewayIntentBits, PartialDMChannel,Options, Partials } from 'discord.js'
import { Bot } from './src/bot'
import profileModel from './models/profileSchema'



setInterval(give_energy,1000*1200);

new Bot({ intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageReactions],

partials: [Partials.Channel],

sweepers: {
messages: {
// check every 5 minutes
interval: 5 * 60,
// delete messages older than 10 minutes
filter: () => msg => Date.now() - msg.createdTimestamp > 10 * 60 * 1_000,
},
threads: {
// check every 5 minutes
interval: 5 * 60,
// delete archived threads
filter: () => thread => thread.archived,
},
reactions: {
// check every 5 minutes
interval: 5 * 60,
// delete reactions older than 10 minutes
filter: () => reaction => reaction.message.createdTimestamp > 10 * 60 * 1_000,
},
},
makeCache: Options.cacheWithLimits({
...Options.DefaultMakeCacheSettings,
// store 20 messages per channel
MessageManager: 20,
GuildMemberManager: 20,
GuildInviteManager: 10,
VoiceStateManager: 10,
ReactionManager: 0,
GuildEmojiManager: 10,
ReactionUserManager: 0,
}),





}).run()

async function give_energy(){
await profileModel.updateMany({energy:{$lt:25}},{$inc:{energy:1}})
}
import { Client, GatewayIntentBits, PartialDMChannel,Options, Partials } from 'discord.js'
import { Bot } from './src/bot'
import profileModel from './models/profileSchema'



setInterval(give_energy,1000*1200);

new Bot({ intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageReactions],

partials: [Partials.Channel],

sweepers: {
messages: {
// check every 5 minutes
interval: 5 * 60,
// delete messages older than 10 minutes
filter: () => msg => Date.now() - msg.createdTimestamp > 10 * 60 * 1_000,
},
threads: {
// check every 5 minutes
interval: 5 * 60,
// delete archived threads
filter: () => thread => thread.archived,
},
reactions: {
// check every 5 minutes
interval: 5 * 60,
// delete reactions older than 10 minutes
filter: () => reaction => reaction.message.createdTimestamp > 10 * 60 * 1_000,
},
},
makeCache: Options.cacheWithLimits({
...Options.DefaultMakeCacheSettings,
// store 20 messages per channel
MessageManager: 20,
GuildMemberManager: 20,
GuildInviteManager: 10,
VoiceStateManager: 10,
ReactionManager: 0,
GuildEmojiManager: 10,
ReactionUserManager: 0,
}),





}).run()

async function give_energy(){
await profileModel.updateMany({energy:{$lt:25}},{$inc:{energy:1}})
}
there was really no effect on RAM should i just remove the sweepers and turn off caching? so since there is nothing to cache, there is no need to sweep do you think this will help in a significant way? does any other reason come to your mind that is causing this ram inflation Also, if I remove the sweepers and still cache messages and stuff how will the cache clean itself? Yeah my bot really does a lot, tbh I'm not that concerned about the ram currently, what I really am concerned about is that it's this much now when it's only in 50 servers and is not very active rn. How much do you think the RAM will increase to in let's say 50k servers and a lot of users? I really have no idea since this is my first time developing a discord bot I know there is sharding to tackle load but do you think it will still be enough? Just genuinely want to know Sorry I didn't get you So judging on the current scenario, how much would you expect my RAM to hit in the future when the bot gets a lot more active?
AnthonyVault
AnthonyVault•2w ago
You're asking abstract questions that can't be answered.
Dante
Dante•2w ago
Sorry about that, how can I frame my question better? Actually the thing is, I have never released a bot nor hosted one before this so I don't even have a general idea about the stats. So I thought it would be possible to estimate ram usage based on current stats since it's linearly proportional I believe 😅
AnthonyVault
AnthonyVault•2w ago
I have a bot 10k, total ram usage is ~4GB with cached DB. Total DB size is 212MB. Without caching DB - minus ~1GB