How to Build a Discord.js Bot on Ubuntu 20.04

In this article, we’ll explain how to build a Discord.js Bot on Ubuntu 20.04.

A Discord bot can execute commands and perform advanced Discord functions.


Let’s get started.

1. Create a Discord Application

We need to create Discord application to use as a Discord account.

  1. Go to the Discord Developer Portal and click the New Application button. Like shown in the image:build Discord bot HostnExtra
  2. Give your Application a name and click Create.
  3. Add an icon and description if desired.
  4. Click Bot in the Settings menu.
  5. Click the Add Bot button.
  6. Click the Yes, do it! button.
  7. Click the link titled "Click to Reveal Token".
  8. Make a note of this token.

2. Keep the server up to date

Next, login into your server via SSH and update the server using following command:

# apt update -y

3. Install NodeJS

Download latest stable release of NodeJS.

# curl -sL | sudo -E bash -

Install NodeJS

Next, install the NodeJS using following command:

# apt-get install -y nodejs

Verify the installation

# node -v && npm -v



4. Create the project

Switch to the root user’s home directory.

# cd /root

Here you can use any directory you choose.

Create a project folder.

# mkdir hostnextra-bot

Switch to the folder.

# cd hostnextra-bot

Initialize your Node.js project.

# npm init

Answer the questions. Leave the defaults unless you need to change them.
Check your package.json file.

# cat package.json

It should look like:

"name": "hostnextra-bot",
"version": "1.0.0",
"description": "This is HostnExtra’s tutorial bot",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1″
"author": "",
"license": "ISC"

5. Install Libraries

For this demonstration purpose, we’re adding Discord.js library. You can add library that compatible to your bot. Here is the link of useful libraries on the npm website.

Install the Discord.js library.

# npm install discord.js --save

The --save flag adds the library as a dependency in your package.json file.

6. Write the bot code

Create an index.js file in the project folder.

# vi index.js

Add the following to index.js, which creates a new Discord client.

const Discord = require('discord.js');
const client = new Discord.Client();

Add a code block to watch for !hello and respond with Hello World!.

client.on('message', message => {
if (message.content === '!hello') {'Hello World!');

Add a code block to log Ready! in the console when client connects to Discord.

client.once('ready', () => {

Add your Discord Application token, which allows the bot access.


Your finished file should look like this:

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('message', message => {
if (message.content === '!hello') {'Hello World!');

client.once('ready', () => {


7. Install a Node.js process manager

Install the PM2 process manager to manage your bot.

# npm install pm2 -g

Navigate to the project folder.

# cd /root/hostnextra-bot

Start the bot with pm2.

# pm2 start index.js

Verify the bot is online.

# pm2 logs

You should see:

0|index | Ready!

To exit the log, press: CTRL + C

8. Invite the Bot to a server

  1. Go to the Discord Developer Portal.
  2. Navigate to the application’s OAuth2 tab
  3. In the Scopes section, select bot.
  4. In the Bot Permissions section, select Administrator. Note: This can create invite links that only give the bot certain permissions.How to build a Discord.js Bot on Ubuntu 20.04
  5. Open the invite link in a new tab.
  6. Select the server to add the bot to.
  7. Approve the permissions and complete the CAPTCHA.

Now, you can see the active bot in the Discord server.

9. Restart the bot

Final step is the restart the bot.

# pm2 restart index.js

That’s it. We have successfully built Discord Bot.

In this article, we have seen how to build a Discord.js Bot on Ubuntu 20.04.

Related Articles