Hosting a Discord bot using Heroku for free

Heroku is a powerful platform as a service (PaaS) that allows you to build, run and operate many different types of applications in the cloud.

We are going to use Heroku to deploy a Discord bot that has been written using Node.js. However, you can use the same techniques described in this article to run a bot that has been written in Ruby, Java, PHP, Python or Go on Heroku.

Heroku offers 550 free Dyno (hosting) hours for new accounts. If you add your credit card to your account, you get an additional 450 hours taking you to a total of 1000 free Dyno hours per month which is more than enough to host your Discord bot 24/7.

Deploying your bot

To get your bot up and running, we need to tell Heroku what services our bot needs and what commands to run to start the bot.

First of all, we need to tell Heroku what version of node we need. We do this by updating the engines property in our package.json

                    {
    "name": "discord-bot-test-js",
    "version": "1.0.0",
    "description": "Discord bot test",
    "main": "index.js",
    "engines": {
        "node": "12.x",
        "npm": "*"
    },
    "author": "Mike Wazowski (https://en.wikipedia.org/wiki/Monsters,_Inc.)",
    "dependencies": {
        "discord.js": "^11.5.1",
    }
}
                

This indicates to Heroku that we need node v12 and npm to use our application.

The Profile

Next, we need to tell Heroku what commands to run on startup. This is done by a file called Procfile that is located in the root directory of your repository.

This file contains the process and the commands to run.

                    service: node index.js
                

The Procfile can be modified to suit your bot needs. We are going to simply start our bot using node.

You can now commit your Procfile to your repository and push it your master branch.

Connect your repository

There are multiple ways you can deploy your code onto Heroku.

You can use Heroku CLI to connect to your git repository, use a Docker container or connect straight to your GitHub.

An image showing the deployment methods available in Heroku

The easiest way is to host your repository on GitHub; go to the "Deploy" section in Heroku, connect your GitHub account to Heroku and simply enter your repository name and you're connected!

Unless you have hardcoded the credentials of your bot, you are most likely going to use environment variables to provide settings to your bot. This can be achieved by updating your projects Config Vars.

You will need to ensure that your service is checked in the "Resources" section of Heroku before you start your app.

An image showing the Resources section in Heroku

Now that we have everything set up and ready, we can simply go to the deploy section in Heroku, and manually deploy a branch.

An image showing the manual deploy functions in Heroku

You can go to "More" and "View logs" and you will be able to see the log output of your bot deployment and if everything goes successfully, your bot will be up and running within minutes!

To conclude

Using Heroku, we can deploy many types of applications including a Discord bot. You can customise the deployment to fit your application requirements and use their free tier to host a basic application.

Heroku allows you to focus on the code of your application and let Heroku do the heavy lifting to host your application in the cloud.

Useful resources