【发布时间】:2021-10-22 01:28:26
【问题描述】:
这是我第一次做一个真正的 javascript 项目(我之前用过一点 js,但仅此而已)。我正在尝试制作一个不和谐的机器人。我把它托管在heroku上,在部署我的项目时,我遇到了一个我无法解决的错误。以下是日志中的内容:
2021-08-20T13:01:23.972825+00:00 heroku[worker.1]: Starting process with command `node index.js`
2021-08-20T13:01:23.771024+00:00 heroku[web.1]: Starting process with command `npm start`
2021-08-20T13:01:24.646055+00:00 heroku[worker.1]: State changed from starting to up
2021-08-20T13:01:27.027743+00:00 app[worker.1]: (node:4) UnhandledPromiseRejectionWarning: ReferenceError: AbortController is not defined
2021-08-20T13:01:27.027754+00:00 app[worker.1]: at RequestHandler.execute (/app/node_modules/discord.js/src/rest/RequestHandler.js:172:15)
2021-08-20T13:01:27.027754+00:00 app[worker.1]: at RequestHandler.execute (/app/node_modules/discord.js/src/rest/RequestHandler.js:176:19)
2021-08-20T13:01:27.027755+00:00 app[worker.1]: at RequestHandler.push (/app/node_modules/discord.js/src/rest/RequestHandler.js:50:25)
2021-08-20T13:01:27.027755+00:00 app[worker.1]: at async WebSocketManager.connect (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:128:9)
2021-08-20T13:01:27.027755+00:00 app[worker.1]: at async Client.login (/app/node_modules/discord.js/src/client/Client.js:245:7)
2021-08-20T13:01:27.027756+00:00 app[worker.1]: (Use `node --trace-warnings ...` to show where the warning was created)
2021-08-20T13:01:27.028065+00:00 app[worker.1]: (node:4) 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-08-20T13:01:27.028108+00:00 app[worker.1]: (node:4) [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.
我的电脑上已经出现了这个错误,我刚刚改进了 node 的版本,我没有更多的错误。但是,我不知道如何处理heroku。此外,当我修复计算机上的错误时,它似乎可以工作,但是当我输入“!ping”时,机器人没有回答我,不和谐。
所以我有两个问题:
- heroku 上的“UnhandledPromiseRejectionWarning”
- 我的机器人不工作
有人可以帮助我,拜托。
以下是版本:
node : v16.7.0
discord.js : v13.1.0
这是我的代码:
index.js
const { Client, Intents } = require('discord.js');
const client = new Client({
intents: [
Intents.FLAGS.GUILDS
]
});
const prefixCmd = '!';
client.on("ready", () => {
console.log("I'm ready !");
});
client.on("message", msg => {
if(!msg.content.startsWith(prefixCmd) || msg.author.bot) return
const args = msg.content.slice(prefixCmd.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (command === "ping") {
msg.reply("pong");
}
});
client.login("MY TOKEN");
package.json
{
"name": "ha",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"discord.js": "^13.1.0",
"node": "^16.6.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/......"
},
"author": "",
"license": "ISC"
}
【问题讨论】:
-
你确定node版本是v16吗?
-
@user15517071 是的,我确定。当我在终端中写'node -v'时,我得到:v16.7.0
-
您好,这个问题是在使用 Discord.js V13 和较旧的 Node 版本时出现的。首先,不应在依赖项中设置节点,因此请尝试删除它并再次运行它。告诉我这是否行不通。另外,你在本地运行时有同样的问题吗?还是只是在 Heroku 上?
-
嗨@Nico,我刚刚从json文件的依赖项中删除了节点。但我仍然有问题。
-
@Nico,对于“UnhandledPromiseRejectionWarning”的问题,我已经在我的电脑上安装了它。我有节点的版本 14,当我切换到 16 时,我不再有问题了。但是我的机器人无法正常工作,我到处搜索,但找不到原因。这就是我去heroku的原因,因为我认为问题可能来自我的计算机(或路由器)的配置,尤其是端口开放。但是在 heroku 上,我遇到了问题:'UnhandledPromiseRejectionWarning',我不知道如何解决它,特别是因为在我的 json 中指定了我想要版本 16 的节点。
标签: javascript node.js heroku discord.js