Knowledgebase

How to Run a Discord.js Bot on CentOS 8 Print

  • 0

Introduction

Discord is a video, voice, and text communication platform that allows bots to perform automated tasks based on user commands. This article explains how to run a Discord.js bot and host it on a CentOS 8 cloud server at Rcs.

1. Create a Discord Application

  1. Go to the Discord Developer Portal and log in.
  2. Click the New Application button.
  3. Enter an application name and click Create.
  4. Select the Bot tab from the Settings menu.
  5. Click Add Bot then click Yes, do it!.
  6. Click Copy to copy your bot's token to the clipboard. Save a copy to use later in this guide.

2. Install Prerequisites

3. Install Dependencies

  1. Install Node.js, a javascript runtime that is required for Discord.js.

     # yum install nodejs -y
  2. Install Node Version Manager (nvm), which is used to update Node.js. For example:

     # wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

    > Note: Please see NVM's github repository and use the command for the latest NVM version.

  3. Reboot the server.

  4. Restart the SSH session.

  5. Verify the nvm installation:

    # command -v nvm

    It should return:

     nvm
  6. Update Node.js to the latest version:

     # nvm install node

4. Create the Discord Bot

  1. Switch to your home directory.

     # cd ~
  2. Create a project folder.

     # mkdir discord-bot
  3. Switch to the project folder.

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

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

  6. Check the contents of your package.json file:

     # cat package.json

    It should look like this:

      {
        "name": "discord-bot",
        "version": "1.0.0",
        "description": "A Discord.js Bot on CentOS 8",
        "main": "index.js",
        "scripts": {
          "test": "echo \"Error: no test specified\" && exit 1"
        },
        "author": "",
        "license": "ISC"
      }

    For this guide, you need to install the Discord.js library using npm. As you add capabilities to your bot, look for other useful libraries on the npm website.

  7. Install the Discord.js library.

     # npm install discord.js --save

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

  8. Create a file named index.js.

     # nano index.js
  9. Paste this sample code to your index.js file. Replace <YOUR BOT TOKEN> with the token you saved in section 1.

      const { Client, Intents } = require('discord.js');
      const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
    
      client.on('message', message => {
          if (message.content === '!hello') {
              message.channel.send('Hello World~!');
          }
          if (message.content === '!testbot') {
              message.channel.send("Hi! I'm up and Running~!");
          }
          if (message.content === '!ping') {
              message.channel.send('Pong~!');
          } 
      })
    
      client.once('ready', () => {
          console.log('The Discord Bot is Ready!');
      })
    
      client.login('<YOUR-BOT-TOKEN>')

    The Discord bot should respond to commands such as !hello, !testbot, and !ping.

  10. Save and exit the file.

5. Run the Discord Bot

Your Discord bot stops running when you exit the SSH session. To keep the bot running full-time, use Tmux or PM2.

Option 1: Run the Discord Bot with Tmux

  1. Follow this guide to install tmux.

  2. create a tmux session, run:

     # tmux new -s DiscordBot
  3. Change to the Discord bot project directory.

     # cd ~/discord-bot
  4. Start the bot.

     # node index.js

    It should return:

     The Discord Bot is Ready!

To stop the Discord Bot, press Ctrl + C

Option 2: Run the Discord Bot with PM2

PM2 is a daemon process manager that will help you manage and keep your application running.

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

     # npm install pm2 -g
  2. Change to the Discord bot project directory.

     # cd ~/discord-bot 
  3. Start the Bot.

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

     # pm2 logs

    It should return:

      0|index    | The Discord Bot is Ready!

    To exit the log, press Ctrl + C

Common PM2 Commands

Make sure you are in the ~/discord-bot directory, then use these commands to control your bot.

  • List all PM2 processes:

      # pm2 list
  • Stop the Discord bot:

      # pm2 stop index.js
  • Restart the Discord bot:

      # pm2 restart index.js

6. Invite the Bot to a Discord Server

  1. Go to the Discord Developer Portal and log in.
  2. Click the Application that you have created.
  3. Navigate to the OAuth2 Tab from the Settings menu.
  4. In the Scopes Section, select bot.
  5. In the Bot Permissions Section, select Administrator. This gives the bot an Administrator Role from the Discord Server.
  6. Click the Copy Button to copy the generated invite link.
  7. Open the invite link on your browser.
  8. Select the Discord server to add the bot to then click Continue.
  9. Authorize the bot and complete the CAPTCHA.

