Author: Kenn Carlo Gutierrez
Last Updated: Wed, Sep 1, 2021Discord 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 on a Docker application.
Install Node.js, a javascript runtime that is required for Discord.js.
# apt-get 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.
# reboot
Reconnect to the SSH session.
Verify the nvm installation:
# command -v nvm
It should return:
nvm
Update Node.js to the latest version:
# nvm install node
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 return something like this:
{
"name": "discord-bot",
"version": "1.0.0",
"description": "A Discord.js bot",
"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. Then, 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 <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 hello
, testbot
, and ping
commands.
Save and exit the index.js
file.
Create a Dockerfile
.
# nano Dockerfile
Paste this to your Dockerfile
.
FROM node:latest
# Create the bot's directory
RUN mkdir -p /usr/src/bot
WORKDIR /usr/src/bot
COPY package.json /usr/src/bot
RUN npm install
COPY . /usr/src/bot
# Start the bot.
CMD ["node", "index.js"]
Save and exit the Dockerfile
.
Change to the Discord bot project directory.
# cd ~/discord-bot
Build the docker container for the Discord bot.
# docker build -t discord-bot .
Run the docker container.
# docker run -d discord-bot
Running the bot with -d
flag runs the container in detached mode (it runs in the background).
List all docker processes:
# docker ps
Stop the docker container (discord bot):
# docker stop <CONTAINER ID>
Restart the docker container:
# docker restart <CONTAINER ID>
You can find the <CONTAINER ID>
when you list all the Docker processes.
The Discord bot is now up and running.
To learn more about Discord bots, please see these resources: