Building an Efficient Bot: Task Automation and Resource Optimizatione
I would like to create a bot that needs to perform a task job every 5 minutes.
Here's what the task do:
1. Retrieving all the guilds from my database and obtaining the value of the variable: lastMessageDate (MongoDB).
2. Iterating through all the fetched guilds:
- Performing the necessary actions with the lastMessageDate obtained for each guild.
So every 5min the bot will send a request to mongodb and retreive the lastMessageDate for each guild.
When a message is sent, the bot will (With MessageCreated event):
1. Identify the guild to which the message belongs.
2. Search for this guild in my database.
3. Update the value of the variable lastMessageDate for this guild in the database.
(More details: In fact, my bot is a minigame bot with a lot of minigames and cultural question games. Every 5 minutes, I want to retrieve the lastMessageDate to then check if the guild is quite active. If the guild is active, I would like to generate a number, and if the number falls within a certain range, I want to start a minigame for this guild.) Initially, I had thought about doing this directly within the MessageCreated event, but I realized that it might be even more costly to do this every time a message is created, especially if the bot is on a large number of servers and those servers generate a significant amount of messages.
I'd like to know if this will require significant resources. If yes, how can I optimize it to minimize resource usage? or maybe you guys have a better idear of how to do this
2 Replies
- 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!Indeed, you've just made me realize that in any case, the MessageCreate event will be emitted
I'm going to opt for a cache and store all the messages in it. Then, I'll keep the task that executes every 5 minutes to go through the cache and do what i need
Thank you ! 🙂
So a cache, something like this ?:
How mb i found channel.messages.cache.get, thx