Skip to content

The Heroku Hosting

Anjan Nair edited this page Oct 8, 2020 · 3 revisions

HOSTING YOUR BOT

How to keep my bot up 24/7?

After writing your code and after running it successfully on your local machine, its time to host it on a cloud platform

What is a cloud platform?

Well they are typically hundreds and hundreds of computers in underground bunkers that will host your code or rather anyone's code or software at a given price or for free. Some of the examples are Google Cloud, Amazon Web Services, Heroku etc.

Where will I be hosting my code?

This tutorial will cover the steps to host your bot on the Heroku platform. Heroku provides 550 free hours per dyno (don't worry about that term yet) and it gives an additional 450 hours if you verify your credit card. Usually Heroku puts your apps hosted for free to sleep after 30 minutes of inactivity but Discords continuous HTTP requests keeps the bot alive indefinitely.


STARTING STEPS

Prerequisites

Have a file named Procfile (with no trailing .txt or anything, just Procfile) added to your main folder, i.e with the index.js file.

Inside the Procfile copy paste this Worker: node index.js

Heroku uses a worker dyno to run your index.js file

If you have you index.js file named as bot.js or anything like that change the Worker accordingly

Important steps before you upload your code to Github

  1. In your index.js folder you may have a bot.login("<TOKEN>")remove that, including the quotes and replace it with process.env.TOKEN
  2. Same with the other tokens you may have. For example if you are using the Tenor API their token should be replaced with process.env.TENOR
  3. While uploading the code to Github you can keep it private or public, it doesn't matter. If you wanna show the world your code keep it public if not keep it private

Your process.env variable can have anything after that, but it needs to be different from the other environment variables

Hosting


Create account page


  • After verifying all your details on Heroku, create a new app


New app page


  • Give it a name and select any region. Don't worry about the pipeline. I have never used it for the bot. The name doesn't matter too.


App name


  • Select Github as the deployment method and connect it


Linking Github


  • Head over to Settings and Config Vars and Reveal Config Vars. This is where all your tokens will go. Replace the VALUE with the token


The config vars


  • After hiding the Config Vars go to Deploy and scroll down and hit DEPLOY. Wait for some time till it shows Your app was successfully deployed


Deploying the repository


  • Go to Resources and refresh the page. It will show something like this if your Procfile was correct.


Procfile worker


  • Use the edit button next to the web npm start dyno and disable it. Now edit the Worker node index.js dyno and enable it. Your end result should be like this


Worker enabled


  • Now go to logs and see if your bot is up or not. If not deploy it again and check logs.


Logs


DONE!!!

This should keep you bot up for 550 hours i.e approximately 23 days per month. To keep it for more link your credit card (they don't charge you) and get 450 more hours per month which is more than enough for a month.

Psst: You can create more than one account on Heroku if you have 2 mails and link both the accounts to the same Github account. So your bot stays up forever.

End note: You can put your Heroku app (the bot in this case) on maintenance mode if you aren't using it. It saves your hours per month.