The Discord bot is now up and running.

More Information

To learn more about Discord bots, please see these resources:

Introduction Discord is a video, voice, and text communication platform that allows bots to perform automated tasks based on user commands. This article explains how to run a Discord.js bot and host it on a CentOS 8 cloud server at Rcs. 1. Create a Discord Application Go to the Discord Developer Portal and log in. Click the New Application button. Enter an application name and click Create. Select the Bot tab from the Settings menu. Click Add Bot then click Yes, do it!. Click Copy to copy your bot's token to the clipboard. Save a copy to use later in this guide. 2. Install Prerequisites Deploy a Rcs CentOS 8 cloud server instance Log in as root Update the CentOS 8 server 3. Install Dependencies Install Node.js, a javascript runtime that is required for Discord.js. # yum install nodejs -y Install Node Version Manager (nvm), which is used to update Node.js. For example: # wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash > Note: Please see NVM's github repository and use the command for the latest NVM version. Reboot the server. Restart the SSH session. Verify the nvm installation: # command -v nvm It should return: nvm Update Node.js to the latest version: # nvm install node 4. Create the Discord Bot Switch to your home directory. # cd ~ Create a project folder. # mkdir discord-bot Switch to the project folder. # cd discord-bot Initialize your Node.js project. # npm init Answer the questions. Leave the defaults unless you need to change them. Check the contents of your package.json file: # cat package.json It should look like this: { "name": "discord-bot", "version": "1.0.0", "description": "A Discord.js Bot on CentOS 8", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" } For this guide, you need to install the Discord.js library using npm. 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. Create a file named index.js. # nano index.js Paste this sample code to your index.js file. Replace with the token you saved in section 1. const { Client, Intents } = require('discord.js'); const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] }); client.on('message', message => { if (message.content === '!hello') { message.channel.send('Hello World~!'); } if (message.content === '!testbot') { message.channel.send("Hi! I'm up and Running~!"); } if (message.content === '!ping') { message.channel.send('Pong~!'); } }) client.once('ready', () => { console.log('The Discord Bot is Ready!'); }) client.login('') The Discord bot should respond to commands such as !hello, !testbot, and !ping. Save and exit the file. 5. Run the Discord Bot Your Discord bot stops running when you exit the SSH session. To keep the bot running full-time, use Tmux or PM2. Option 1: Run the Discord Bot with Tmux Follow this guide to install tmux. create a tmux session, run: # tmux new -s DiscordBot Change to the Discord bot project directory. # cd ~/discord-bot Start the bot. # node index.js It should return: The Discord Bot is Ready! To stop the Discord Bot, press CTRL + C Option 2: Run the Discord Bot with PM2 PM2 is a daemon process manager that will help you manage and keep your application running. Install the PM2 process manager to manage your bot. # npm install pm2 -g Change to the Discord bot project directory. # cd ~/discord-bot Start the Bot. # pm2 start index.js Verify if the bot is online. # pm2 logs It should return: 0|index | The Discord Bot is Ready! To exit the log, press CTRL + C Common PM2 Commands Make sure you are in the ~/discord-bot directory, then use these commands to control your bot. List all PM2 processes: # pm2 list Stop the Discord bot: # pm2 stop index.js Restart the Discord bot: # pm2 restart index.js 6. Invite the Bot to a Discord Server Go to the Discord Developer Portal and log in. Click the Application that you have created. Navigate to the OAuth2 Tab from the Settings menu. In the Scopes Section, select bot. In the Bot Permissions Section, select Administrator. This gives the bot an Administrator Role from the Discord Server. Click the Copy Button to copy the generated invite link. Open the invite link on your browser. Select the Discord server to add the bot to then click Continue. Authorize the bot and complete the CAPTCHA. The Discord bot is now up and running. More Information To learn more about Discord bots, please see these resources: Discord's Documentation Developer Portal Discord.js Website

Was this answer helpful?
Back

Powered by WHMCompleteSolution