如何解决等待消息中未处理的 Promise 拒绝?

Posted

技术标签:

【中文标题】如何解决等待消息中未处理的 Promise 拒绝?【英文标题】:How to solve unhandled Promise rejection from await messages? 【发布时间】:2021-10-29 04:38:54 【问题描述】:

我在控制台中收到此错误:

(node:33480) UnhandledPromiseRejectionWarning: TypeError: Function.prototype.apply was called on #<Object>, which is 
a object and not a function
    at MessageCollector.handleCollect (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\structures\interfaces\Collector.js:92:53)
    at Client.emit (events.js:327:22)
    at MessageCreateAction.handle (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\event-target.js:132:16)      
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\websocket.js:978:20)  
(node:33480) 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:33480) [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:33480) UnhandledPromiseRejectionWarning: TypeError: Function.prototype.apply was called on #<Object>, which is 
a object and not a function
    at MessageCollector.handleCollect (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\structures\interfaces\Collector.js:92:53)
    at Client.emit (events.js:327:22)
    at MessageCreateAction.handle (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\event-target.js:132:16)      
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\websocket.js:978:20)  
(node:33480) 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)
(node:33480) UnhandledPromiseRejectionWarning: TypeError: Function.prototype.apply was called on #<Object>, which is 
a object and not a function
    at MessageCollector.handleCollect (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\structures\interfaces\Collector.js:92:53)
    at Client.emit (events.js:327:22)
    at MessageCreateAction.handle (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\event-target.js:132:16)      
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\websocket.js:978:20)  
(node:33480) 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: 3)
(node:33480) UnhandledPromiseRejectionWarning: TypeError: Function.prototype.apply was called on #<Object>, which is 
a object and not a function
    at MessageCollector.handleCollect (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\structures\interfaces\Collector.js:92:53)
    at Client.emit (events.js:327:22)
    at MessageCreateAction.handle (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\event-target.js:132:16)      
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\websocket.js:978:20)  
(node:33480) 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: 4)
(node:33480) UnhandledPromiseRejectionWarning: TypeError: Function.prototype.apply was called on #<Object>, which is 
a object and not a function
    at MessageCollector.handleCollect (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\structures\interfaces\Collector.js:92:53)
    at Client.emit (events.js:327:22)
    at MessageCreateAction.handle (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\event-target.js:132:16)      
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\websocket.js:978:20)  
(node:33480) 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: 5)
(node:33480) UnhandledPromiseRejectionWarning: TypeError: Function.prototype.apply was called on #<Object>, which is 
a object and not a function
    at MessageCollector.handleCollect (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\structures\interfaces\Collector.js:92:53)
    at Client.emit (events.js:327:22)
    at MessageCreateAction.handle (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\event-target.js:132:16)      
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\websocket.js:978:20)  
(node:33480) 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: 6)
(node:33480) UnhandledPromiseRejectionWarning: TypeError: Function.prototype.apply was called on #<Object>, which is 
a object and not a function
    at MessageCollector.handleCollect (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\structures\interfaces\Collector.js:92:53)
    at Client.emit (events.js:327:22)
    at MessageCreateAction.handle (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\event-target.js:132:16)      
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\websocket.js:978:20)  
(node:33480) 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: 7)
(node:33480) UnhandledPromiseRejectionWarning: TypeError: Function.prototype.apply was called on #<Object>, which is 
a object and not a function
    at MessageCollector.handleCollect (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\structures\interfaces\Collector.js:92:53)
    at Client.emit (events.js:327:22)
    at MessageCreateAction.handle (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\event-target.js:132:16)      
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\websocket.js:978:20)  
(node:33480) 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: 8)
(node:33480) UnhandledPromiseRejectionWarning: TypeError: Function.prototype.apply was called on #<Object>, which is 
a object and not a function
    at MessageCollector.handleCollect (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\structures\interfaces\Collector.js:92:53)
    at Client.emit (events.js:327:22)
    at MessageCreateAction.handle (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\event-target.js:132:16)      
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\websocket.js:978:20)  
(node:33480) 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: 9)
(node:33480) UnhandledPromiseRejectionWarning: TypeError: Function.prototype.apply was called on #<Object>, which is 
a object and not a function
    at MessageCollector.handleCollect (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\structures\interfaces\Collector.js:92:53)
    at Client.emit (events.js:327:22)
    at MessageCreateAction.handle (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\event-target.js:132:16)      
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\websocket.js:978:20)  
(node:33480) 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: 10)
(node:33480) UnhandledPromiseRejectionWarning: TypeError: Function.prototype.apply was called on #<Object>, which is 
a object and not a function
    at MessageCollector.handleCollect (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\structures\interfaces\Collector.js:92:53)
    at Client.emit (events.js:327:22)
    at MessageCreateAction.handle (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\event-target.js:132:16)      
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\websocket.js:978:20)  
(node:33480) 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: 11)
(node:33480) UnhandledPromiseRejectionWarning: TypeError: Function.prototype.apply was called on #<Object>, which is 
a object and not a function
    at MessageCollector.handleCollect (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\structures\interfaces\Collector.js:92:53)
    at Client.emit (events.js:327:22)
    at MessageCreateAction.handle (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\event-target.js:132:16)      
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (C:\Users\RIJAK\Documents\GitHub\asta-jr\node_modules\ws\lib\websocket.js:978:20)  
(node:33480) 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: 12)

