Bot Uptime

How to Deploy a Python discord.py 2.x Bot with requirements.txt & .env

Python remains one of the most popular languages for Discord bots thanks to discord.py 2.x, clean async/await syntax, and rapid prototyping. Hosting a Python bot cleanly requires just three essentials: an accurate requirements.txt, async-safe code (no blocking requests.get calls inside event loops), and proper secret storage in .env.

1. Create a Clean requirements.txt

On startup, the Python container reads requirements.txt and installs every package using pip. Pin major versions so an unexpected upstream breaking release never breaks your bot after a restart.

Avoid dumping pip freeze from your global Windows environment into requirements.txt — Windows-specific packages like pywin32 will fail on Linux containers. List only the packages your bot genuinely imports.

requirements.txt
discord.py>=2.4.0,<3.0.0
python-dotenv>=1.0.1
aiohttp>=3.9.5

2. Structure a Modern discord.py 2.x Bot with Slash Commands

discord.py 2.x uses app_commands for native Discord slash commands. Using setup_hook() to sync commands cleanly and storing your token in .env guarantees smooth restarts and predictable command registration.

Always use aiohttp instead of the synchronous requests library inside Discord event handlers — synchronous HTTP calls freeze the entire asyncio event loop and can cause Discord Gateway heartbeat disconnects.

main.py
import os
import discord
from discord import app_commands
from discord.ext import commands
from dotenv import load_dotenv

load_dotenv()

class MyBot(commands.Bot):
    def __init__(self):
        intents = discord.Intents.default()
        super().__init__(command_prefix="!", intents=intents)

    async def setup_hook(self):
        await self.tree.sync()

    async def on_ready(self):
        print(f"Logged in as {self.user} (ID: {self.user.id})")

bot = MyBot()

@bot.tree.command(name="status", description="Check bot uptime and latency")
async def status_cmd(interaction: discord.Interaction):
    ms = round(bot.latency * 1000)
    await interaction.response.send_message(f"Bot is online! Latency: {ms}ms")

bot.run(os.environ["DISCORD_TOKEN"])

3. Organizing Larger Bots with Cogs & Persistent SQLite

Once your bot grows beyond 5–10 commands, split features into a cogs/ folder (for example cogs/moderation.py and cogs/utility.py) and load them inside setup_hook() via await self.load_extension("cogs.moderation").

For persistent data like warnings, levels, or server configs, Python ships with sqlite3 built right into the standard library (or use aiosqlite for non-blocking queries). Save your .sqlite file inside a ./data/ directory inside your container so it persists across every restart.

  • Use PYTHONUNBUFFERED=1 or flush=True on prints if you want instant real-time console output.
  • Never call time.sleep() inside async functions — always use await asyncio.sleep().
  • Check Linux filename casing: cogs/Mod.py and cogs/mod.py are different files on Linux!
  • Set PY_FILE in Startup Settings if your entry point is bot.py instead of main.py.

Frequently Asked Questions

Why does pip fail with pywin32 on Linux?

Because pywin32 is Windows-only. Edit requirements.txt and keep only cross-platform libraries your bot actually imports (discord.py, python-dotenv, aiohttp, aiosqlite, etc.).

My slash commands are not showing up in Discord right away.

Global tree.sync() calls can take up to an hour to propagate across all servers. During development, sync directly to your test guild using tree.sync(guild=discord.Object(id=YOUR_GUILD_ID)) for instant updates.

Ready to deploy your bot 24/7?

Launch a custom Discord, Telegram, or WhatsApp container (Python, Node.js, Java, Rust) with Managed Databases and Free AI Agents — or deploy a ready-made Discord bot in 1 click on Bot Uptime.