Introduction to Discord.JS

Atharva Deosthale
Clever Programmer
Published in
4 min readNov 13, 2020

Discord is a communication application that is made on the idea of guilds which are called “servers” in Discord. Discord was initially for gamers to connect and play along and for joining gaming communities. It has voice channels so that players can play along. But Discord has changed a lot and people use Discord for general purposes and not just gaming!

One important factor of Discord is bots. The automated bots power the server (guilds) and make it even more interactive and easy to manage. Any Discord based community is difficult to exist without bots. Bots can play any role in the server, they can be used as a support bot or a moderation bot which will look closely at the user’s activities in the server and warn them on any violation of rules without any need for moderators getting involved.

So today we will be looking at how to get started with Discord bot development using NodeJS. The development of Discord bots is too easy and a very fun task. So let’s get into it!

First of all, we need to create a Discord bot on the Discord Developers website. So click here to go to Discord Developers.

Now you should see an option to create a new application on the top!

Click on “New Application” and name your application, in this case, I will use “Test Medium Bot”. Hit “Create” once you’re done.

Now go to “Bots” in the left panel. Now you should see this screen.

Just click “Add Bot” and Discord will create your first bot account!

Remember to keep your bot token a secret! I’m going to change this as soon as I’m done writing this article. If you expose your token, others can have access to your bot and could potentially harm the servers it’s in or could be misused!

Now copy the token and store it in a safe place!

Let’s invite the bot to a server! Go to the OAuth tab, select bot and set permissions as Administrator, copy the link and follow it, and join the server you want it to.

Now let’s get into the coding part! In an empty directory, open a terminal and type the following command to initialize a NodeJS project.

npm init -y

Now you need to install a dependency to handle your Discord bot. To install it, type the following command in the terminal.

npm install discord.js

This will install discord.js into your Node project. Now create a new file index.js and let’s start coding.

This is the code to bring the bot online.

This is what’s happening in the above code:-

  • We are importing the discord.js module we just installed.
  • Initializing the bot with the Discord Client
  • Logging in the bot using our Discord bot token which we generated at Discord Developers.

Now if you look at your server, the bot must’ve come online.

Now that your bot is online, we can work on the functionality.

This is the code that responds to the command !hello with Hello World.

Let’s see what’s happening here:-

  • We are setting an event listener which listens for the “message” event and if someone sends a message, this event is fired off.
  • We are passing a message object in the callback function
  • The message.content contains the message received by the bot.
  • We check if the message is “!hello” and if it is, we reply Hello World to the same channel as the message was received.

So these are the basics of making a Discord bot, you can manage almost the entire server using Discord.js. Here’s the official discord.js documentation if you want to try out some really cool stuff. And the discord.js documentation is super easy to read!

Discord.js Documentation

Have any questions? Let me know in the comments or you can connect with me on Discord (HappyStark#0001) or Instagram (atharvadeosthale) and I’d be happy to help you!

Thank you!

--

--

Atharva Deosthale
Clever Programmer

Fullstack Web Developer, Freelancer, Teacher and a Blogger. I love to teach people through my writing skills. IG — @atharvadeosthale