Discord bots2 minutes to read

How do I host a discord.py bot 24/7?

Run a discord.py bot around the clock without your PC: project layout, requirements.txt, token variable, start command and the checks that prove it works.

Use a Python bot host with an always-on process

Upload your bot to a host that keeps a Python process running and installs packages from requirements.txt. On Bothosting, choose the discord.py profile, upload main.py and requirements.txt, save the token as BOT_TOKEN and start it. Spark runs it for free with 256 MB RAM, renewed every 7 days. Paid plans start at $2.25 a month with no renewal.

Lay out the project

Project root
main.py
requirements.txt
cogs/
  moderation.py
requirements.txt
discord.py>=2.4
main.py · read the token from the environment
import os
import discord

client = discord.Client(intents=discord.Intents.default())

@client.event
async def on_ready():
    print(f'Logged in as {client.user}', flush=True)

client.run(os.environ['BOT_TOKEN'])

Start it and prove it runs without you

  1. Create a deployment with the discord.py profile and upload the files to the root.
  2. Save the token from the Discord Developer Portal as BOT_TOKEN in Variables.
  3. Start the deployment. Console should show Logged in as followed by your bot’s name.
  4. Stop any copy running on your PC, close the laptop and run a command from your phone.

Using flush=True makes prints appear in Console immediately. See why Python logs appear all at once.

Keep data across restarts

Files the bot writes to its own directory persist across restarts, so SQLite works for per-server settings. Use a database deployment when several processes need the same data. See SQLite in a hosted Python bot. To deploy every push, connect a GitHub branch.