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

通过Heroku部署Discord.js机器人时遭遇500错误,请求技术协助

Hey there, let's break down what's going on with your Discord bot deployment on Heroku and fix it step by step.

问题分析

The error you're seeing (code: 500, path: /gateway/bot) is a Discord API server-side error, but it's often tied to configuration issues on your Heroku setup—especially since you mentioned suspecting your Procfile. Let's rule out the most common culprits first.

1. Fix Procfile Placement & Configuration

Your hunch about the Procfile is totally valid—this is one of the most common deployment pitfalls for Discord bots on Heroku:

  • Location: The Procfile must live in the root directory of your GitHub repo (not inside a subfolder like src/ or config/).
  • Filename: It has to be exactly Procfile (capital P, no file extension like .txt—Heroku won't recognize it if it has one).
  • Content: The line should point directly to your bot's entry script. For example, if your main file is index.js, the Procfile should have:
    worker: node index.js
    
    Double-check that the script path is correct relative to the repo root.

2. Verify Your Discord Token Setup

The error occurs during Client.login(), which almost always means your bot can't access its token properly:

  • Go to your Heroku app's Settings tab, scroll down to Config Vars, and add a new variable named DISCORD_TOKEN with your bot's token from the Discord Developer Portal.
  • Make sure your code uses process.env.DISCORD_TOKEN to fetch the token (never hardcode it in your repo—this is a security risk and will break if you change tokens later).
  • Test this locally first: Set the same environment variable on your machine and run the bot to confirm it connects to Discord without issues.

3. Resolve the Discord API 500 Error

While a 500 error is technically a Discord server issue, there are steps you can take to work around it:

  • Reset Your Bot Token: Head to the Discord Developer Portal, go to your bot's page, click "Reset Token", then update the DISCORD_TOKEN in Heroku's Config Vars. Sometimes old tokens can have weird permission issues.
  • Check Bot Permissions: Ensure your bot has the necessary gateway permissions enabled in the Developer Portal (under "OAuth2 > URL Generator", make sure you've checked bot and any required permissions like Send Messages).
  • Restart Heroku Worker: Sometimes a simple restart fixes transient connection issues. You can do this from Heroku's Resources tab (toggle the worker off then back on) or via the CLI:
    heroku restart worker.1
    

4. Confirm Node.js Version & Dependencies

Discord.js has strict Node version requirements (v16.9+ for v14+, v18+ for v15+):

  • Add an engines field to your package.json to specify the correct Node version, e.g.:
    "engines": {
      "node": "18.x"
    }
    
  • Make sure all your dependencies are up to date and that the Heroku build process isn't throwing any hidden warnings (check the full build log in Heroku's Activity tab).

Quick Validation Steps

  1. Run your bot locally with the same environment variables to confirm it connects to Discord.
  2. Double-check that the Procfile is in the repo root, named correctly, and has the right command.
  3. Push any changes to GitHub, then trigger a new deployment on Heroku.
  4. Monitor the logs again to see if the error clears up.

内容的提问来源于stack exchange,提问作者SphenX

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 14:07:49