You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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:

1. Double-Check Your Procfile Setup

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:
    worker: python your_bot_filename.py
    
    Make sure there are no extra spaces, typos in the filename, or wrong command (it’s worker, not web—web dynos are for websites, not bots).
2. Verify Your Discord Token is Set Correctly

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_TOKEN and paste your actual bot token as the value
  • In your code, make sure you’re pulling this variable correctly, e.g.:
    import os
    from discord.ext import commands
    
    bot = commands.Bot(command_prefix="!", token=os.getenv("DISCORD_TOKEN"))
    
    No hardcoding the token directly—this is a security risk and will break if you ever regenerate your token.
3. Dig Into Heroku Logs for Errors

Heroku tells you exactly what’s wrong, you just need to look. To see real-time logs:

  • Use the Heroku CLI: run heroku logs --tail in 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: Your requirements.txt is missing a dependency (like discord.py)
  • LoginFailure: Invalid token: Your token is wrong or not loaded from the environment variable
  • IntentsMissing: You’re using discord.py v2.0+ but haven’t enabled required intents (more on that below)
4. Fix Your Requirements.txt File

Heroku uses this to install all your bot’s dependencies. Make sure:

  • It includes discord.py with 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)
5. Enable Required Intents (discord.py v2.0+)

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
6. Make Sure Your Dyno is Running

Sometimes the worker dyno gets turned off automatically:

  • Go to your Heroku app dashboard → Resources
  • Find the worker dyno 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:31:35