Skip to content

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.

Audience
New to running a server
Last checked
Steps
7
Reading time
4 min

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.

On Hostsrc

App hosting is in beta. Order Hobby or Pro 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

Steps

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

    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:

    main.py
    import os import discordfrom discord.ext import commands intents = discord.Intents.default()intents.message_content = True bot = commands.Bot(command_prefix="!", intents=intents)  @bot.eventasync 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"])
    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.

  2. Step 2: Turn on the Message Content intent

    A bot with commands like !ping reads messages, so Discord must allow it. In the Discord Developer Portal (external site, opens in a new tab), 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.

  3. Step 3: Push the code to GitHub

    Create a repository on GitHub (external site, opens in a new tab) and push the bot to it. Keep any .env file with your token out of it.

    echo ".env" >> .gitignoregit initgit add .git commit -m "First version"git branch -M maingit remote add origin https://github.com/you/your-bot.gitgit 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.

  4. Step 4: Connect the repository

    Sign in at control.hostsrc.com (opens in a new tab) 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.

  5. Step 5: Add the token and deploy

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

    DISCORD_TOKEN=paste-your-token-here
  6. Step 6: Check that the bot logged in

    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.

  7. Step 7: Update the bot

    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.