You have built a Discord bot using Python or Node.js, but you need a place to run it 24/7 without paying for a server. Replit is a cloud-based development platform that lets you write and execute code directly in your browser. It offers a free tier that can keep your bot running as long as you take a few precautions. This article explains how to set up a Discord bot on Replit, keep it online, and avoid common pitfalls that cause bots to stop working.
Key Takeaways: Hosting a Discord Bot on Replit
- Replit Secrets tab: Store your bot token securely without exposing it in your code.
- Uptime Robot monitoring: A free external service that pings your Replit bot every 5 minutes to prevent sleep.
- Always-on Replit deployment: Use the “Always On” feature in your Replit account settings to keep the repl running 24/7.
How Replit Runs Your Discord Bot
Replit is an online IDE that executes your code on its servers. When you create a repl for Python or Node.js, Replit provides a virtual machine with a public URL. Your Discord bot code runs inside that machine. The free tier has a critical limitation: if you close the browser tab or leave the repl idle for about 30 minutes, Replit puts the machine to sleep. A sleeping repl stops your bot from responding to commands. To keep the bot awake, you need either the paid “Always On” feature or a free external monitoring service like Uptime Robot that sends periodic HTTP requests to your repl’s public URL.
Prerequisites Before You Start
Before you begin, make sure you have the following ready:
- A Discord account and a server where you have permission to add bots.
- A Discord bot application created at the Discord Developer Portal. You need the bot token.
- A Replit account (free tier is sufficient).
- Basic familiarity with your bot’s programming language (Python or Node.js).
Steps to Host Your Discord Bot on Replit
- Create a new repl on Replit
Log in to Replit and click the “Create” button. Choose the language that matches your bot code, for example Python or Node.js. Give your repl a descriptive name such as “my-discord-bot”. Click “Create Repl”. - Upload or paste your bot code
If your bot code exists as files, drag and drop them into the file panel on the left. Alternatively, paste your main bot file content directly into the editor. The main file is usually namedmain.pyfor Python orindex.jsfor Node.js. - Add the bot token as a secret
In the Replit sidebar, click the lock icon to open the Secrets tab. Click “New Secret”. In the key field, typeDISCORD_TOKEN. In the value field, paste your bot token exactly as it appears in the Discord Developer Portal. Click “Add Secret”. Never paste the token directly into your code. - Modify your code to read the token from the environment
In your bot code, replace any hardcoded token string withos.getenv('DISCORD_TOKEN')in Python orprocess.env['DISCORD_TOKEN']in Node.js. This keeps the token secure and outside your source files. - Add a keep-alive web server
Replit requires a web server to respond to health checks. Create a new file namedkeep_alive.py(Python) orkeep_alive.js(Node.js) with a simple HTTP server that listens on port 8080. In Python, use the Flask library. In Node.js, use the built-in http module. The server does nothing except return “OK” when accessed. - Start the web server in your main file
At the beginning of your main bot file, import and start the keep-alive server. In Python, addfrom keep_alive import keep_aliveand thenkeep_alive()beforeclient.run(). In Node.js, require the keep-alive file at the top ofindex.js. - Run the repl
Click the green “Run” button at the top of the Replit editor. The console will show your keep-alive server starting and your bot logging in. You should see your bot appear online in your Discord server. - Set up Uptime Robot to keep the bot awake
Go to uptimerobot.com and create a free account. Click “Add New Monitor”. Select “HTTP(s)” as the monitor type. In the URL field, paste the public URL of your Replit repl. You can find this URL in the Replit browser panel after running the repl. It looks likehttps://my-discord-bot.yourusername.repl.co. Set the monitoring interval to 5 minutes. Click “Create Monitor”. Uptime Robot will now ping your repl every 5 minutes, preventing Replit from sleeping.
If Your Bot Still Goes Offline
Bot disconnects after a few hours
The free Replit tier has a usage limit. If your bot consumes too many resources, Replit may stop the repl. Check the Replit console for memory or CPU warnings. Reduce your bot’s resource usage by limiting the number of commands or using a lighter library like discord.py without voice support.
Uptime Robot monitor shows “Down”
If Uptime Robot reports your repl as down, the keep-alive server may have crashed. Open your repl and check the console for errors. Common mistakes include missing the Flask library in Python or a port conflict. Make sure your keep-alive server listens on port 8080, not port 80.
Bot token leaks or is invalid
If your bot fails to log in, verify that the token in Replit Secrets is correct. Regenerate the token in the Discord Developer Portal if you suspect it was compromised. Update the secret in Replit immediately.
Replit changes the public URL
Each time you rename your repl or change its visibility, Replit issues a new URL. Update the URL in Uptime Robot to match the new address. Otherwise, Uptime Robot will keep pinging the old URL and your bot will sleep.
| Item | Free Replit Tier | Paid Replit Tier |
|---|---|---|
| Cost | $0 per month | $7 per month (Hacker plan) |
| Always On | No, requires Uptime Robot | Yes, built-in |
| CPU and RAM limits | Limited, may throttle | Higher limits, no throttling |
| Private repls | Public only | Private repls available |
| Best for | Small bots with low activity | Bots with moderate to high activity |
You can now host your Discord bot on Replit without paying for a server. The free tier works well for small bots and testing, but you need Uptime Robot to keep the bot awake. For production bots that must stay online 24/7, consider upgrading to the Replit Hacker plan for the built-in Always On feature. As an advanced tip, set up a second Uptime Robot monitor pointing to your keep-alive server’s health endpoint so you receive email alerts if the bot goes down.