
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 Debian 11 cloud server at Vultr.
Install Node.js, a javascript runtime that is required for Discord.js.
Install Node Version Manager (nvm), which is used to update Node.js. For example:
Note: Please see NVM's GitHub repository and use the command for the latest NVM version.
Reboot the server.
Reconnect to the SSH session.
Verify the nvm installation:
It should return:
Update Node.js to the latest version:
Switch to your home directory.
Create a project folder.
Switch to the project folder.
Initialize your Node.js project.
Answer the questions. Leave the defaults unless you need to change them.
Check the contents of your package.json file:
It should return something like this:
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.
The --save flag adds the library as a dependency in your package.json file.
Create a file named index.js.
Paste this sample code to your index.js file. Replace <YOUR BOT TOKEN> with the token you saved in section 1.
The Discord bot should respond to commands hello, testbot, and ping.
Save and exit the file.
Your Discord bot stops running when you exit the SSH session. To keep the bot running full-time, use Tmux or PM2.
Follow this guide to install tmux.
Create a tmux session.
Change to the Discord bot project directory.
Start the bot.
It should return:
To stop the Discord Bot, press Ctrl + C
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.
Change to the Discord bot project directory.
Start the Bot.
Verify if the bot is online.
It should return:
To exit the log, press Ctrl + C
Make sure you are in the ~/discord-bot directory, then use these commands to control your bot.
List all PM2 processes:
Stop the Discord bot:
Restart the Discord bot:
The Discord bot is now up and running.
To learn more about Discord bots, please see these resources:
0 Comments
Be the first to comment and share your perspective with the community.