Hosting Discord Bot Heroku – There are several resources that discuss creating a Discord bot using the discordpy library, and a few more that discuss hosting on Heroku. However, information appears to be lacking when these two topics are combined.
When I started building the Discord bot, I consolidated all my code into one file titled
Hosting Discord Bot Heroku
. As my programming knowledge and abilities grew, I soon learned about the beauty of gears as a way to organize instructions in matching groups. In my latest project, I decided to implement the dblpy API to receive a webhook when users vote for my bot. The API is easy enough to integrate as a cog in my project, and it looks like this:
Build A Simple Discord Bot In Node.js For Beginners
Gear has basically created a webhook server (in the initialization of the class) with a specific path, authentication token and port. Then, when a POST request is sent to the route, the server will record it and
Function. My goal is to send custom messages in user-voted Discord channels, using this feature.
The first problem I ran into was not knowing where to send the custom message. By default, the data sent by the POST request contains only the voting user id and the voting bot id. There is no way for me to determine which server and channel to respond to using this data.
My second problem is setting up a web server endpoint to receive webhooks. When I test my program locally, I can expose my machine to the internet using ngrok and then send requests to the specified ngrok URL. This is not possible when I deploy my bot to Heroku.
Heroku Alternatives: 2 Awesome Platforms To Consider
Included in the data sent by the POST request is the poll URL query. A typical URL looks like this:
By asking the user to click a link with a query parameter containing the server and channel id, I can easily retrieve that data when I receive a polling event. My implementation looks like this:
With that, I’m ready to host my bot on Heroku. I cleaned up my code and merged my development branch with my master branch, which was then picked up by Heroku’s automatic deployment. I changed the web server endpoint on the Discord Bot List (DBL) to point to where my Heroku app is hosted, instead of pointing to my local machine. I wait for my bot to start and then send a test POST request from DBL. Immediately I started getting errors telling me there were no web processes running.
After researching the error, it appears that my Procfile is misconfigured. The procfile tells Heroku what type of process to run and the file to run that process. I have set my process type as
Discord Rpg Bot With Python
“The Heroku app web process type is special: it’s the only process type that can accept external HTTP traffic from the Heroku router. If your app includes a web server, you must declare it as your app’s web process.”
Clearly, I had just configured my process type incorrectly and all it took was a quick change
I pushed my code to GitHub and tested my DBL voice event again by sending a POST request to my Heroku app endpoint, and it worked! My bot has received the webhook and is responding. However, I wouldn’t be writing an article about the simple solution I reached on my second attempt if there wasn’t a caveat that came with it. And unfortunately, my change
The reason worker processes are recommended for Python bots is because they are always active. This means that employees are always listening to instructions and can respond immediately. Instead, the web process goes to sleep after being enabled for some time.
How To Properly Protect The Bot Token When Hosting On Heroku? (python)
“If the app has a free web dyno and that dyno receives no web traffic for 30 minutes, it will sleep.”
For obvious reasons, this won’t work for Discord bots, which receive traffic through the API and not through the web. At this point I’ve tried many different solutions, from creating my own web server and hosting it outside of Heroku to refactoring my code to always call the dblpy API for vote counting. None of this gave me much success, and I’m back where I started: needing web process HTTP routing, but worker process runtime. How about just running these two processes at the same time?
I push my code and send a test webhook to my app domain. The web process is triggered and sends the appropriate response in the correct channel. Everything looks good there. Next, I tried the command in my Discord chat and waited for the worker process to respond. That works too; however, the work process is not the only one that receives instructions.
Remember how web processes idle for a while after receiving web traffic? During that idle time, it acts like a worker process and can receive commands from the Discord API. This means that for thirty minutes after someone votes, all the following commands entered will get a duplicate response like that:
Deploying A Java Discord Bot Jar On Heroku
I think that the only way to avoid duplicate responses is to divide the process tasks. I composed my copy
. This new file runs a second instance of my Discord bot, but it will only be responsible for the web functionality.
Map. Now the only wheels accessible to the web process are the ones connected to the web server.
And I pushed my new code to GitHub for reuse. I again send my test webhook to my Heroku app and my test command to my Discord server. Everything looked perfect this time, and I was able to use the bot as intended. But unfortunately, I’ve missed a line in the Heroku documentation that would be the downfall of this solution.
Discord Bot Development
The web process basically brings down all other processes. This means that when the web process goes to sleep, the worker process follows.
Shocked by the repeated failures, I had one last idea to push my Discord bot into production: two different Heroku apps. Although the web server is part of the main project, Heroku doesn’t like it being hosted with the whole bot. A simple solution to this is domain separation. One app will handle the main functionality of the bot, while the other will host the web server and be the endpoint for webhooks.
I accomplished this by creating a new Heroku app and connecting it to the same GitHub repository that my original app used. Once both applications pull the same code, I change the process type on each application as follows:
My original Heroku app had the worker process enabled so it could respond to Discord commands and the web process disabled to prevent it from sleeping. Meanwhile, my web server Heroku app works in reverse, with the web process enabled to receive webhooks and the worker process disabled. This new configuration works like a charm because there are no dyno processes interfering with each other or overlapping tracks.
Solving Deceiving Problems Presented By Heroku Dyno Processes
Hopefully this article has given some insight into a problem that I find very frustrating but can be dealt with in the end. There might be a more efficient solution than running two instances of the same bot, and I’d love to hear about it. Feel free to leave any thoughts below or contact me personally through my profile. Share to Twitter Share to LinkedIn Share to Reddit Share to Hacker News Share to Facebook Share to Mastodon
After going through the basic steps to set up a bot in the Discord App Dashboard Build a Discord Bot (Basic Settings) and invite it to our servers.
Build a Discord Bot with Nodejs – Project 01 and our bot is ready to be hosted in the cloud.
Now if you want to be a bit fancy you can also host your bot on a raspberry pi or you can host it on your laptop and set it to run 24/7.
Discord Bot In Typescript Hosted On Heroku
But for simplicity we will host our bot on Heroku in a free tier account. The hosting procedure will remain the same whether you are using the paid tier or the free tier. But in the case of the free tier, your bot has a quota limit of 550 hours/month. So your bot will only stay online for about 22 days in a month. Verifying your account with a credit card will add an additional 450 hours to your quota.
Before we go to Heroku, let’s first jump into our project directory and prepare it for hosting.
A procfile is a file that specifies the commands that the app will execute on startup. That file is required by Heroku. A procfile is always a simple text file called Procfile with no file extension. For example,
Discord bot hosting for free, heroku bot, discord twitter bot, discord bot, free discord bot hosting python, discord bot creator, discord bot development, free discord bot hosting, heroku hosting, discord bot hosting, heroku discord bot, best free discord bot hosting