Run a Discord.js Bot on Ubuntu 20.04

Updated on July 25, 2024
Run a Discord.js Bot on Ubuntu 20.04 header image

A Discord bot can execute commands and perform advanced Discord functions. This tutorial explains how to create a simple bot with Discord.js on a Vultr Ubuntu 20.04 server.

Create a Discord Application

A Discord bot needs an application to use as a Discord account.

  1. Go to the Discord Developer Portal and click the New Application button.

    Discord Token

  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.

Install Vultr Server

  1. Deploy a new Vultr Ubuntu instance. Use the following settings:

    • Server Type: Cloud Compute Server
    • Server Location: The location closest to you, or closest to the majority of the bot's users.
    • Server OS: Ubuntu 20.04, 64-bit.
    • Server Size: Choose a size according to your expected user volume. You can start with a small instance and upgrade later with the Change Plan option in the server control panel. You do not need to reinstall when upgrading plans.
  2. Click the View Console icon in the top right corner, or SSH to the server.

  3. Log in as root using the password on the server information page.

  4. Update the apt database.

     # sudo apt-get update

Install Dependencies

  1. Install Node, which is required for Discord.js

     # sudo apt-get install nodejs
  2. Install the Node Package Manager, which is used to install the Discord.js library.

     # sudo apt-get install npm
  3. Install Node Version Manager (nvm), which is used to update Node.js. For example:

     # curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash

    Note: Use the command for the latest version, which is found here: https://github.com/nvm-sh/nvm#installing-and-updating

  4. Reboot the server.

  5. Reconnect to the server console.

  6. Verify the nvm installation:

     # command -v nvm

    It should return:

     nvm

    If it returns nothing, or an error, run the following:

     # export NVM_DIR="$HOME/.nvm"
     # [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
     # [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
  7. After you've installed nvm, update Node.js to the latest version:

     # nvm install node
     # nvm use node

Create the Project

  1. Switch to the root user's home directory.

     # cd /root
  2. Create a project folder.

     # mkdir tutorial-bot
  3. Switch to the folder.

     # cd tutorial-bot
  4. Initialize your Node.js project.

     # npm init
  5. Answer the questions. Leave the defaults unless you need to change them.

  6. Check your package.json file.

     # cat package.json

    It should look like:

     {
       "name": "tutorial-bot",
       "version": "1.0.0",
       "description": "Tutorial Bot",
       "main": "index.js",
       "scripts": {
         "test": "echo \"Error: no test specified\" && exit 1"
       },
       "author": "",
       "license": "ISC"
     }

Install Libraries

Libraries make writing code easier. For this guide, you need the Discord.js library. As you add capabilities to your bot, look for other 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.

Write the Bot Code

  1. Create an index.js file in the project folder.

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

     const Discord = require('discord.js');
     const client = new Discord.Client();
  3. Add a code block to watch for !hello and respond with Hello World!.

     client.on('message', message => {
         if (message.content === '!hello') {
             message.channel.send('Hello World!');
         }
     });
  4. Add a code block to log Ready! in the console when client connects to Discord.

     client.once('ready', () => {
        console.log('Ready!');
      }
  5. Add your Discord Application token, which allows the bot access.

     client.login('YOUR-TOKEN-HERE')

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') {
        message.channel.send('Hello World!');
    }
})

client.once('ready', () => {
    console.log('Ready!');
})

client.login('YOUR-TOKEN-HERE')

Install a Node.js Process Manager

  1. Install the PM2 process manager to manage your bot.

     # npm install pm2 -g
  2. Navigate to the project folder.

     # cd /root/tutorial-bot
  3. Start the bot with pm2.

     # pm2 start index.js
  4. Verify the bot is online.

     # pm2 logs

    You should see:

     0|index | Ready!

    To exit the log, press: Control + C

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.

    OAuth2

  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.

    The bot is now active and able to respond to your command.

    Bot Joined

    Hello World

Next Steps

After you develop your bot further, go to your server's project folder restart the bot.

# pm2 restart index.js

The Discord.js website has more documentation needed to develop advanced JavaScript bots.