无法在承诺中捕获 UnhandledPromiseRejectionWarning

Cannot catch UnhandledPromiseRejectionWarning in promise

所以我试图在我的承诺中捕获 UnhandledPromiseRejectionWarning,但由于某种原因它不起作用。它忽略了我的代码,只是将错误输出到控制台。

错误:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Forbidden (Cannot send messages to this user)

代码:

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

这是一个使用 discordie 的 discord 机器人。在我看来,上面的代码应该通过私人消息将单词 "test" 发送给消息作者,如果机器人不能,它将在他们发送消息的频道中发送错误和单词测试。但是,第二部分(在 catch 内)没有被执行。
tl; 博士,上面代码中的 catch 不起作用,如果机器人没有 dm 用户的权限,我会在控制台中收到上述错误。

您忘记了 then 函数中的 return 语句。 我想 message.sendMessage('test') returns 一个承诺

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