Easiest Way To Make a Discord Economy Bot In NodeJS (for Beginners)

Economy Bot in NodeJS

Easiest way to make a Discord Economy Bot in NodeJS for Beginners

Let’s start in our index.js file. Let’s import DiscordJS, and Quick.DB which we’re going to be using for storing our balance data.

const Discord = require("discord.js");
const client = new Discord.Client();
const db = require('quick.db');

Now, let’s log into our bot. If you haven’t already made your application, go ahead. discord.com/developers (If you don’t know how to create an application, you’re screwed.)
Put you bot’s token into the ENV, as a variable called “TOKEN”.

client.login(TOKEN);

Now, it should look like this:

const Discord = require("discord.js");
const client = new Discord.Client();
const db = require('quick.db');

client.login(TOKEN);

Let’s configure the DB now. This code will create a variable called “bal” in our DB system. We will be using this to store the data for our users’ money.

client.on("ready", () => {
  console.log(`Logged in as ${client.user.tag}!`)
  db.set('userInfo', { bal: 0 })
})

Now, your bot is set up, but it doesn’t have any commands yet… let’s fix that!

client.on("message", msg => {
  if (msg.content === "!bal") {
     msg.channel.send('Your current balance is ' + db.get(msg.author.id + '.bal') + '.');
  }

This will return the user’s balance. But, currently, it returns NULL. This is because the user’s balance has not been set yet! Let’s fix that with this quick code:

client.on("message", msg => {
  if (msg.content === "!bal") {
    if (typeof(db.get(msg.author.id + '.bal')) === 'number') {
     msg.channel.send('Your current balance is ' + db.get(msg.author.id + '.bal') + '.');
    }else{
     db.add(msg.author.id + '.bal', 0);
     msg.channel.send('Your current balance is 0.');
    }
  }

What this code does, is, if the user’s balance is NULL, then it would set a value of “0” to our user’s balance.

Now, you can make lots of commands! To add money to a user’s balance, use this:

db.add(msg.author.id + '.bal', 10);

And you can set a user’s balance using this:

db.set(msg.author.id, { bal: 10 })

So that’s all that you need to know about making a basic economy bot in NodeJS. Thanks for reading! (Please like this topic if you find it helpful, too!)

5 Likes

Very useful!

2 Likes

Thank you!

It is nice to post example such as this. I hope you don’t mind but I’ll nitpick the code a bit because I see this done fairly often. Notice that you have done a db.get in order to test the typeof and then when it was a number and you had the balance you fetch it again. Perhaps assigning the result of your db.get to a const and testing that would work better. You have the value to return without fetching again.

I would also (if it was me) db.add as you have done but then fetch it using db.get rather than returning a string constant. You have 2 msg.channel.send lines that have identical structure. So if it is not a number, add it, fetch it and use the single msg.channel.send for both cases.

That’s it for me :slight_smile:

3 Likes

Hi. Just a question. How can I keep track of more things, specifically a time / date? I want to make a !work cmd and it can only happen every 10 minutes. Any suggestions?

Hi, please consider starting your own thread instead of bumping an 8 month thread that is somewhat related.