This guide walks you through building a simple Discord bot using Python, hosting it for free with Replit, and keeping it online continuously with Uptime Robot. No software installation is needed—everything runs in your browser.
1. What is a Discord Bot?
A Discord bot is like a user in your server, but controlled by code instead of a person. Bots can:
- Respond to messages
- Moderate servers
- Play music
- Post updates from external services (like GitHub)
- …or do whatever you program them to do!
We’ll build a bot called Encourage Bot, which responds to certain messages with positive encouragements.
2. Create a Discord Server (if you don’t have one)
- Log in to Discord.
- Click the + button on the left sidebar.
- Choose For me and my friends.
- Name it something like Robot Family Server.
3. Create a Bot Account
- Go to the Discord Developer Portal.
- Click New Application → give it a name (Encourage Bot).
- Go to the Bot tab → click Add Bot.
- Copy the Bot Token — this is like a password. Keep it secret.
- You can regenerate it if it gets exposed.
4. Invite the Bot to Your Server
- In the Developer Portal, go to OAuth2 > URL Generator.
- Under Scopes, select Bot.
- Under Bot Permissions, choose what you need (for this tutorial: text permissions like Send Messages and Read Messages).
- Copy the generated URL, paste it in your browser, and select your server.
- Authorize the bot → it will appear in your server (offline for now).
5. Set Up Replit
We’ll host the bot in Replit, an online coding environment.
- Log in or sign up at replit.com.
- Create a new project → choose Python.
- Name it something like encouraged-bot.
- You’ll see:
- main.py (where we’ll write code)
- Console (to see output and run the bot)
- Files panel (for extra files)
6. Install and Import Discord.py
In main.py
:
import discord
client = discord.Client()
When you press Run, Replit auto-installs dependencies like discord.py
.
7. Add Basic Bot Events
Bots work using events. For example:
- When the bot is ready:
@client.event
async def on_ready():
print(f"We have logged in as {client.user}")
- When the bot receives a message:
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith("$hello"):
await message.channel.send("Hello!")
8. Secure Your Token
Never paste the token directly in your code. Use a .env
file:
- Create a file named
.env
. - Add:
TOKEN=your_bot_token_here
- In
main.py
:
import os
TOKEN = os.getenv("TOKEN")
client.run(TOKEN)
9. Add Functionality
You can extend your bot with features like:
- Encouragements (stored in Replit’s built-in database)
- Add/Delete Commands (
$new
to add a message,$del 1
to remove one) - Random Quotes (using an API)
Example:
starter_encouragements = ["Cheer up!", "You can do it!", "Don’t give up!"]
@client.event
async def on_message(message):
if message.author == client.user:
return
if any(word in message.content for word in ["sad", "depressed"]):
await message.channel.send(random.choice(starter_encouragements))
10. Keep the Bot Running
By default, Replit bots stop when you close your browser. To fix this:
Step 1: Create a Web Server
Make a file keep_alive.py
:
from flask import Flask
from threading import Thread
app = Flask('')
@app.route('/')
def home():
return "Hello, I’m alive!"
def run():
app.run(host="0.0.0.0", port=8080)
def keep_alive():
t = Thread(target=run)
t.start()
In main.py
:
from keep_alive import keep_alive
keep_alive()
client.run(TOKEN)
Step 2: Use Uptime Robot
- Go to Uptime Robot.
- Create a free account.
- Add a New Monitor → type HTTP(s).
- Paste in your Replit webserver URL.
- Set it to ping every 5 minutes.
Now your bot stays online 24/7!
11. Bonus: Webhooks
You can connect bots to external services (like GitHub). In Discord:
- Go to Server Settings > Integrations > Webhooks.
- Copy the webhook URL.
- Add it to GitHub (or another service) → now updates get posted to Discord automatically.
Conclusion
You just built and deployed a Discord bot that:
✔ Responds to messages
✔ Stores and retrieves data
✔ Runs continuously online for free
✔ Can integrate with external services
From here, you can expand it with moderation tools, music, games, or anything you dream up!