【问题标题】:Error with promise rejections while using bulkDelete使用 bulkDelete 时承诺拒绝错误
【发布时间】:2020-06-06 14:43:09
【问题描述】:

所以在使用从频道中删除消息的.bulkDelete() 方法时,我收到此错误:

UnhandledPromiseRejectionWarning: DiscordAPIError: Unknown Message

(node:11720) UnhandledPromiseRejectionWarning: 未处理的承诺 拒绝。此错误源于在异步内部抛出 没有 catch 块的函数,或者通过拒绝一个承诺 不使用 .catch() 处理。 (拒绝 ID:2)(节点:11720)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。在 未来,未处理的承诺拒绝将终止 具有非零退出代码的 Node.js 进程。

这是我的代码:

//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));

提前致谢。

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    问题是你的 catch 有代码抛出异常,因为它发生在 catch 块内。

    值得在全局级别设置一个 catch 并捕获错误并记录它。

    另外,关于你链接你的承诺的方式,你可以像这样简化它

    //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));
    

    【讨论】:

    • 感谢您的回复,但是在更正我的代码后,我收到了相同的错误 DiscordAPIError: Unknown MessageUnhandledPromiseRejectionWarning: Unhandled promise rejection.。你有什么想法吗?
    • 我添加了 catch 块,我仍然得到同样的错误
    • 所以我检查了代码并在最后一个 catch 块之后放置了一个 console.log("here") 语句,它在这里打印然后发送错误,PS:除了我没有任何承诺我发送的代码中的那些。
    • 什么都没有,我在每个 catch 块中添加了 console.logs,错误就消失了,没有触发任何 catch console.log。
    猜你喜欢
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 2019-11-25
    相关资源
    最近更新 更多