生产中的 Discord bot UnhandledPromiseRejectionWarning (HEROKU)

Discord bot UnhandledPromiseRejectionWarning in production ( HEROKU)

我将我的 discord 机器人部署到 Heroku,但它给出了未处理的承诺拒绝错误 这是日志

2021-09-10T20:50:26.000000+00:00 app[api]: Build started by user codertanishq@gmail.com
2021-09-10T20:50:45.558185+00:00 app[api]: Deploy c608c194 by user codertanishq@gmail.com
2021-09-10T20:50:45.558185+00:00 app[api]: Release v11 created by user codertanishq@gmail.com
2021-09-10T20:50:46.000000+00:00 app[api]: Build succeeded
2021-09-10T20:50:46.978335+00:00 heroku[web.1]: State changed from crashed to starting
2021-09-10T20:50:48.877754+00:00 heroku[web.1]: Starting process with command `npm start`
2021-09-10T20:50:51.529490+00:00 app[web.1]: 
2021-09-10T20:50:51.529505+00:00 app[web.1]: > start@1.0.0 start /app
2021-09-10T20:50:51.529506+00:00 app[web.1]: > node ./src/bot.js
2021-09-10T20:50:51.529506+00:00 app[web.1]: 
2021-09-10T20:50:51.762757+00:00 app[web.1]: (node:22) UnhandledPromiseRejectionWarning: ReferenceError: AbortController is not defined
2021-09-10T20:50:51.762758+00:00 app[web.1]:     at RequestHandler.execute (/app/node_modules/discord.js/src/rest/RequestHandler.js:172:15)
2021-09-10T20:50:51.762758+00:00 app[web.1]:     at RequestHandler.execute (/app/node_modules/discord.js/src/rest/RequestHandler.js:176:19)
2021-09-10T20:50:51.762758+00:00 app[web.1]:     at RequestHandler.push (/app/node_modules/discord.js/src/rest/RequestHandler.js:50:25)
2021-09-10T20:50:51.762759+00:00 app[web.1]:     at async WebSocketManager.connect (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:128:9)
2021-09-10T20:50:51.762759+00:00 app[web.1]:     at async Client.login (/app/node_modules/discord.js/src/client/Client.js:245:7)
2021-09-10T20:50:51.762759+00:00 app[web.1]: (Use `node --trace-warnings ...` to show where the warning was created)
2021-09-10T20:50:51.763064+00:00 app[web.1]: (node:22) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
2021-09-10T20:50:51.763091+00:00 app[web.1]: (node:22) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
2021-09-10T20:50:51.847328+00:00 heroku[web.1]: Process exited with status 0
2021-09-10T20:50:51.902780+00:00 heroku[web.1]: State changed from starting to crashed

Discord.js v13 需要 AbortController 才能运行,但它仅在节点版本 16 及更高版本中引入。您有两种选择来修复此错误:

  • 将您在 heroku 上的节点版本升级到 16 或更高版本

  • 为你的 AbortController 使用一个 polyfill,AbortController-polyfill 是一个包,你可以像这样使用它:

const { AbortController } = require('abortcontroller-polyfill/dist/cjs-ponyfill');
global.AbortController = AbortController;

您必须升级您的节点版本。您可以将以下内容添加到您的 package.json:

"engines": {
    "node": "16.9.1",
  }

然后重新部署您的项目,它就会工作