使用Heroku部署discord.py开发的Discord Bot后无法上线求助
Hey there, let's troubleshoot why your discord.py bot isn't going online on Heroku. I’ve dealt with this exact issue dozens of times, so here are the most common fixes to check first:
This is the #1 culprit for deployment failures. Your Procfile needs to be exactly right:
- It must be named
Procfile(capital P, no file extension like .txt) - The line inside should point directly to your bot’s main file, like:
Make sure there are no extra spaces, typos in the filename, or wrong command (it’sworker: python your_bot_filename.pyworker, notweb—web dynos are for websites, not bots).
Heroku doesn’t read local .env files by default, so you need to add your bot token as an environment variable:
- Go to your Heroku app dashboard → Settings → Config Vars
- Add a key like
DISCORD_TOKENand paste your actual bot token as the value - In your code, make sure you’re pulling this variable correctly, e.g.:
No hardcoding the token directly—this is a security risk and will break if you ever regenerate your token.import os from discord.ext import commands bot = commands.Bot(command_prefix="!", token=os.getenv("DISCORD_TOKEN"))
Heroku tells you exactly what’s wrong, you just need to look. To see real-time logs:
- Use the Heroku CLI: run
heroku logs --tailin your terminal (make sure you’re logged into Heroku first) - Or go to your app dashboard → More → View Logs
Common errors to look for: ModuleNotFoundError: Yourrequirements.txtis missing a dependency (like discord.py)LoginFailure: Invalid token: Your token is wrong or not loaded from the environment variableIntentsMissing: You’re using discord.py v2.0+ but haven’t enabled required intents (more on that below)
Heroku uses this to install all your bot’s dependencies. Make sure:
- It includes
discord.pywith a compatible version, e.g.:discord.py>=2.3.0 python-dotenv>=1.0.0 # If you use dotenv locally - No extra blank lines or typos in package names
- You’re not mixing incompatible versions (e.g., don’t use discord.py v1.7 if your code uses v2.0 features like slash commands)
Discord’s API now requires explicit intents for certain actions. If your bot uses message content, member lists, etc.:
- In your code, initialize intents properly:
import discord from discord.ext import commands intents = discord.Intents.default() intents.message_content = True # Required to read message content intents.members = True # Required to fetch server members bot = commands.Bot(command_prefix="!", intents=intents, token=os.getenv("DISCORD_TOKEN")) - Go to the Discord Developer Portal → Your Bot → Privileged Gateway Intents
- Toggle on the intents you need (Message Content Intent, Server Members Intent) and save changes
Sometimes the worker dyno gets turned off automatically:
- Go to your Heroku app dashboard → Resources
- Find the
workerdyno and make sure the toggle is switched on - If it keeps crashing, check the logs for repeated errors (like an infinite loop or unhandled exception in your code)
Go through these steps one by one—9 times out of 10, you’ll spot the issue. If you still get stuck, share the relevant snippets from your Heroku logs and I can help dig deeper!
内容的提问来源于stack exchange,提问作者user11786261

