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
- Order a Discord bot; the Micro plan can be tried for 3 days without a bank card.
- At setup, choose Python, its version, and the file to run (
bot.py). - Upload your project under Files. Do not upload your
venvfolder or__pycache__. - In My bot, the Startup file card:
bot.py. - In the Packages (dependencies) card: type
discord.pyand any other libraries if yourrequirements.txtis not enough. - In the Secret variables (.env) card: add
DISCORD_TOKEN. - 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 inrequirements.txt, then restart.discord.errors.PrivilegedIntentsRequired: enable the required intent in the developer portal.LoginFailure: Improper token: wrong or regenerated token. Update the.envfile.- The bot starts then stops without an error: your script ends (no final
bot.run, or an exception insetup_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 withrequests) in events: they freeze the whole bot. Useasyncio.sleepandaiohttp. - Log errors with the
loggingmodule 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.