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
- A Hostsrc account with App hosting turned on. It's in beta, so order Hobby or Pro first.
- Your bot's application in the Discord Developer Portal (external site, opens in a new tab), with its token.
- A GitHub account.
Steps
Step 1: Put the bot in main.py and list its packages
We start
main.pyfrom the top folder of the repository. Next to it,requirements.txtlists the packages to install, one per line. The bot reads its token from theDISCORD_TOKENvariable, never from the code.This small bot answers
!pingwithPong: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.pyNote
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-versionfile that holds a version such as3.12.Step 2: Turn on the Message Content intent
A bot with commands like
!pingreads 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 = Truefrom the code.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
.envfile 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 mainWarning
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
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.
Step 6: Check that the bot logged in
Open Deployments and wait for ACTIVE. Then open Logs. The line from
on_readyshows there, such asLogged in as YourBot#1234. Whatprintwrites shows in the logs straight away.Type
!pingin your Discord server. The bot answersPong.A bot needs no web address. The service shows Private service, and that is right for a bot.
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.
Links
- Discord Developer Portal (external site, opens in a new tab)
your bot's application, its token and its intents
- discord.py documentation (external site, opens in a new tab)
the library's reference and examples
- App hosting
plans and prices, with a link to order each plan
App hosting
Hobby €5/month
Includes €5 of usage every month. In beta. Final price, no VAT added.
Choose a planDiscord
Ask other admins and our team.
Join our Discord (external site, opens in a new tab)Related guides
How to put a Discord bot online with App hosting
Add the bot's repository, save its token as a variable and press Deploy. A bot needs no web address. App hosting is in beta.
8 steps6 min readNew to running a server
How to deploy an app you built with AI
Push the code to GitHub, connect the repository once, add the app's variables and press Deploy. Every push after that deploys by itself.
7 steps5 min readNew to running a server
How to deploy a Next.js app from GitHub
Connect the repository and press Deploy. We run your build and start scripts, next build and next start, and every push to GitHub deploys by itself.
6 steps3 min readNew to running a server