通过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/orconfig/). - 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:
Double-check that the script path is correct relative to the repo root.worker: node index.js
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_TOKENwith your bot's token from the Discord Developer Portal. - Make sure your code uses
process.env.DISCORD_TOKENto 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_TOKENin 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
botand any required permissions likeSend 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
enginesfield to yourpackage.jsonto 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
- Run your bot locally with the same environment variables to confirm it connects to Discord.
- Double-check that the Procfile is in the repo root, named correctly, and has the right command.
- Push any changes to GitHub, then trigger a new deployment on Heroku.
- Monitor the logs again to see if the error clears up.
内容的提问来源于stack exchange,提问作者SphenX

