How to create a counter that increases with each slash command execution in Discord bot?

I’m building a Discord bot using commandbot and I’m pretty new to programming. I want to make a /track command that counts how many times people drop food on the floor. Every time someone uses /track, the bot should reply with something like “Food dropped 3 times” where the number goes up by 1 each time.

I found some sample code online and tried copying it but I’m not sure what parts I need to modify to make it work the way I want. Can someone help me understand how to set up this kind of incrementing counter system?

u could also just use a simple var in ur bot code. set up a global counter like let dropCount = 0; at the top, then add dropCount++ inside ur slash cmd handler & return the updated number. way easier for beginners, but it’ll reset every time the bot restarts.

Try SQLite - it’s simple but keeps your data safe. Create a table with one row for your counter, then use UPDATE commands to bump it up each time someone runs the slash command. Your data survives bot crashes and restarts, unlike variables that just disappear. SQLite’s lightweight and doesn’t need a separate database server, so it’s perfect for Discord bots. Setup looks scary at first, but there’s tons of tutorials out there. Once you’ve got it running, you’ll have a solid base for adding more features.

To create a /track command that counts how many times food is dropped, you can use a simple system involving a JSON file named counter.json. Initialize this file with { "count": 0 }. In your command handler, read the count from this file, increment it by one each time the command is executed, and then write the updated count back to the file before sending a response like “Food dropped X times”. Make sure to include error handling in case the JSON file is missing, as this approach is best for smaller projects; larger applications would benefit from a more robust database solution.