Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 向作者发送直接消息_Javascript_Node.js_Discord_Discord.js - Fatal编程技术网

Javascript 向作者发送直接消息

Javascript 向作者发送直接消息,javascript,node.js,discord,discord.js,Javascript,Node.js,Discord,Discord.js,我在下面提供的代码可以正常工作。但是,当我关闭DM时,它会发送两条消息,无法被DMed和发送了一条消息。如果消息已经发送,我怎么可能让bot发送一条消息 message.author.send("hi").catch(() => message.channel.send("could not be DMed!")).then(() => message.channel.send(`Sent a message`)); 当dm关闭时,您似乎遇到

我在下面提供的代码可以正常工作。但是,当我关闭DM时,它会发送两条消息,
无法被DMed
发送了一条消息
。如果消息已经发送,我怎么可能让bot发送一条消息

message.author.send("hi").catch(() => message.channel.send("could not be DMed!")).then(() => message.channel.send(`Sent a message`));

当dm关闭时,您似乎遇到了某种错误,请尝试使用async await语法查看关闭dm时会出现什么错误,然后继续

  (
    async () => {
      try {
        const response = await message.channel.send('sent a message')
        console.log(response)
      } catch(err) {
        console.log(err)
        await message.channel.send("could not be DMed!")
      } 
    }
  )()

当dm关闭时,您似乎遇到了某种错误,请尝试使用async await语法查看关闭dm时会出现什么错误,然后继续

  (
    async () => {
      try {
        const response = await message.channel.send('sent a message')
        console.log(response)
      } catch(err) {
        console.log(err)
        await message.channel.send("could not be DMed!")
      } 
    }
  )()

只需颠倒
.then()
.catch()
的顺序即可。如果在
.catch()
之后附加
.then()
,它将像“finally”一样运行,即只要catch返回的承诺没有拒绝,它就保证运行

message.author.send("hi")
  .then(() => message.channel.send(`Sent a message`))
  .catch(() => message.channel.send("could not be DMed!"));
如果希望
.catch()
仅在原始承诺被拒绝时运行,而不希望
.then()
承诺被拒绝时运行,则可以使用
.then()
的第二个参数:


只需颠倒
.then()
.catch()
的顺序即可。如果在
.catch()
之后附加
.then()
,它将像“finally”一样运行,即只要catch返回的承诺没有拒绝,它就保证运行

message.author.send("hi")
  .then(() => message.channel.send(`Sent a message`))
  .catch(() => message.channel.send("could not be DMed!"));
如果希望
.catch()
仅在原始承诺被拒绝时运行,而不希望
.then()
承诺被拒绝时运行,则可以使用
.then()
的第二个参数: