Avoid commands being sent at the same time

Is there any way to avoid application commands being sent at the same time? For example, when two different users use two application commands at the same time (or almost the same time, around ~1 second), only one will activate the command successfully, the other's command will not be activate. Here are some main scripts of my bot and two commands for illustration. I used the guide from https://discordjs.guide/ for most of the scripts. index.js: https://srcb.in/e254B2JQb5 ready.js: https://srcb.in/XD5GVVRDUp interactionCreate.js: https://srcb.in/jtu3GnanzQ Commands: https://srcb.in/Qoa2iQU3mh https://srcb.in/BVrRxTrLP2
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
12 Replies
d.js toolkit
d.js toolkit2mo 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! - Marked as resolved by OP
Булочка | pasha_boez
Hey. You can use queue packages like "promise-queue" or "@sapphire/async-queue" Check for queue length and stop unwanted executions @ntb896
Svitkona
Svitkona2mo ago
looking at your commands, i don't see why there'd be an issue if two users tried to use the command at the same time what error are you getting?
ntb896
ntb8962mo ago
well the .json files sometimes don't update the value, idk if it's because of users in my server sending commands too fast or not, but the issue happened when they did that so I think that's why; it happened with commands like coinflip (https://sourceb.in/RzE88GB5sS) or commands that update the value in the .json file maybe there are other solutions, but for now this is the only one I came up with :blob_thanks:
Svitkona
Svitkona2mo ago
oh, i see, you're writing back to the files... you should use a database for this
ntb896
ntb8962mo ago
yeah I tried using MongoDB, I followed some guides online but I failed by the way, where should I put these in the code? in the commands script or the index.js or anywhere else?
Булочка | pasha_boez
I think the best option would be to create a queue in another file like utils.js and work with them only in the required commands by adding the request to the queue before requesting the file and deleting after request is over.
Amgelo
Amgelo2mo ago
I think you should really consider using a database that will manage isolation for you reading/writing from/to IO can take some time, and if you don't reply to the command interaction in 3 seconds it'll expire
𝒄𝒐𝒄𝒐𝒌𝒊𝒕𝒕𝒆𝒏
I don't think limiting the command execution is the right way to go about that.. for simplicity, on a smaller bot, I think a json database is just fine, but 1. don't read and write synchronously, 2. maybe load your databases once instead of re-reading the same file every time.. there's no need if you just keep the object in memory. then for writing, implement an asynchronous debounced write function that u call every time u update anything in ur database.
𝒄𝒐𝒄𝒐𝒌𝒊𝒕𝒕𝒆𝒏
if ur bot is bigger or the data is important, keyv might be a good database solution if u didn't like mongodb https://www.npmjs.com/package/keyv
npm
keyv
Simple key-value storage with support for multiple backends. Latest version: 4.5.4, last published: 9 months ago. Start using keyv in your project by running npm i keyv. There are 1426 other projects in the npm registry using keyv.
𝒄𝒐𝒄𝒐𝒌𝒊𝒕𝒕𝒆𝒏
its simple... and usually enough for a private bot with not really critical data they're just a lot more work in handling them correctly,.. vs just an object storage not that it's not the better solution, just like, at least personally if it's a smaller project anyway I wanna keep the workload low too
ntb896
ntb8962mo ago
I wonder which one is easier and better to use, sqlite or keyv lol
Want results from more Discord servers?
Add your server