【问题标题】:How to reply to any DMs sent to the bot?如何回复发送给机器人的任何 DM?
【发布时间】:2019-01-07 22:24:22
【问题描述】:

我正在尝试让我的机器人回复发送给它的任何 DM。

所以我目前有这个:

client.on('msg', () => {
  if (msg.channel.DMChannel) {
    msg.reply("You are DMing me now!");
  }
});

但很遗憾,它没有回复任何 DM。

我尝试将msg.channel.DMChannel 替换为msg.channel.type == 'dm',但这不起作用。

我也尝试将msg.reply 替换为msg.author.sendmsg.channel.send,但它们都不起作用。

任何帮助将不胜感激。

谢谢!

【问题讨论】:

  • 不需要把msg对象传给函数吗?

标签: discord discord.js


【解决方案1】:

你的函数没有接收到正确的“msg”对象,试试这个:

client.on('message', async message => {
    if (message.channel.type == 'dm') {
        message.reply("You are DMing me now!");
    }
});

编辑:我引用了示例代码here 来得出这个结论。也许此链接将来会对您有所帮助。祝你好运!

【讨论】:

  • 编辑:这是我的错误 xD - 现在可以使用了!非常感谢!
  • @André - 是的,我已经用你的答案message.channel.type == "dm" 替换了它,它正在工作。我建议@Christian Scillitoe 使用正确的语法编辑答案。
【解决方案2】:

official documentation 上,我没有看到提到事件client.on('msg') 只有client.on('message')
顺便说一句:

client.on('message', msg => {
  if (msg.channel.type == "dm") {
    msg.author.send("You are DMing me now!");
    return;
  }
});

刚刚尝试过,没有任何问题。

【讨论】:

  • 啊,这是正确的。我在回答时什至没有检查。编辑我的以反映正确的语法。
猜你喜欢
  • 2019-01-07
  • 1970-01-01
  • 2021-06-13
  • 2021-03-11
  • 2020-09-14
  • 2021-12-23
  • 2020-09-16
  • 2020-03-05
  • 2019-06-27
相关资源
最近更新 更多