This article will go through the basic steps to get a Discord Bot setup and hosted through Docker. A more in depth guide on creating commands will be coming soon!
The following article assumes that you some general knowledge in the following areas:
It also assumes that you have a local development environment setup.
First of all, we need to install Node and Docker to our intended server host.
To install Docker, run the command relative to your distro. For our purposes, we will be installing Docker to Ubuntu.
sudo apt install docker
Next, we need to install Node.JS:
sudo apt install nodejs
We can verify that we successfully installed Docker and Node by running:
docker -v
node -v
Now itās time to create the bot! First of all, letās create ourselves a directory for our development files to live.
mkdir bot && cd bot/
This will create a folder called ābotā and enter into it.
Letās initialise our project using Node Package Manager:
touch index.js && npm init --yes
This creates a file called index.js and allows us to begin creating our bot!
We will also create a Dockerfile while weāre at it. This is the file Docker will read from when creating a container.
touch Dockerfile
Now, letās install discord.js - the library we will use to code our bot!
npm install hydrabolt/discord.js --save
Weāll also need a configuration token that stores our bots token.
touch .env
In order to use the Discord Bot API, we need to create a Discord Bot account.
Navigate on over to the Discord Application Page.
Then select, āNew Applicationā.
Give your bot a name and select āCreateā.
Then, navigate to the āBotā tab and select āAdd Botā.
Be sure to grab your bots token here!
Now letās head back on over to our .env
file and enter the
following:
TOKEN:[YOUR-BOT-TOKEN-HERE]
Replace YOUR-BOT-TOKEN-HERE
with the token you copied from your bot.
Note: Do not push the .env file to a Git repository. Ensure this file is in your .gitignore file. Exposing this token allows other users to control your bot.
While weāre at it, letās invite our Discord Bot to our server now. This will allow us to ensure the bot is working in the next step!
Navigate over to the āOAuth2ā tab and select ābotā under the āscopesā section.
Now choose the permissions you want your bot to have over the servers itās connected to. Since our bot wonāt be doing much to begin with, letās start with the following permissions:
After selecting the appropriate permissions, click the ācopyā button above the permissions. Paste the URL into your browser, choose a server to invite the bot to, and click āAuthorizeā.
To successfully add the bot, your account needs āManage Serverā permissions.
Now that weāve configured our botās token, itās time that we actually made one!
Inside of the index.js
file, we will create the bot. Here is an
example bot I will be using for the tutorial:
const Discord = require('discord.js')
const client = new Discord.Client()
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`)
})
client.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('pong')
}
})
client.login(process.env.TOKEN)
Now, letās run node index.js
inside our console. We should see the
bot return ready
. Now type āpingā into your Discord server! You
should receive a reply from the bot!
This means our bot is working as expected! Now itās time for us to put it inside a Docker container.
Now, we need to fill our Dockerfile with everything it needs to run a container of our application!
Since we are using the latest version of Node.JS, our first lne should be:
FROM node:latest
Now letās create a working directory for our bot to run inside of.
RUN mkdir -p /usr/src/bot
WORKDIR /usr/src/bot
Now letās tell Docker what packages it needs by copying our package.json to our working directory:
COPY package.json /usr/src/bot
RUN npm install
This will automatically install our Node Packages when we run our container.
We also need to copy the rest of the project across:
COPY . /usr/src/bot
Now, letās tell Docker how to run the bot:
CMD ["node", "index.js"]
Our finished file should like this:
FROM node:latest
# Create the directory!
RUN mkdir -p /usr/src/bot
WORKDIR /usr/src/bot
# Copy and Install our bot
COPY package.json /usr/src/bot
RUN npm install
# Our precious bot
COPY . /usr/src/bot
# Start me!
CMD ["node", "index.js"]
Now our Dockerfile is ready to be used to build a container for our bot!
To build the container, we first need to create a Docker Image.
docker build -t my-bot .
To check that our image was created successfully, run docker images
.
Now itās time to run the bot using Docker!
docker run -d my-bot
This grabs the Docker Image we just created and runs it using Docker!
Note: Running our bot with `-d` allows the container to run in a detached mode also known as in the background. If you want to see each command is being run, remove that option.
To check that our Docker Container is up and running, run docker ps
.
This shows all your currently running Docker Containers.
You can view the logs of the container using
docker logs <container ID>
.
If you need to get inside the container and run a command, you can run
docker exec -it <container id> /bin/bash
.
Congratulations! Youāve successfully created a Discord Bot and hosted it with Docker!
This is a fantastic project to get started with the basics of Docker and using containers. This same method can be used with a number of different applications.
If you have any questions or think I could have taken a better approach, let me know! Feel free to reach out in the comments below or reach out to me via email.