discord-hosting.cloud
20% Off First MonthSee prices

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.

Updated 8 min read

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

requirements.txt
discord.py>=2.4,<3
python-dotenv>=1.0
bot.py
import 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"])
.env
DISCORD_TOKEN=paste-your-token-here

Calling 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

  1. 1Create a Python bot server

    In the panel, create a Discord bot server with the Python (discord.py) option.

  2. 2Upload the files

    Upload bot.py, requirements.txt and .env in Files or over SFTP. Do not upload a virtual environment folder.

  3. 3Set the start file

    On Startup, set the file the bot starts from to bot.py.

  4. 4Start it

    Packages install from requirements.txt on start. The logs should end with your ready line.

    Ready as MyBot#1234 in 1 servers

Common errors

Log showsFix
discord.errors.PrivilegedIntentsRequiredEnable the intent in the Developer Portal, or stop requesting it in code.
discord.errors.LoginFailure: Improper tokenThe 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 appearSync the tree, then restart Discord. Guild syncs show up right away; see slash commands.