arrow left
Back to Developer Education

How to Build a Discord Bot using JavaScript

How to Build a Discord Bot using JavaScript

Discord is a robust communication app used by a large community of gamers, developers, and cryptocurrency enthusiasts. <!--more--> Admins can add unique Discord bots to their servers. There are also powerful APIs for creating Discord bots for those who prefer to take matters into their own hands. For instance, Discord.js allows us to create a simple Discord bot using Javascript.

In this tutorial, we'll discuss how to create a Discord bot and run it on the server.

Table of contents

Requirements

To follow along, the reader will need:

  • A Wi-Fi connection.
  • A Discord account.
  • A code editor such as Visual Studio Code.
  • Node v17.1.0.

Add a new Discord application

To get started, we need to register the application via Discord Developer's portal. To access this portal, navigate to https://discord.com/developers/applications.

Then click on the New Application button on the right-hand side.

new application

After clicking the button, a dialog box will pop up which will ask you to name your bot. In my case, I will name the bot simply as 'mybot':

Dialog

Next, click the bot option, followed by the add bot button. The following dialog will pop up:

Confirmation

Then confirm the creation of the new application and that is it; you have a bot.

However, I don't think you want a dummy bot. Let's add some extra features by giving it several permissions which will improve its interactivity with users.

Click the OAuth2 menu and check the boxes, as shown below. Then, copy the link that appears in the text box:

bot

permission

Next, paste the link you copied earlier in any browser. You should see the following screen:

paste-link

Select your server and then click on the authorize button:

authorize

In case you don't have a server, follow this link to create one.

Creating a Node.js project

Since we have registered our bot, we now need to create a folder where will store our Node.js files. You can download the latest Node.js version from here.

Installing dependencies

Dependencies are packages that are required by the application to work. In our case, we only need the discord.js library. Nevertheless, we will install the dotenv package to assist in managing mybot token.

This will ensure that all calls are properly authorized. We also require nodemon to keep the server running. Start a new terminal and type in the following command to initialize the project:

npm init -y

Note how the above command generates an npm project without going through the interactive process.

In my case, the following are the results:

{
  "name": "mybot",
  "version": "1.0.0",
  "description": "",
  "main": "bot.js",
  "scripts": {
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

The command below will install discord.js and dotenv library in the project:

npm install discord.js dotenv

To install nodemon library, use the following command:

npm install -D nodemon

Nodemon restarts the server every time we make a change to our files.

Building our bot

We need to authorize calls from Discord.js in the developers' portal. To do this, we must copy our bot's token.

copy-token

Once copied, open a new file and name it .env, then paste it in the following format.

TOKEN=Paste_token_here

Create the bot file

Create a new file and name it bot.js in the main project directory. We will set up environment variables using the dotenv package, import classes from discord.js, and then start up a new client:

require("dotenv").config(); //to start process from .env file
const {Client, Intents}=require("discord.js");
const client=new Client({
    Intents:[
        Intents.FLAGS.GUILDS,//adds server functionality
        Intents.FLAGS.GUILD_MESSAGES //gets messages from our bot.
    ]
});
client.once("ready", () =>{
    console.log("BOT IS ONLINE"); //message when bot is online
})
client.login(process.env.TOKEN);

To run the bot, type the following in your terminal:

node bot.js

bot-online

As shown above, our bot is online but it does not have any functionality.

Note that we are authenticating the named variable from our .env file, which is connected to the Discord API. To test the bot, we have to be logged into a Discord account and have a server running.

Replying to messages

So far, our bot is online but has no active functionality. The following lines of code will have our bot reply to a simple hello message:

client.on('message',
function (messages){
    if(messages.content.toLocaleLowerCase()==='hello') 
    messages.channel.send('hello' + ' '  + messages.author.username); //reply hello word message with senders name
})

Our bot will reply to the hello message and mention the sender.

reply-message

Summary

We added a new Discord application via the Discord developers portal.

To develop our bot, we created a Node.js project and installed the required dependencies. We then allowed the bot to reply to certain messages from users.

You can access the complete code from here.

Conclusion

The Discord platform is growing rapidly. It has created a secure way for gamers, entrepreneurs, and cryptocurrency enthusiastic to communicate and connect with other like-minded people.

Having a good bot that can reply, ban, and mute members is an added advantage. In this article, we covered how to create a bot to reply to certain text. You can, therefore, use this knowledge to incorporate other functionalities.

Happy coding!

Other resources


Peer Review Contributions by: Wanja Mike

Published on: Dec 28, 2021
Updated on: Jul 15, 2024
CTA

Start your journey with Cloudzilla

With Cloudzilla, apps freely roam across a global cloud with unbeatable simplicity and cost efficiency