Programming a Discord bot can be very fun, but if you’d like to have it online 24/7, you need to host it. The best platform to host any Node.js related applications is Heroku. It’s fast and easy and you have multiple ways when it comes to deploying the application.
The first part is to make an account on https://www.heroku.com/ — after registering and logging in, you will be greeted by the dashboard.
To make a new application, click the “New” button and press “Create New App”.
After choosing your name and your region, you can create the application.
Now if we go in the deploy tab, we can see that we can use two ways to deploy our application. We will go through the both ways.
1. Github Repository
I mainly suggest this method of deploying your application because it’s quite easy and much faster than the method using Heroku’s CLI.
So, before deployment we need to do some things with our work space. Firstly, create a file called Procfile and add the following line:
The “Worker: node” part is the same for all applications you just adapt to yours, depending on how your main file is called. Mine is index.js — After that we need to go into the main file and change the line where you login your bot using your token. It’s best practice to always remove tokens and use environment variables. So let’s do it!
You can call your variable however you want, I’d like to keep it short and simple so I called it TOKEN. We are done done with this part, next we need to initialize a repository. Now we need to head to Github and and create a repository, after doing so, we just follow the instructions given by Github.
git add .
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/(your-username)/(your-repo-name)
git push -u origin main
After pushing to our repository, we’ve completed the most hard part of this whole process. Now let’s head to Heroku. Go to the Settings tab and to the Config Vars part specifically and click “Reveal Config Vars”, upon doing so, you should see this:
Now lets add the TOKEN variable and our actual token that you can get from:
Discord Developer Portal — API Docs for Bots and Developers
Integrate your service with Discord — whether it’s a bot or a game or whatever your wildest imagination can come up…
After doing it, it should look something like this:
Then, let’s head to the deploy section and let’s click on the Connect to Github button.
After connecting to Github and connecting our repository, we should enable automatic deploys, this means each time you push your code onto Github, it will update the application on Heroku.
The final part is selecting the correct Profcile, to do so head to the Resources tab. We should disable the web procfile and enable to Worker procfile, so do the following:
Confirm the changes and that’s it. Congratulations, you have hosted your first Discord.js bot!
2. Heroku’s CLI
To use Heroku’s CLI, firstly we need to install it on our system.
The Heroku CLI
The Heroku Command Line Interface (CLI) makes it easy to create and manage your Heroku apps directly from the terminal…
From the link, we download and install the CLI which is quick and easy and after that, we can follow the instructions in our deploy tab.
We open a terminal window and we write the following command:
Like the terminal suggests, we press any button to open the login window. Then we are prompted to login from our browser and after doing so, we are set. We change to the directory of our Discord.js bot and we initialize a Git repository by the following command:
- git init
After initializing the repository, we need to add a remote, we need to execute:
- heroku git:remote -a -(application name)
You need to change the last argument with the name of your application but other than that it’s pretty much the same.
After the initialization we need to take care some of our files before deploying our application. For Heroku to know which file to execute on each launch, we need to create a file named Procfile and add the following line:
This file should be in the main directory of the Discord bot, and depending on how is your main file called, you adapt the Procfile, mine is index.js — other than that, it’s best practice to always hide your Discord.js token into environment variables, so nobody can steal your token. So on your last line, where you login the bot, remove the token itself and change it with the following:
You can always name it however you want, I’d like to call it TOKEN.
Now we are ready to deploy our application!
By following these commands, we can easily deploy the application, so lets do it!
After running all of the commands, we should get this message at the end. So now onto the last step.
Firstly go to the Settings tab and scroll to the Config Vars part, we firstly need to inform Heroku about our token.
Make sure you add a variable called TOKEN or however you named it in your your index.js and in the second input, you put your token that you get from:
Discord Developer Portal - API Docs for Bots and Developers
Integrate your service with Discord - whether it's a bot or a game or whatever your wildest imagination can come up…
The final step is to head to the Resources tab and select the correct Procfile.
You should enable the Worker procfile since that is the one we created.
Voila! That’s it. You have successfully deployed your Discord.js bot using Heroku’s CLI.
Those are the two main ways to deploy your Discord.js bot using Heroku, it’s a really simple and easy way to host a bot of any size, if you guys have any other website that is good for hosting, be sure to leave it in the comment section below.
— Thanks for your time.