How To Create a Discord Bot in Python (Free, Cloud-Hosted)

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)

  1. Log in to Discord.
  2. Click the + button on the left sidebar.
  3. Choose For me and my friends.
  4. Name it something like Robot Family Server.

3. Create a Bot Account

  1. Go to the Discord Developer Portal.
  2. Click New Application → give it a name (Encourage Bot).
  3. Go to the Bot tab → click Add Bot.
  4. 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

  1. In the Developer Portal, go to OAuth2 > URL Generator.
  2. Under Scopes, select Bot.
  3. Under Bot Permissions, choose what you need (for this tutorial: text permissions like Send Messages and Read Messages).
  4. Copy the generated URL, paste it in your browser, and select your server.
  5. 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.

  1. Log in or sign up at replit.com.
  2. Create a new project → choose Python.
  3. Name it something like encouraged-bot.
  4. 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:

  1. Create a file named .env.
  2. Add: TOKEN=your_bot_token_here
  3. 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

  1. Go to Uptime Robot.
  2. Create a free account.
  3. Add a New Monitor → type HTTP(s).
  4. Paste in your Replit webserver URL.
  5. 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!