gpt4 book ai didi

javascript - 使用bulkDelete时出现 promise 拒绝错误

转载 作者:太空宇宙 更新时间:2023-11-04 01:17:43 26 4
gpt4 key购买 nike

因此,在使用 .bulkDelete() 后方法,从 channel 中删除消息,我收到此错误:

UnhandledPromiseRejectionWarning: DiscordAPIError: Unknown Message

(node:11720) 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(). (rejection id: 2) (node:11720) [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.

这是我的代码:

//delete the given amount of message + 1 for the sent message
let msgToDelete = parseInt(args[1]) + 1;

//filter messages too old and delete unfiltered messages
msg.channel.bulkDelete(msgToDelete, true)
.then(deleted => {
if (deleted.size <= 1) return;
msg.channel.send(`deleted ${args[1]} messages.`)
.then(m => m.delete(2500))
.catch(err => console.error);
}).catch(err => msg.channel.send(err));

提前致谢。

最佳答案

问题是您的 catch 中的代码抛出了异常,该异常在 catch block 内发生时未得到处理。

值得在全局级别进行捕获并捕获错误并记录它。

此外,关于您链接 promise 的方式,您可以像这样简化

//delete the given amount of message + 1 for the sent message
let msgToDelete = parseInt(args[1]) + 1;

//filter messages too old and delete unfiltered messages
msg.channel.bulkDelete(msgToDelete, true)
.then(deleted => {
if (deleted.size <= 1) return;
return msg.channel.send(`deleted ${args[1]} messages.`);
})
.then(m => m.delete(2500))
.catch(err => msg.channel.send(err));

关于javascript - 使用bulkDelete时出现 promise 拒绝错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60351016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com