如何使我的discord.js机器人以不同的消息响应DM?

时间:2020-08-06 13:35:24

标签: javascript node.js discord discord.js

我正在discord.js中编码一个机器人,并且设法使该机器人响应DM,但它仅响应1条消息。

我希望它能够首先回应一件事,例如“ Hello”,然后第二次当同一个人DM回应时,我希望它回复“你好吗?”,然后循环/停止一旦所有答复都用完了。

以下是其响应DM的代码:

    if (msg.channel.type == "dm") {
      msg.author.send("Hey!");
      return;
    }
  })

谢谢:D

1 个答案:

答案 0 :(得分:1)

您可以保存发给作者的消息的频率,但是,如果您打算长时间进行此操作(由于在重新启动bot时会删除运行时的所有保存内容),则需要数据库。将是一个字典式的方法:

//at the top of your file
let msgs = { }; //this is where you will dynamically store the amount
const dms = ["hey!", "How are you?", "Nice to meet you!", "..."]; //array of answers

...
if (msg.channel.type == "dm") {
    let count = msgs[message.author.id]; //get the amount of messages sent
    if(!count) count = msgs[message.author.id] = 0; //set to 0 if non sent before
    msgs.author.send(dms[count]); //send the corresponding message => fetched from array
    msgs[message.author.id]++; //increase message sent count by one
}