Guide
Host a discord.py bot: intents, requirements.txt and startup
This guide builds a discord.py 2.x bot with one slash command and runs it on a Python bot server. Forks such as py-cord and nextcord work the same way with their own imports.
Create the bot
Create the application, copy the token and invite the bot as in the first part of the discord.js guide: the Developer Portal steps are the same for every library.
Write the bot
discord.py>=2.4,<3
python-dotenv>=1.0import os
import discord
from discord import app_commands
from dotenv import load_dotenv
load_dotenv()
intents = discord.Intents.default() # add intents.message_content = True only if you read message text
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
@tree.command(name="ping", description="Check the bot's latency")
async def ping(interaction: discord.Interaction):
await interaction.response.send_message(f"Pong! {round(client.latency * 1000)} ms")
@client.event
async def on_ready():
await tree.sync() # sync once at startup; see the slash commands guide for faster test syncs
print(f"Ready as {client.user} in {len(client.guilds)} servers")
client.run(os.environ["DISCORD_TOKEN"])DISCORD_TOKEN=paste-your-token-hereCalling tree.sync() on every start is fine for a small bot. Larger bots sync only when commands change, because syncing is rate limited.
Put it on the server
1Create a Python bot server
In the panel, create a Discord bot server with the Python (discord.py) option.
2Upload the files
Upload
bot.py,requirements.txtand.envin Files or over SFTP. Do not upload a virtual environment folder.3Set the start file
On Startup, set the file the bot starts from to
bot.py.4Start it
Packages install from
requirements.txton start. The logs should end with your ready line.Ready as MyBot#1234 in 1 servers
Common errors
| Log shows | Fix |
|---|---|
discord.errors.PrivilegedIntentsRequired | Enable the intent in the Developer Portal, or stop requesting it in code. |
discord.errors.LoginFailure: Improper token | The token is wrong or was reset. Copy a new one into .env. |
ModuleNotFoundError: No module named 'discord' | The package is missing from requirements.txt, or the file name is misspelled. |
| Commands do not appear | Sync the tree, then restart Discord. Guild syncs show up right away; see slash commands. |