Discord Bot Development with Js!

Divyanshu Parihar
6 min readMay 30, 2021

why should you learn it?

Image from freecodecamp!

As being a Discord bot developer on various platforms like Fiverr, Upwork and even handling my own clients for few years I have seen a surge in discord user base in recent times! It started as a “GAMERHUB” which means it was made for Gamers initially with competition to TeamSpeak and other platforms but the features it provided just outraced these platforms and more and more people started to join Discord! Even I have brought many of my friends to this platform just because of the features it provides. Today as of this posting of this article there are 6.7 M servers on discord with over 250 million registered users and I can tell you this number is only going to increase in future! Many other communities have joined Discord to Like Businesses (not o mention the pandemic had a great impact in that in the past year!). If you are reading this you might be thinking that topic was why should I learn to make a discord bot, So here we go → You need Bot to manage large communities. that's it!

LET’s Start →

Pre-requisites :

Step 1 — Setting Up a Discord Bot

you need a Discord developer account to start making a bot! so if you don’t have one now please make one continue with this tutorial.

when you successfully completer sign up process, you will be redirected to Application Page which just show all your past application.

Application: An Application can be anything that intent to interact with users in any form like using their data to do something in response to that!

so make a new one by clicking on the “ New Application ” Button.

Name your Application (Name it whatever you want to name your bot). Then click “Create”. this will create a new application.

You will be redirected to your application settings. If you don’t change the
“Selected Tab ” to your Applications. and then Navigate to the “Bot ” section of your Application.

Click the Add Bot button to add a bot to the application. Click the Yes, do it! button when it prompts you for confirmation. You will then be on a dashboard containing details of your bot’s name, authentication token, and profile picture.

Note : Never share or upload your bot token as it allows anyone to log in to your bot.

Now you need to create an invite that allows you to add the bot Discord guilds where you can test the bot. First, navigate to the OAuth2 tab of the application dashboard. To create an invite, scroll down and select bot under scopes. You must also set permissions to control what actions your bot can perform in guilds. For this tutorial, select Administrator, which will give your bot permission to perform nearly all actions in guilds. (YOU SHOULD REALLY NOT DO THIS IN PRODUCTION OR FOR CLIENTS)

Copy the link with the Copy button. and got this URL through your browser. If you are logged in you will be able to select the server of yours to which you want to add your bot. Otherwise, you will be prompted to first sign up.

Now click Continue. Ensure you have the tickbox next to Administrator ticked — this will grant the bot administrator permissions. Then click Authorize. Discord will ask you to solve a CAPTCHA before the bot joins the server. You’ll now have the Discord bot on the member's list in the server you added the bot to under offline as we are not running the bot anywhere which is our next step!

You’ve successfully created a Discord bot and added it to a server. Next, you will write a program to log in to the bot.

Step 2 — Creating Your Project: Where fun Begin

In this step, you’ll set up the basic coding environment where you will build your bot and log in to the bot programmatically.

First, you need to set up a project folder and necessary project files for the bot.

Create a project folder and cd into it:

mkdir bot-tutorial && cd bot-tutorial

create a config file to store confidential data like “secret token”:

for Unix System:

nano config.json

for Windows :

echo. > config.json

and remove everything inside that file.

Replace Everything inside that file with this and Add your bot Token from the dashboard. save and exit.

{
“BOT_TOKEN”: “YOUR BOT TOKEN”
}

Start Node project By

npm init -y

create a new index.js file in that folder with

UNIX :

nano index.js

Windows :

echo > index.js

Now install Discord.js Lib by

npm install discord.js

Remove everything inside that file and add these lines :

const Discord = require(“discord.js”);
const config = require(“./config.json”);

const client = new Discord.Client();

const prefix = “!”;

client.on(“message”, function(message) {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;

const commandBody = message.content.slice(prefix.length);
const args = commandBody.split(‘ ‘);
const command = args.shift().toLowerCase();

if (command === “ping”) {
const timeTaken = Date.now() — message.createdTimestamp;
message.reply(`Pong! This message had a latency of ${timeTaken}ms.`);
}

client.login(config.BOT_TOKEN);

now run this command :

node index.js

If you have followed all the steps correctly your bot should be running now!

and will respond to few commands like!

And …

EXPLANATION OF THE SCRIPT :

After importing the necessary config which contains our secret key. we are making a Discord.js Client which is just a Wrapper javascript library for native discord API and helps us use discord API easily and in a more production-ready way!

then we set a prefix value to “!” which mean the command will have to start with this prefix. NOTE THAT THIS FUNCTIONALITY DO NOT COME FOR DISCORD.JS we ourselves are handling this pragmatically in the below lines.

Next is just Event Definition. As discord.js use an event-based system. A callBack is issued when a new event is triggered which in this case “message event”. which is triggered on every message bot can see . and then in the callback function do something with that message. we can just simply do “message. reply(<your message>)” to reply to that message.

then we are logging the client or we can say running the bot with config key!

That's It! I Know this process of handling commands is NOT SCALABLE at all. but there are some other frameworks like Commando and RPC which I will talk about in later articles. So Please Follow on that! which help us easily manage a large project and a large number of commands.

Thanks for Reading! If you have reached here, GOOD JOB!

Please Follow for more article like this, That will be Greatly Appreciated! and help me be motivated.

--

--

Divyanshu Parihar

Full Stack Developer In App , Web and Bot Development