My journey with Discord bots from Java to Node.js — Part 3 (Final) Deploying the bot to Heroku

Amr Gawish
kblog
Published in
4 min readMay 3, 2017

--

Continuing my journey after finishing Part 2, now that everything in place I want a good place to host my bot and monitor its progress.

Table of Content

The first rule about Discord bots

The first rule about Disrcord bot, we don’t talk about Discord bots!

The truth about Chat bots, especially the Discord User Bots — not webhook bots — is that you don’t need to deploy them. You just need a computer that is connected to the internet and your bot listening to the socket connection, and you should be good to go.

But since we are in the cloud era, it makes sense to host your bot elsewhere and Heroku is a great place to host your Node.js application.

I first ran my bot on my private server, even when the internet connection was always there, I had a lot of trouble with web socket connection going down periodically, and connecting via VNC or SSH to my server and restarting it was troublesome, even with solutions like Forever, the connection were never stable, so I decided to go for Heroku and it was a great decision!

Pros and Cons of deploying to Heroku

There are multiple things you need to worry about when deploying to Heroku or any other cloud provider:

  1. Bots don’t have any UI Interface, so you have to check the log or have a monitoring dyno in order to track your application health.
  2. Free dynos may not suitable for your bot, since your bot is going to be idle a lot of the times, and Heroku automatically put them into an idle state when they are not used — This is useful in a web interface, not so much for a bot! — which stops all socket connections!
  3. Unless you have a robust Error Handling built in, Heroku won’t be able to tell you if things are not working!

However there are a good news in here, some of them are:

  1. Heroku provides you with a public URL, which can be used for webhooks and integration with other messaging platforms like Facebook messanger, Line and others.
  2. Heroku has a lot of integrations between different AI/ML engines that can make your bot a lot smarter.
  3. You get Continuous Integration for Free, just push your code and everything is redeployed and updated!

Deploying to Heroku

Everything is ready for Heroku, just hook it to my Github repository or use the Heroku CLI and you should be good to go.

Heroku also uses Foreman to load your Environment variables and you can utilise that in your bot to save the API Keys and token in these environment variables — good if you open source your project — instead of your config file.

In my case I just needed to add a start script to my package.json— since my main entry point is not “server.js” — and I’m good to go!

So my package.json becomes something like this:

{"name": "trials-bot","version": "1.0.0","description": "Trials of Osiris Discrod Bot","main": "app.js","scripts": {"start": "node app.js","test": "echo \"Error: no test specified\" && exit 1"},"keywords": [],...
}

And we are done, now you have our bot hosted in Heroku and working with Discord.js!

Taking your bot a step further

Since you have a public URL for your bot, why not utilise it!

Just creating a static HTML page with a minimal bootstrap template to show case your bots commands and maybe a screenshot can be useful right!

And that is exactly what I did, I didn’t want to do anything fancy, just a static HTML page describing the bot and that is it.

First I added express as a dependency.

npm install express --save

I downloaded Bootstrap template — I went for the cover theme — and put everything in “public” folder, so my structure became something like this:

├───lib/
│ └───services/
│ └───bungie.service.js
│ └───commands/
│ └───command.js
│ └───command.registry.js
│ └───help.command.js
├───conf/
│ └───config.js
├───public/
│ └───js/*
│ └───css/*
│ └───img/*
│ └───index.html
├───app.js
├───package.jso

I made sure my index.html looks nice offline, and I added the below code to my app.js

And voila, now I can see my bot website in action, and the bot is secretly running in the background, here is how it looks.

You can try it out yourself from here!

Marketing your bot

The bot is there, but you still need to tell the people what it is and how to use it through different commands.

The perfect place to market your Discord bot is by adding it to the unofficial Discord bot marketplace.

This way users can search what it is, check the website, and even know the technology behind it. The portal makes it really easy for Server/Guild admins to search and add your bot to their channels, just make sure you add the important searchable keywords in your bot description.

Final words

Hope you liked this series of posts about the journey I went through from converting this bot from Java to Node.js, I’ll open source the project soon so check back later.

Overall I’m glad that I made the switch, Node.js community is very vibrant and it is a great match for such projects. All my bots are going to be developed with Node.js from now on!

If you have any questions, please don’t hesitate to ask in the comment section, and leave a ❤ if you liked this post.

Thanks, Amr.

--

--

Editor for

A father, Gamification advocate, and a fellow software craftsman!