How to deploy a Discord Bot on Heroku
Discord BOT

Hey nerd! Are you looking for a solution on How to truly deploy a Discord Bot on Heroku that doesn’t crash after a while? Congratulations! You’re in the right place. Today I’m going to share my experience in deploying a Discord Bot on Heroku.

When I deployed my first Bot on Heroku it crashed and I didn’t even notice it until the server members started complaining that the Bot isn’t working. I panicked! This was my first product release and I messed it up. I sat back and researched about this and finally found what was the problem.

I know I know you’re here for the solution and some of you are here to know how to deploy a bot on Heroku. So without any further ado let’s start.

1. Signup/Login on Heroku

Go to Heroku official site and create an account if you don’t have one already

After logging in you’ll be redirected to the dashboard. If you’re new to Heroku your dashboard will look something like this. Now, we need to Create a New App, click on the purple button or the New button at the top right side corner.

Heroku dashboard

On this page, give your app a unique name and proceed by clicking Create app. This will create an app for you and you’ll get a dashboard for the app. Where you can customize your app to your liking.

Create a new app in Heroku

2. Deploying your application

In the app dashboard, navigate to the Deploy tab. Here, you’ve 3 options to deploy your application with Heroku. I’m going to cover the Connect with GitHub method. Obviously, you can choose any other option for deployment. I’m choosing this method for the sake of simplicity of this tutorial.

Heroku deployment settings

Click on the GitHub option. It’ll ask you to connect your GitHub account with Heroku. Authorize Heroku to access your GitHub account and search for the repository you want to deploy

Searching for a GitHub repository

Click on the repository and you’ll get extra options for deploying the application

Connecting a GitHub repository

Here you can choose the branch you want to deploy. And then click on the Deploy Branch button to deploy your application. But hey! what if your application depends on sensitive API keys or tokens that are stored in environment variables? Well, I got you covered. Don’t click the Deploy Branch button just yet! In the next section, I talk about how to set env variables for your Discord BOT

3. Setting up environment variables

To set env variables in Heroku, switch to the Settings tab in your application dashboard. Here you’ll find the section named Config Vars. Click on Reveal Config Vars to reveal all the env variables.

Heroku config vars

Add your env variables in a `key: value` manner. Just like a JS Object

Adding environment variables

Click on Add. You can add multiple env variable as per your application needs. Once you’re done adding all the env variables, you can switch back to the Deploy tab and click the Deploy Branch.

4. Fixing the crashing behavior

Crashing error

It’s painful to see your BOT crashing like this every 60 seconds, right? So let’s ease the BOT’s pain. The reason for the crash — Heroku’s web dyno is looking for a dynamic $PORT every 60 seconds and since the BOT is not assigning or using a $PORT, it crashes. How to fix this? Good that you asked!

You can write a Procfile which Heroku reads while deploying your application. Make .Procfile in the root directory of your NodeJS application

worker: node index.js

Add this line to the file, push it to GitHub and trigger a manual deployment. But Rahul, the problem still persists., my BOT is still screaming in pain!

I agree sometimes Heroku doesn’t read the .Procfile we defined in the previous step. In that case, I would recommend going with the second alternative.

Installing Heroku CLI

npm install -g heroku

After installing the CLI, run heroku login it’ll prompt a login tab in your browser. After logging in successfully. We need to scale the worker dyno, this will allow your BOT to not depend on the $PORT anymore.

heroku ps:scale worker=1 -a discordbotappname

Run the above command to scale the worker dyno. The flag -a is a required flag which takes in the Heroku application name against which the command will run. The application name is going to be the name you set for your Heroku application in the very first step.

After this command is finished. Heroku will deploy your application again OR if not you can trigger a manual deployment. And TADA!!! We just saved your BOT from crashing again and again
['Rahul Ravindran']
['Discord', 'Heroku', 'Discord Bot']