未处理的承诺警告。如何修复?
我有一个机器人,它获取用户消息内容,删除用户消息,然后向 message.author 发送私人消息,然后在嵌入中发送用户消息内容。 几条消息之后,机器人发送了 2 条消息而不是 1 条,并出现警告。有什么办法可以修复它吗?
警告:
superadmin@vps-XXXXXX:~/path$ node test.js (node:1059) UnhandledPromiseRejectionWarning: DiscordAPIError: Unknown Message at RequestHandler.execute (path/node_modules/discord.js/src/rest/RequestHandler.js:170:25) at processTicksAndRejections (internal/process/task_queues.js:97:5) (node:1059) 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: 1) (node:1059) [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.
脚本:
const discord = require('discord.js');
const client = new discord.Client;
client.on('message', message => {
if (message.author === client.user && message.channel.id !== (508728211837026325)) {
return;
}
if (message.channel.id == (508728211837026325)) {
message.delete();
message.author.send("Success!");
message.channel.send({
"embed": {
"color": 61183,
"description": message.content + "\n\nCreated by: " + "<@" + message.author.id + ">",
"author": {
"icon_url": "imgururl;",
"url": "imgururl",
"name": "test",
},
timestamp: new Date()
}
})
}
})
client.login('token');
您描述的行为可能是由于您的机器人同时启动两次而导致的。事实上,如果您的机器人启动两次:
- 您会收到“未知消息”警告,因为该消息已被机器人的另一个实例删除。
- 机器人发送了两次消息。
阅读 这篇文章 ,它应该可以解决您的问题。
该机器人正在被自己激活。
if (message.author === client.user && message.channel.id !== (508728211837026325))
# should probably be
if (message.author === client.user || message.channel.id !== (508728211837026325))
另外需要注意的是,频道 id 是一个 twitter snowflake,可以像 uint64 一样大,对于 javascript 来说,这意味着你无法处理太大的 id,因为限制是 9007199254740991,所以你应该使用字符串版本。
if (message.channel.id === "508728211837026325")