Skip to content

How to run a Discord bot in Python on hosting

Run a Python Discord bot (discord.py) on a Hostero server: the bot's files, the token in .env, libraries from requirements.txt, pyproject.toml and uv.

A Python bot server installs the libraries and starts the bot's file on every start. All you need to upload is the code, the token and the list of libraries. The example uses discord.py (opens in a new tab), but you run a bot in another library, such as nextcord or py-cord, the same way.

The client panel is available in Polish only, so this article gives its labels in Polish with a translation the first time they appear.

Before you start

You need a bot created in the Discord Developer Portal and its token. How to create a Discord bot shows how to get them.

The bot's files

In the Pliki (Files) tab, create three files: click Nowy plik (New file), paste the content, click Stwórz plik (Create file) and enter the name. You can upload files from your computer with Wgraj plik (Upload file) or over SFTP.

The token in a .env file

Keep the token in a .env file, not in the code. That way it doesn't leak when you show someone the code or push it to GitHub.

.env
TOKEN=<your-token>

The bot's code

An app.py file with a bot that logs in to Discord and answers the /ping command:

app.py
import os

import discord
from discord import app_commands
from dotenv import load_dotenv

load_dotenv()


class MyBot(discord.Client):
    def __init__(self):
        super().__init__(intents=discord.Intents.default())
        self.tree = app_commands.CommandTree(self)

    async def setup_hook(self):
        await self.tree.sync()


bot = MyBot()


@bot.event
async def on_ready():
    print(f"Logged in as {bot.user}")


@bot.tree.command(name="ping", description="Checks that the bot responds")
async def ping(interaction: discord.Interaction):
    await interaction.response.send_message(f"Pong! {round(bot.latency * 1000)} ms")


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

The list of libraries

A requirements.txt file with the libraries, one per line. python-dotenv loads the token from the .env file.

requirements.txt
discord.py
python-dotenv

Starting the bot

Click Uruchom (Start) in the Konsola (Console) tab. The server installs the libraries from requirements.txt and runs app.py. When you see Logged in as, the bot is online on Discord.

Server settings

In the Parametry startowe (Startup parameters) tab, you can change how the server runs the bot:

  • Wersja środowiska (Obraz Docker) (Environment version): the Python version, from 2.7 to 3.12. discord.py needs at least 3.8. The change takes effect after the server restarts.
  • Plik aplikacji py (App .py file): the file the server runs. app.py by default. If your bot starts from main.py or bot.py, enter that name here.
  • Plik zależności (Dependencies file): the file with the list of libraries. requirements.txt by default.
  • Dodatkowe pakiety Python (Extra Python packages): libraries to install without a dependencies file, separated by spaces, such as requests aiohttp.

Libraries

Library versions

On every start, the server updates the libraries to the newest versions the dependencies file allows. If you don't give a version, a new release of a library can break the bot after a restart. Pin the versions you've tested:

requirements.txt
discord.py==2.7.1
python-dotenv==1.2.4

Regenerating requirements.txt

If you write the bot on your computer in a virtual environment (venv), generate the list of libraries with the versions you have installed:

pip freeze > requirements.txt

Run this command in the environment of this bot only. In the global Python, every library on your computer ends up in the file. Then upload the new requirements.txt in the Pliki tab and restart the server.

pyproject.toml and uv

The server installs libraries only from a file in the requirements.txt format. If the project uses pyproject.toml and uv (opens in a new tab), generate a requirements.txt from it on your computer:

uv export --format requirements.txt --no-dev --no-emit-project --output-file requirements.txt

Upload this file together with the code. Generate it again after every change to the dependencies in pyproject.toml.

Common problems

ModuleNotFoundError: No module named …

A library is missing. Add it to requirements.txt or to the Dodatkowe pakiety Python field and restart the server. The name to install sometimes differs from the name in import, such as import dotenv, which you install as python-dotenv.

discord.errors.LoginFailure: Improper token has been passed

The token is wrong or was reset. Generate a new one in the Developer Portal and enter it in .env after TOKEN=, without quotes or spaces.

PrivilegedIntentsRequired

The code asks for an intent that isn't turned on in the portal, such as message_content. Turn it on in the Developer Portal, in the Bot tab, under Privileged Gateway Intents.

The server runs the wrong file or says there's no app.py

Check the Plik aplikacji py field in Startup parameters. It must contain the name of the file the bot starts from.

The /ping command doesn't show up on the Discord server

The bot registers commands when it starts. Refresh Discord with Ctrl + R. If they still aren't there, invite the bot again with a link that has the applications.commands scope.