Economy Bot in NodeJS
Easiest way to make a Discord Economy Bot in NodeJS for BeginnersLet’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!)