Blog Bots 3 min read

Host a Discord bot in Python (discord.py) 24/7

Put a Python Discord bot online with discord.py: requirements.txt, token in a .env file, intents, sending files, Python version and automatic restart.

On this page
  1. 1. Prepare the project
  2. 2. The token outside the code
  3. 3. Putting the bot online at Artaun
  4. 4. Common errors
  5. 5. Keeping it online
  6. 6. Data and database
  7. Frequently asked questions

To host a Discord bot in Python 24 hours a day, you need a server that runs your script continuously with the right version of Python, installs the libraries listed in requirements.txt and reads the token from an environment variable. With a bot host, you upload your files, specify the script to run (bot.py or main.py), and it is online.

1. Prepare the project

mon-bot/
  bot.py
  requirements.txt
  cogs/
  .env        (jamais publié)

requirements.txt contains one library per line:

discord.py
python-dotenv

To pin the versions that work for you: pip freeze > requirements.txt from your virtual environment (then remove the unnecessary lines).

2. The token outside the code

# bot.py
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv

load_dotenv()
intents = discord.Intents.default()
intents.message_content = True

bot = commands.Bot(command_prefix="!", intents=intents)

@bot.event
async def on_ready():
    print(f"Connecté en tant que {bot.user}")

bot.run(os.getenv("DISCORD_TOKEN"))

In .env: DISCORD_TOKEN=your_token. Enable the Message Content intent in the Discord developer portal (Bot tab) if your bot reads messages, otherwise it refuses to start.

3. Putting the bot online at Artaun

  1. Order a Discord bot; the Micro plan can be tried for 3 days without a bank card.
  2. At setup, choose Python, its version, and the file to run (bot.py).
  3. Upload your project under Files. Do not upload your venv folder or __pycache__.
  4. In My bot, the Startup file card: bot.py.
  5. In the Packages (dependencies) card: type discord.py and any other libraries if your requirements.txt is not enough.
  6. In the Secret variables (.env) card: add DISCORD_TOKEN.
  7. Start, then check in the Console for the message Connecté en tant que ….

4. Common errors

  • ModuleNotFoundError: a library is missing. Add it under Packages or in requirements.txt, then restart.
  • discord.errors.PrivilegedIntentsRequired: enable the required intent in the developer portal.
  • LoginFailure: Improper token: wrong or regenerated token. Update the .env file.
  • The bot starts then stops without an error: your script ends (no final bot.run, or an exception in setup_hook). Read the last lines of the console.

The diagnostic under More options › Game tools › Analyse recognises most of these errors.

5. Keeping it online

  • Add Automatic start under More options › Automatic tasks.
  • Avoid blocking functions (time.sleep, synchronous HTTP requests with requests) in events: they freeze the whole bot. Use asyncio.sleep and aiohttp.
  • Log errors with the logging module so you can find them in the console.

6. Data and database

For a small bot, an SQLite file in the bot's folder is enough. For more, create a MySQL database under More options › Databases and connect with aiomysql or asyncmy. Remember backups (the Backups page): your data is your responsibility.

Frequently asked questions

discord.py or another library? discord.py is the most widely used and maintained. Alternatives exist (py-cord, nextcord, disnake); the hosting method is the same.

How much memory for a Python bot? A simple bot fits in 256 to 512 MB; plan for 1 GB or more if you load models, images or many servers.

Can I host several bots? One service per bot is the simplest to manage: each has its own console, restarts and .env.

Your bot deserves better than a PC left on overnight: Discord bot hosting, from 0.99 € per month. Node.js version: host a discord.js bot 24/7.

By Équipe Artaun