This guide moves a discord.py bot from your computer to a server that keeps it online 24/7. It also works for Pycord, Nextcord and disnake, which are forks of discord.py and are hosted the same way. The examples use Zettako's Python Discord bot hosting, but the steps apply to any Python host.
1. Write a requirements.txt
The server needs to know which packages to install. Create requirements.txt in your bot's folder:
discord.py>=2.3
python-dotenvAdd anything else you import, such as aiohttp or aiosqlite. A quick way to check is to create a fresh virtual environment, install from the file, and run the bot.
2. Load the token from the environment
Never hard-code your token. Put it in a .env file:
DISCORD_TOKEN=your-token-hereand load it in your bot:
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
intents = discord.Intents.default()
intents.message_content = True # only if you use prefix commands
bot = commands.Bot(command_prefix="!", intents=intents)
@bot.event
async def on_ready():
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
@bot.command()
async def ping(ctx):
await ctx.send("Pong!")
bot.run(os.getenv("DISCORD_TOKEN"))On Zettako you can also set DISCORD_TOKEN as a startup variable instead of uploading a .env file.
3. Enable the right intents
If your bot reads message text, for example for !ping-style commands, you need the Message Content intent in two places: in your code (intents.message_content = True) and in the Discord Developer Portal under Bot > Privileged Gateway Intents. Missing this is the most common reason a newly hosted bot is online but ignores commands. Slash-command bots usually don't need it.
4. Choose a plan and Python version
Order a Python bot hosting plan and select the Python version you develop with. Async Python bots are light, so a small plan is a good start.
5. Upload your files
Upload your .py files, cogs folder and requirements.txt through the file manager or SFTP. Don't upload your local venv folder or __pycache__: the server installs packages itself.
6. Set the startup file and start
Set the file to run, usually main.py or bot.py, then press start. Packages install from requirements.txt, and you'll see your "Logged in as" message in the console.
7. Syncing slash commands
If you use app commands, sync the command tree with await bot.tree.sync(). Syncing on every start is a common habit, but it can hit Discord's rate limits. A better pattern is an owner-only !sync command that you run after changing commands.
Troubleshooting
- ModuleNotFoundError: the package is missing from
requirements.txt. Remember the package name can differ from the import name:import dotenvcomes frompython-dotenv. - LoginFailure: Improper token: the token variable is empty or wrong.
- PrivilegedIntentsRequired: enable the intent in the Developer Portal, or stop requesting it in code.
- Bot is online but doesn't respond: check the Message Content intent, then check the console for errors in your commands.
Want music? Pair your bot with a Lavalink node using our Lavalink setup guide.