我已将问题缩小到我的代码的这一部分:

for (var x = 0; x < questions.length; x++) 
        message.author
          .send(
            embed: 
              title: `***Q$x + 1. $questions[x]***`,
              description:
                "Please answer questions honestly and if asked about your details, please provide REAL life details",
              footer: 
                text: "You have 1 minute to answer questions!",
              ,
              color: 0xe556f5,
            ,
          )
          .then((msg) => 
            dmCh = msg.channel;
          )
          .catch((err) => 
            message.author.send("Some error occurred");
          );
          dmCh.awaitMessages( max: 1, time: 60000 )
            .then((answer) => 
              // answers.append(answer.toLowerCase());
              console.log(answer);
            )
            .catch((collected) => 
              // console.log(collected);
            );
      

我已经尝试过我的机器人的其他命令,它们工作正常,但是一旦我运行这个命令,它就会给出上述错误,之后,如果我在频道中输入任何消息,它就会给出这个错误。 我还尝试在我的代码周围放置一个 try 和 catch 块,如下所示:

try 
          dmCh.awaitMessages( max: 1, time: 60000 )
            .then((answer) => 
              // answers.append(answer.toLowerCase());
              console.log(answer);
            )
            .catch((collected) => 
              // console.log(collected);
            );
         catch (e) 
          console.log(e);
        

但它不起作用。

请帮帮我!

【问题讨论】:

如果您还没有尝试将 djs 更新到 v13 dmCh = msg.channel; does not work。将msg.channel.awaitMessages(…) 移入 then 回调 @Bergi 我试过了,但我仍然得到这个错误:UnhandledPromiseRejectionWarning: TypeError: Function.prototype.apply was called on #&lt;Object&gt;, which is a object and not a function at MessageCollector.handleCollect 我认为问题是在收集消息时发生的。 【参考方案1】:

问题中显示的代码中唯一看起来可能会产生未处理拒绝的部分是

message.author.send("Some error occurred");

catch 处理程序中。如果这引发错误,它将拒绝来自catch 的承诺,并且该拒绝不会在任何地方得到处理。

您可以通过添加第二个catch 处理程序或在您拥有的catch 处理程序中使用try/catch 来捕获该拒绝:

.catch(err => 
    try 
        message.author.send("Some error occurred");
     catch (e) 
        // ...
    
);

如果send 返回一个承诺,则不会处理该承诺。要同时处理抛出异常来自send 的拒绝,返回send 的结果并添加第二个catch 处理程序:

.catch(err => 
    return message.author.send("Some error occurred");
)
.catch(err => 
    // Do whatever you want to do when the error handling failed
);

但也有可能您正在调用的某个函数正在创建一个它没有传递回您的代码的承诺,并且未能处理对该承诺的拒绝。

【讨论】:

我已经尝试过了,但它仍然显示相同的错误。此外,我尝试注释掉等待消息部分,它停止给出该错误。所以我认为这是有问题的。自 2 小时以来,我一直在尝试解决此问题,但我只是不明白为什么会有拒绝... @RijakSingh - 这表明上面的最后一段适用:dmCh.awaitMessages 正在创建一个它返回的承诺,并且未能处理对该承诺的拒绝。 阅读awaitMessages() 上的docs - 它提供了一些关于消息收集器如何工作的示例代码。 尝试等待.send() 进行错误处理,因为它也会返回一个承诺 @Elitezen 我试过了,现在它没有给出错误并发送嵌入,但是一旦我输入答案,它就会给出以下错误:(node:39544) UnhandledPromiseRejectionWarning: TypeError: Function.prototype.apply was called on #&lt;Object&gt;, which is a object and not a function【参考方案2】:

发生错误是因为您没有提供filter 函数。

这是文档中的一个示例

// Await !vote messages
const filter = m => m.content.startsWith('!vote');

// Errors: ['time'] treats ending because of the time limit as an error
channel.awaitMessages( filter, max: 4, time: 60000, errors: ['time'] )
  .then(collected => console.log(collected.size))
  .catch(collected => console.log(`After a minute, only $collected.size out of 4 voted.`));

DmChannel#awaitMessages() (v13) 上的文档

如果您使用的是 v12

channel.awaitMessages(filter,  max: 4, time: 60000, errors: ['time'] ) //...

【讨论】:

过滤功能后也不行 这个错误还出现吗?文档指向 v13 示例,确保您使用的是 v13 或根据 v12 文档实现过滤器 我认为它正在尝试将对象作为函数调用,因为它位于过滤器参数位置。因此,我相信它们在 v12 中,过滤器必须是第一个参数 非常感谢您的回答,但有没有办法回到 v12?我有点更新了,因为我陷入了困境,现在我的代码坏了。 你可以先npm uninstall discord.js 然后npm install discord.js@&lt;VERSION&gt;。但是最好学习 v13,因为 v12 将来会被弃用

以上是关于如何解决等待消息中未处理的 Promise 拒绝?的主要内容,如果未能解决你的问题,请参考以下文章

如何找出未处理的 Promise 拒绝发生的位置? [复制]

来自本机代码的 mocha 测试中未处理的拒绝

如何使用节点 js 控制台拒绝邮递员中的解析消息

在 JavaScript ES6 中,thenable 的接受如何解决和拒绝?

处理多个承诺拒绝[重复]

在 Angular 中,如何在使用 async/await 时处理 Promise 拒绝