Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
community creator

How to make a basic Discord Bot using NodeJS

Sanyam Sharma

What is a bot on discord?

Bots on discord can provide various services, ranging from functionalities that help a person manage servers better to in-chat games and services that make the overall experience better. They may act like real users, but they have preset replies that allow them to automatically reply or react to a certain event.
We will be using NodeJS to make the bot.

Prerequisites

  • NodeJS and npm installed
  • Basic knowledge of JavaScript

Setup

Open the terminal in the desired location – this is where all of the bot’s code will be.
Now, enter the following command to initiate an npm directory.

npm init

After filling in all the details above, type:

npm install discord.js dotenv

This command installs two packages, Discord.js and Dotenv.

  • Discord.js makes it easier for us to access the Discord API by providing functions that implement the API directly. (We will talk about the use of Dotenv later.)

You can expect this as an output for the above command:

Before we begin coding, we need to do one more thing – we need to register the bot with discord so that we can access it using our code.
Head on over to Discord Developers Portal and, after signing in, you should see an interface similar to this:

Here, you will be able to see all your registered applications. Click on the New Application button:


Now, you give it a name (anything works) and click on create. You should get something similar to this:

You can explore this on your own but, for now, we are going to keep this to the Bot Sub-Menu on the right-side panel.

Click on Add Bot and press Yes, Do it on the pop-up menu that appears.

Once you are done with that, you will see that your bot is created, and something like this will show up:

Navigate to the OAUTH2 sub-menu and in the scope section check BOT:

Checking BOT should reveal another section beneath scopes:

These are all the permissions you are willing to give the bot. Anyone familiar with discord and discord servers will be able to tinker with them on their own. However, the Administrator role lets the bot do anything on the server, including kicking people out and banning people.

Now, scroll up to the scope section, where you will need to copy the link provided, and paste it into another browser tab/window:

Once you do that, you will be given a list of servers you can add the bot to:

I will be adding it to the Bot Test server that I created for this shot. Finally, click Continue and then Authorize.

You will notice that the bot has now been added to your server:

Code

First, we need to import all the packages we just installed:

require('dotenv').config();
const Discord = require('discord.js');

On the bot sub-menu, find the Token and click on Click here to reveal Token or just click on Copy directly.

Now, make a file in your project directory with the name .env, and write the following:

This will make a variable that can be accessed in our code directly without actually showing the TOKEN – for this, we will be using the dotenv package:

const Bot = new Discord.Client();

const TOKEN = process.env.TOKEN;

Now, we can use the token without actually showing it in the code. The variable Bot contains the client through which we will access the API.

Bot Login

Bot.login(TOKEN)
Bot.on('ready', (client)=>{
    console.log('Logged in as '+ Bot.user.tag);
})

This authenticates your client when you run execute the code, and the ready state gets triggered once the client is authenticated.

The message stage gets triggered every time there is a message in your server and returns whatever message was sent. Therefore, this is the state we care most about and where most of our main code will be.

Below is the code for a simple greeting:

Bot.on('message', (message)=>{
    msg = message.content.toLowerCase();
    if(msg.includes('hello') || msg.includes('hi')){
        message.reply('Hi, I am Hal')
    }
})

In short, we are converting all text to lowercase to avoid any matching errors and then checking if the message contained either a “hello” or “hi,” and then replying with “Hi, I am Hal.”

Bots can be used for a lot of things, you can code minigames or kicks people out who say/do something you don’t like.

I have made a bot that makes it easier to delete mass messages at once. This can be checked on my Github.

Complete code

The following is the entire code:

require('dotenv').config();
const Discord = require('discord.js');
const Bot = new Discord.Client();

const TOKEN = process.env.TOKEN;

Bot.login(TOKEN)
Bot.on('ready', (client)=>{
    console.log('Logged in as '+ Bot.user.tag);
})

Bot.on('message', (message)=>{
    msg = message.content.toLowerCase();
    if(msg.includes('hello') || msg.includes('hi')){
        message.reply('Hi, I am Hal')
    }
})

RELATED TAGS

javascript
community creator
RELATED COURSES

View all Courses

Keep Exploring