Keeping your Discord bot online 24/7: the hosting guide
You've built a Discord bot. It hands out roles neatly, responds to commands or keeps moderation in check, and your community loves it. Until you close your laptop and the bot goes offline. Sound familiar? Then it's time to run your bot somewhere it stays online day and night, without your PC having to be on for it.
In practice, good bot management comes down to four things: a stable place to run, a safely stored token, automatic restarts after a crash and logs you can read back when something goes wrong. Let's walk through all four.
Why your own PC is a bad host
For building and testing your bot, your own computer is fine. For running 24/7 it isn't, and that has little to do with how fast your PC is. The problem is everything around it:
- Updates and reboots. Windows installs an update, your PC reboots, and your bot is gone until you notice. Put your PC to sleep and your bot sleeps with it.
- Power and noise. A gaming PC running 24/7 for a bot of a few megabytes is a waste of your energy bill, and you will definitely hear those fans at night.
- Home internet. Your connection drops occasionally, your router gets a different IP address now and then, and during a provider outage your bot is offline as long as you are.
- Everything on one machine. If your PC crashes mid-game, you drag your bot down with it.
A Raspberry Pi in the utility closet is a fun alternative and can work fine, but it too hangs off your home connection and your power supply. A small spot in a data centre, with redundant power and a fixed, fast connection, solves all of these problems in one go. And no, you don't need a heavy server for that: most bots are actually very light.
Node.js or Python: both fine
The two most popular ways to build a Discord bot are JavaScript with discord.js (runs on Node.js) and Python with discord.py. Functionally it makes little difference for most bots: both can handle commands, process events and talk to the Discord API. Just pick the language you already know.
For hosting, this is what matters most:
- Pin your dependencies. With a
package.json(Node) orrequirements.txt(Python) the server installs exactly the same packages as on your own machine. Without those files, "works on my machine" becomes a recurring conversation. - Know your start command. Something like
node index.jsorpython main.py. That's what the host runs to start your bot, so test it locally before uploading. - Don't overestimate your resources. An average community bot only uses a modest amount of memory. Music bots and bots that cache a lot of data are the exception; those have a bigger appetite for RAM.
Your token is the key to your bot
The bot token you create in the Discord Developer Portal gives full control over your bot. Anyone who gets hold of that token can send messages as your bot, leave servers or spam your community. Tokens leak more often than you'd think, and almost always the same way: they're hardcoded in the code and end up on GitHub. Public repositories are automatically scanned by bad actors; a leaked token is often abused within minutes.
Here's how to do it right:
- Never put your token directly in your code, but in a
.envfile or in the environment variables of your hosting panel. - Make sure that
.envfile is in your.gitignore, so it never ends up on GitHub. - Never print your token in logs. Log files get shared too, for example when you ask for help with a problem.
- Token leaked anyway? Reset it immediately in the Discord Developer Portal. The old token stops working right away.
Also, only give your bot the permissions and intents it really needs. A bot that only hands out roles doesn't need to be an administrator. If something does go wrong, the damage is limited.
Auto-restart: crashes happen, downtime doesn't have to
Every bot crashes sometimes. An unexpected error in your code, a flaky connection to the Discord API, a dependency behaving differently than you thought. On your own PC that means: bot offline until you happen to look. With a good host it doesn't, because the panel keeps an eye on your process and automatically restarts your bot as soon as it stops. Usually the bot is back online within seconds, before anyone in your community even notices.
Fair warning though: auto-restart is a plaster, not a cure. If your bot crashes every five minutes, automatic restarts only mask that there's a real bug in your code. If you see a string of restarts in your panel, dive into the logs and fix the cause. Catch errors properly in your code where you can, so one failed command doesn't take down your whole bot.
Reading logs: boring, but essential
When your bot acts up, logs are your best friend. Through your hosting panel's live console you can see exactly what your bot is up to: when it started, which commands are coming in and which error message appeared right before things went wrong.
A few tips to actually get value out of them:
- Log the important moments: startup, connecting to Discord and every caught error, preferably with a timestamp. When someone complains "the bot wasn't working last night", you can search back with precision.
- Don't log everything. Writing down every incoming message makes your logs unreadable and can also be a privacy risk.
- Take rate-limit warnings seriously. If your bot sends too many requests to the Discord API, it gets throttled and feels sluggish. A loop in your code is often the culprit.
In our knowledge base you'll find guides for uploading and managing your bot through the panel, including where to find the console and logs.
Start for free at MC-Node
The great thing about Discord bots is that they're so light that hosting barely needs to cost anything. At MC-Node it costs nothing at all: the Starter plan for Discord bot hosting is free, at €0.00 per month. Your bot runs on our own hardware in the Previder PDC2 data centre in Hengelo, with NVMe storage and DDoS protection, on a connection that doesn't drop because someone at home turns on the microwave.
Through the web panel you upload your files, set your start command and environment variables (that's where your token belongs) and follow the live console. If your bot crashes, it's automatically restarted. If your bot ever outgrows its clothes, for example because you're running a big music bot, you can always move up to a heavier solution, but for most community bots this is more than enough.
Want your bot online 24/7 by tonight? Start your free bot server via mc-node.net/store/discord-bot-hosting, upload your code and you're done. If it's not for you, you're not tied to anything.