【问题标题】:Make the code below .then fire even if there is an error编写下面的代码。即使有错误也触发
【发布时间】:2021-04-09 11:12:05
【问题描述】:

我的 discord.js 机器人中有一个 kick 命令,我想让机器人向被踢的人发送 DM。我不能这样做:

user.send(message)

                    
target.kick(reason).then((m) => {
   // do the other stuff here
});

使用此代码,不会发送 DM。 这就是我所做的:

user.send(message).then((msg) => {              
   target.kick(reason).then((m) => {
      // do the other stuff here
   });
});

现在的问题是,如果目标阻止了机器人,机器人就不能 DM 他们,导致踢他们的代码无法运行。 我该如何解决这个问题?

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    您可以使用.finally,无论承诺是履行还是拒绝,它都会运行。

    user.send(message).finally(() => {              
       target.kick(reason).then((m) => {
          // do the other stuff here
       });
    });
    

    【讨论】:

      猜你喜欢
      • 2017-10-03
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 2021-09-21
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      相关资源
      最近更新 更多