在承诺
问题描述:
不能抓住UnhandledPromiseRejectionWarning所以我试图抓住我的承诺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。在我看来,上面的代码应该通过私人消息向消息作者发送单词“测试”,如果僵尸不能,它会在他们发送消息的通道中发送错误和单词测试。但是,第二部分(在catch中)不会被执行。
tl; dr,上面代码中的catch不起作用,我在控制台中得到了上述错误,而如果bot没有dm用户的权限。
答
忘记return
then
函数内的函数。 我想message.sendMessage('test')
退货承诺
e.message.author.openDM().then((message) => {
return message.sendMessage(`test`);
}).catch((error) => {
e.message.channel.sendMessage(error + "test");
});