【问题标题】:Why does the .catch() fail to do anything?为什么 .catch() 没有做任何事情?
【发布时间】:2021-08-11 23:40:57
【问题描述】:

我正在用 javascript 制作一个不和谐的机器人,并且我制作了一个将消息发送到特定频道的命令。它以前有效,但现在无效。我发现,问题出在这部分代码上:

let sugchannel = message.guild.channels.cache.find(c => c.name === "name");

  sugchannel.send(embed).then((msg) =>{
    
    message.delete();

  }).catch((err)=>{
    throw err;
  });

我解决了很多类似的问题,但没有解决问题。此外,它确实有效,如果我将其更改为

message.channel.send(embed).then...

这是错误信息:

(node:416) UnhandledPromiseRejectionWarning: DiscordAPIError: Missing Permissions
    at RequestHandler.execute (/home/runner/cutiefoxy/node_modules/discord.js/src/rest/RequestHandler.js:154:13)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async RequestHandler.push (/home/runner/cutiefoxy/node_modules/discord.js/src/rest/RequestHandler.js:39:14)
(node:416) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:416) [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.
(node:416) UnhandledPromiseRejectionWarning: DiscordAPIError: Missing Permissions
    at RequestHandler.execute (/home/runner/cutiefoxy/node_modules/discord.js/src/rest/RequestHandler.js:154:13)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async RequestHandler.push (/home/runner/cutiefoxy/node_modules/discord.js/src/rest/RequestHandler.js:39:14)
(node:416) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

【问题讨论】:

  • 在我看来,您的机器人只是缺少在您的服务器中执行该操作的权限。
  • 它的权限和我一样。我是业主。这正是我感到困惑的原因。
  • 你为什么在throw err里面抓到?
  • 所问的问题是询问承诺以及拒绝处理程序如何工作。不过,我怀疑您可能想发布一个不同的问题,询问为什么 sugchannel.send(embed) 失败并出现您在 message.channel.send(embed) 成功时显示的权限错误。

标签: javascript node.js promise discord.js


【解决方案1】:

为什么 .catch() 什么都不做?

它确实做了一些事情,只是没有做任何有用的事情。 .catch(err => { throw err; }) 从来没有任何意义。它所做的只是连接一个拒绝处理程序,该处理程序创建一个新的 Promise 并以它收到的相同错误拒绝它。 (有一个抛出的拒绝处理程序可能是有意义的,但如果 all 它所做的只是抛出原始错误而不做任何其他事情。)

thencatch 创建承诺。根据回调的作用和返回的内容,这些承诺会被履行还是被拒绝。如果回调抛出,则拒绝已创建的承诺,这就是您的代码正在执行的操作。

promise 的规则之一是你必须要么

  1. 处理拒绝,

  2. 将承诺返回给可以处理拒绝的东西

通常您需要 #2,然后只在代码的最顶层处理拒绝 (#1)。

因此,如果此代码是代码的最顶层,请删除 throw err 并改为记录或以其他方式处理错误。如果这不在代码的最顶层,请完全删除 .catch 并从 then 返回承诺,以便调用者可以处理它(或调用者的调用者,或调用者的调用者的调用者等)。

这是尽可能使用async 函数的原因之一,当您在其中使用awaitreturn 时,它们会自动返回承诺链。

【讨论】:

    猜你喜欢
    • 2021-12-20
    • 2011-07-25
    • 2015-06-30
    • 2023-04-06
    • 2021-04-10
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多