# How to host a discord.py bot

Put the bot in a GitHub repository with requirements.txt, read the token from a variable, connect the repository and press Deploy.

Source: https://hostsrc.com/guides/host-discord-py-bot. Last checked 2026-12-01, the day someone last followed the steps.

To host a discord.py bot, put its code in a GitHub repository with a `requirements.txt` file, read the token from a variable, connect the repository in the control panel and press **Deploy**. We detect Python, install the requirements and start `main.py`.

This guide is for a bot you write in Python with discord.py. For a bot someone else wrote, in any language, see [how to put a Discord bot online with App hosting](/guides/discord-bot-app-hosting).

> **On Hostsrc:** App hosting is in beta. [Order Hobby or Pro](/app-hosting#pricing) and **Apps** shows in the panel once your payment is confirmed. A bot at 150 MB of RAM, 2% of a vCPU and 1 GB of disk costs about €0.90 a month, inside the Hobby plan's €5/month, which includes €5 of usage. Final price, no VAT added.

## Before you start

- A Hostsrc account with App hosting turned on. It's in beta, so [order Hobby or Pro](/app-hosting#pricing) first.
- Your bot's application in the [Discord Developer Portal](https://discord.com/developers/applications), with its token.
- A GitHub account.

## Steps

### Step 1: Put the bot in main.py and list its packages

Link to this step: https://hostsrc.com/guides/host-discord-py-bot#step-1

We start `main.py` from the top folder of the repository. Next to it, `requirements.txt` lists the packages to install, one per line. The bot reads its token from the `DISCORD_TOKEN` variable, never from the code.

This small bot answers `!ping` with `Pong`:

```text title="main.py"
import os

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.message_content = True

bot = commands.Bot(command_prefix="!", intents=intents)


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


@bot.command()
async def ping(ctx):
    await ctx.send("Pong")


bot.run(os.environ["DISCORD_TOKEN"])
```

```text title="requirements.txt"
discord.py
```

> **Note:** If your bot starts from another file, set **Start command** in **Settings**, under **Deploy**, for example `python bot.py`. To choose the Python version, add a `.python-version` file that holds a version such as `3.12`.

### Step 2: Turn on the Message Content intent

Link to this step: https://hostsrc.com/guides/host-discord-py-bot#step-2

A bot with commands like `!ping` reads messages, so Discord must allow it. In the [Discord Developer Portal](https://discord.com/developers/applications), open your application, then **Bot**. Under **Privileged Gateway Intents**, turn on **Message Content Intent** and save.

A bot that uses only slash commands doesn't need it. Then remove `intents.message_content = True` from the code.

### Step 3: Push the code to GitHub

Link to this step: https://hostsrc.com/guides/host-discord-py-bot#step-3

Create a repository on [GitHub](https://github.com/new) and push the bot to it. Keep any `.env` file with your token out of it.

```text
echo ".env" >> .gitignore
git init
git add .
git commit -m "First version"
git branch -M main
git remote add origin https://github.com/you/your-bot.git
git push -u origin main
```

> **Warning:** The token is the bot's password. If it was ever pushed to a repository, press **Reset Token** on the **Bot** page and use the new one.

### Step 4: Connect the repository

Link to this step: https://hostsrc.com/guides/host-discord-py-bot#step-4

Sign in at [control.hostsrc.com](https://control.hostsrc.com) with your Hostsrc account. In the header, switch to **Apps**.

On **Projects**, press **New**, then **Connect GitHub**, and choose **GitHub**. GitHub asks which repositories to share: allow your bot's repository. Back in Apps, press **New** again, choose **GitHub Repository** and pick it.

The panel makes a project named after the repository, with the bot in it as a service. The service opens on **Variables**.

### Step 5: Add the token and deploy

Link to this step: https://hostsrc.com/guides/host-discord-py-bot#step-5

On **Variables**, press **New variable**. Type `DISCORD_TOKEN` as the name and paste the token as its value. Press **Save variables**, then **Deploy**.

```text
DISCORD_TOKEN=paste-your-token-here
```

### Step 6: Check that the bot logged in

Link to this step: https://hostsrc.com/guides/host-discord-py-bot#step-6

Open **Deployments** and wait for **ACTIVE**. Then open **Logs**. The line from `on_ready` shows there, such as `Logged in as YourBot#1234`. What `print` writes shows in the logs straight away.

Type `!ping` in your Discord server. The bot answers `Pong`.

A bot needs no web address. The service shows **Private service**, and that is right for a bot.

### Step 7: Update the bot

Link to this step: https://hostsrc.com/guides/host-discord-py-bot#step-7

Every push to the repository's branch installs the requirements again and restarts the bot with the new code. To go back to an earlier version, open **History** on **Deployments**, choose **Rollback to this** on an earlier deployment and confirm with **Roll back**.

## Troubleshooting

### The logs show KeyError: 'DISCORD_TOKEN'

The bot can't find the variable. Check that its name on **Variables** is exactly `DISCORD_TOKEN`, capitals included, then press **Save variables** and **Deploy**.

### The logs show LoginFailure

Discord refused the token. Copy it again from the **Bot** page, or press **Reset Token** for a new one. Paste the whole token with no spaces, save the variables and deploy.

### The logs show PrivilegedIntentsRequired

The code asks for the Message Content intent, but Discord hasn't allowed it. Turn on **Message Content Intent** in the Developer Portal (step 2), then press **Restart**.

### The bot is online but ignores !ping

Check that **Message Content Intent** is on in the Developer Portal and that the code sets `intents.message_content = True`. Without both, the bot can't read the messages it answers.

### The logs show ModuleNotFoundError: No module named 'discord'

The packages weren't installed. Check that `requirements.txt` is in the top folder of the repository, next to `main.py`, and lists `discord.py`. Push the change.

## Related guides

- [How to put a Discord bot online with App hosting](https://hostsrc.com/guides/discord-bot-app-hosting)
- [How to deploy an app you built with AI](https://hostsrc.com/guides/deploy-ai-built-app)
- [How to deploy a Next.js app from GitHub](https://hostsrc.com/guides/deploy-nextjs-app-from-github)

## Links

- [Discord Developer Portal](https://discord.com/developers/applications): your bot's application, its token and its intents
- [discord.py documentation](https://discordpy.readthedocs.io/): the library's reference and examples
- [App hosting](https://hostsrc.com/app-hosting): plans and prices, with a link to order each plan
