首页 文章

无法在承诺中捕获UnhandledPromiseRejectionWarning

提问于
浏览
1

所以我试图在我的承诺中捕获UnhandledPromiseRejectionWarning,但由于某种原因它无法正常工作 . 它忽略了我的代码,只是将错误输出到控制台 .

错误:

UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):错误:禁止(无法向此用户发送消息)

码:

e.message.author.openDM().then((message) => {
    message.sendMessage(`test`);
}).catch((error) => {
    e.message.channel.sendMessage(error + "test");
});

这是一个使用discordie的不和谐机器人 . 在我看来,如果机器人无法执行,上面的代码应该通过私人消息将消息作者发送给消息作者"test" .
tl; dr,如果机器人没有dm用户的权限,上面代码中的catch不会在控制台中出现上述错误 .

1 回答

  • 4

    你忘记了 then 函数中的 return 语句 . 我想 message.sendMessage('test') 会返回一个承诺

    e.message.author.openDM().then((message) => {
        return message.sendMessage(`test`);
    }).catch((error) => {
        e.message.channel.sendMessage(error + "test");
    });
    

相关问题