Keep a Discord bot online 24/7
Your bot runs as long as your PC runs. Once you switch it off, the bot is offline. Here are the ways to keep it online for good, what they cost and where they fall short.
What “online” means for a bot
A Discord bot is a program that holds a permanent connection to the Discord gateway, a kind of open line. Over that line it receives events: new messages, reactions, new members, slash commands. The bot is online exactly as long as the program runs and the line stands.
That gives three requirements for any place your bot runs:
- It runs without pause. Not only when someone opens a web page, but always.
- It restarts after a crash. Every bot crashes at some point, through a bug in the code, a library update or a network hiccup.
- Its data survives the restart. Points, warnings and settings must not disappear.
The options
Your own PC
Good for developing and testing. Unsuitable for running all the time: the bot is gone as soon as you turn off the PC, Windows installs an update or you close the terminal. It also costs performance on the computer you may be streaming from.
A Raspberry Pi or an old laptop at home
Runs continuously and, after buying it, costs only electricity. In return you are the administrator: updates, restarts after a power cut, an SD card that gives up one day and backups you have to remember. If your home internet goes down, the bot is offline. And a bot that should react to Twitch webhooks lacks a public HTTPS address.
Your own VPS
A small virtual server costs a few francs a month and runs in a data centre. You get full control and full responsibility: updating the operating system, setting up a firewall, securing SSH, setting up a process manager such as systemd or pm2, getting certificates, planning backups. If you enjoy that, it is the right place. If you just want to run a bot, you spend a lot of time on things that have nothing to do with the bot.
Free hosts
There are many offers that host bots for free. They nearly always have a catch: services fall asleep after a few minutes without requests, background services are not free at all, an hour quota runs out before the end of the month, or the bot has to be renewed by hand regularly. The guide Free bot hosting takes a close look at the well-known free tiers.
Managed hosting
You hand over the code, and the provider takes care of servers, restarts, storage and backups. It costs something but saves all the server work. On EStage you pay for memory, not per bot: in the smallest 2 GB plan for CHF 4 a month, several bots, an overlay and a database run side by side. What is included is on Discord bot hosting.
What your code can do itself
Wherever the bot runs, a few things in the code make it sturdier:
- Let the library keep the connection. discord.js and discord.py reconnect a dropped gateway connection themselves. Catch errors in your commands so that a single error does not end the whole process.
- Shut down cleanly. When the process receives a
SIGTERM, for example on a restart or a new version, close the connection and save what is still open. - Log to standard output. Then you see the logs where the bot runs, without hunting for log files. In Python set
PYTHONUNBUFFERED=1, or the lines arrive late. - Store data in the right place. On EStage that is
/dataor the database. Everything else in the container is back to its initial state after a restart.
// discord.js: shut down cleanly on a restart
process.on('SIGTERM', async () => {
await client.destroy();
process.exit(0);
});
Which option fits
| Option | Runs without pause | Restart on crash | Backups | Effort for you |
|---|---|---|---|---|
| Your own PC | no | no | yourself | small |
| Raspberry Pi | yes, while power and internet hold | set up yourself | yourself | medium |
| VPS | yes | set up yourself | yourself | large |
| Free host | often not | depends on the provider | rarely | small |
| Managed hosting such as EStage | yes | yes | yes | small |
For a bot your community relies on, the two most important columns are the first two. Everything else can be added later.
Common questions
Why does my bot go offline when I turn off my PC?
Because the bot is a program on your PC that holds a connection to Discord. When the program ends, the connection ends. Discord itself does not run any code for your bot.
Is it enough to keep the bot awake with a ping service?
On free offers that put services to sleep without requests, that is a common trick. Many providers consider it abuse of their free tier and can suspend the service. For a bot your community relies on, it is no foundation.
What is the cheapest way?
For testing, your own PC. For a bot that should run all the time, a small paid place without sleep mode; on EStage from CHF 4 a month for 2 GB, which you can split across several bots.