Deploying a Java Discord Bot jar on Heroku

Discord Bots are awesome!

Bots are the most awesome feature of Discord and are a common sight in every community server. There are tons of public bots covering almost all popular features. But maybe they don’t have something you want? Well, you can create a bot yourself too! Developing a Discord bot is easy thanks to dozens of community API wrappers and bot-development tools. Read more about Java bot discord development here:

But after developing the bot, you have to keep it online 24/7. You can either make it run on your PC. Or you can host it in the Cloud/VPS/PaaS.

Heroku makes running apps simple

What is Heroku?

Heroku is a cloud platform as a service (PaaS) supporting several programming languages. Heroku provides services and tools to build, run, and scale web applications.

They have a free tier which gives you 1000 hours/month for any number of apps. A month has 720 hours… so you can deploy your 1 Java app (aka bot) 24/7 on Heroku for free. 😃

Requirements

  • Verified free Heroku account.
  • Heroku CLI installed on your machine.
  • The bot project with Git initialized and Maven/Gradle.

Step 1: Creating Jar

Since Discord bots usually have more than one dependencies, we will need to include them in the jar in order for it to run (fat Jar).

For Maven

We’re using the maven-shade-plugin. Add this to your <build> tag:

YOUR_MAIN_CLASS is your bot’s entry point (like org.bot.MainClass).

Now run the Maven goal: mvn clean compile assembly:single

For Gradle

We’re using the shadow plugin. Add this to your build.gradle:

YOUR_MAIN_CLASS is your bot’s entry point (like org.bot.MainClass).

Now run the shadowJar task.

Either of these will create a fat jar in your build output directory. (which can be anything like out or target).

Step 2: Creating Procfile

A Procfile is a UTF-8 file that specifies the commands that are executed by Heroku (more specifically app’s dynos).

Create a file named Procfile in your bot project’s root directory. Put the following line in the file.

worker: java -jar target/bot-1.0-jar-with-dependencies.jar

Step 3: Deployment to Heroku

First, we have to create the app on Heroku. You can do this by either visiting your Heroku dashboard or using the Heroku CLI (command-line interface) installed on your local machine.

Here’s how to do it with CLI: heroku create --no-remote .

What this does is create a Heroku app without any remote to track your git project.

You can also provide a name heroku create myDiscordBot --no-remote.

Now we can upload our Jar using the command

heroku deploy:jar target/bot.jar --app APP_NAME

APP_NAME is your Heroku app name like myDiscordBot. Replace target/bot.jar with your relative path of your fat jar produced in last step.

Scale your bot to 1 dyno with

heroku ps:scale worker=1 --app APP_NAME


Once this finishes your Bot should be online 24/7, free of cost.

P.S. Heroku is not suitable for bots that need high data storage or persistent file-system. Data stores can get expensive on Heroku and it uses an ephemeral file-system.