Discord.js an introduction

Adonis
3 min readDec 10, 2020

--

Most of you nerds must have heard or use discord for sure! Discord is not only an abode for those in need of friends but also for awesome coders, with there spectacular developer portal solely made for coders. Discord has in the few years of its making have made some of these awesome modules for awesome bot developers! Main of those are discord.js, discord.py and JDA. And obviously by their extensions you must be knowing what they mean, discord.js for javascript, py for python and JDA for Java Development API pretty reasonable stuff ain’t it?

Lets go to what we came here for Discord.js. But before we take off let me just recommend you a thing, learn javascript before you attempt to make a bot with discord.js, and to be true an object oriented background of programming is necessary for better understanding as discord.js has some object oriented paradigms which you will see in the next couple of blogs. So a basic understanding at least an ametureish knowledge is necessary in both contexts.

To start off we’ll have to go to the discord developers portal and pretty much self explanatory is that we will have to Click on New application>name the application> click create>go over to “bot” in the menu at the left and create a new bot, give the bot a catchy pfp and I guess you’re done! Sorry I just guessed it! You aren’t. Go over to the OAuth2 section and then:

Just do what I did, and copy the link over there, and paste into a new page, do all it says and yes you just succeeded in bringing your bot into your server!

Lets get over now, to the main part. Coding.

npm init -y

This uploads a new file called package.json into the bot folder.

npm install

This installs all the node modules necessary for running the bot, with discord.js.

npm i discord.js

This is to download discord.js!

const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {console.log('Ready To Rumble');})client.on('message', message => {if(message.content === '!ping'){
channel.message.send(`Pong ${Date.now() - message.createdTimestamp}ms.`);
}})
client.login('YOUR_BOT_TOKEN');

Remember to put your bot token, into the client.login() area, and remember not to share the token with anyone else except your colleagues helping you in the project. If by chance the token leaks, you can easily regenerate it.

Where is the BOT TOKEN?

In the bot section of the dev portal

UNDERSTANDING

In this very basic code, there are two types of handlers, one is the on ready event handler while the other is the on message event handler. The on ready handler just specifies when the bot files have been read and have been compiled. The on message specifies when the bot detects a message from the guild and tries to match it with the !ping command, which is done with the help of the if statement which checks if the message contains the !ping command if the condition is true it fulfills the code block and if false it doesn’t.

${Date.now() - message.createdTimestamp}ms.

What is this!

This shows us the latency. The latency, meaning the time taken by the bot to respond to the command.

SO THAT’S A LOT FOR THIS TUTORIAL, IF YOU HAVE ANY PROBLEMS BE SURE TO COME OVER TO MY SUPPORT SERVER, I’LL BE WAITING!!

--

--

Adonis
Adonis

Written by Adonis

0 Followers

Don’t go by the greek reference of my name, I like discord and have a mere affinity towards coding discord bots, I hope I can help you all too!

No responses yet