
A Discord bot can execute commands and perform advanced Discord functions. This tutorial explains how to run a Discord.js Bot on Ubuntu by creating a simple bot with Discord.js on a Vultr Ubuntu 20.04 server.
A Discord bot needs an application to use as a Discord account.
Go to the Discord Developer Portal and click the New Application button.
Give your Application a name and click Create.
Add an icon and description if desired.
Click Bot in the Settings menu.
Click the Add Bot button.
Click the Yes, do it! button.
Click the link titled "Click to Reveal Token".
Make a note of this token.
Deploy a new Vultr Ubuntu instance. Use the following settings:
Click the View Console icon in the top right corner, or SSH to the server.
Log in as root using the password on the server information page.
Update the apt database.
Install Node, which is required for Discord.js
Install the Node Package Manager, which is used to install the Discord.js library.
Install Node Version Manager (nvm), which is used to update Node.js. For example:
Note: Use the command for the latest version, which is found here: https://github.com/nvm-sh/nvm#installing-and-updating
Reboot the server.
Reconnect to the server console.
Verify the nvm installation:
It should return:
If it returns nothing, or an error, run the following:
After you've installed nvm, update Node.js to the latest version:
Switch to the root user's home directory.
Create a project folder.
Switch to the folder.
Initialize your Node.js project.
Answer the questions. Leave the defaults unless you need to change them.
Check your package.json file.
It should look like:
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.
The --save flag adds the library as a dependency in your package.json file.
Create an index.js file in the project folder.
Add the following to index.js, which creates a new Discord client.
Add a code block to watch for !hello and respond with Hello World!.
Add a code block to log Ready! in the console when client connects to Discord.
Add your Discord Application token, which allows the bot access.
Your finished file should look like this:
Install the PM2 process manager to manage your bot.
Navigate to the project folder.
Start the bot with pm2.
Verify the bot is online.
You should see:
To exit the log, press: Control + C
Go to the Discord Developer Portal.
Navigate to the application's OAuth2 tab
In the Scopes section, select bot.
In the Bot Permissions section, select Administrator. Note: This can create invite links that only give the bot certain permissions.
Open the invite link in a new tab.
Select the server to add the bot to.
Approve the permissions and complete the CAPTCHA.
The bot is now active and able to respond to your command.
After you develop your bot further, go to your server's project folder restart the bot.
The Discord.js website has more documentation needed to develop advanced JavaScript bots.
0 Comments
Be the first to comment and share your perspective with the community.