提问者:小点点

如何让我的discord.js bot以不同的消息响应DM?


我在discord.js中编写一个bot,我已经设法使bot响应DM,但它只响应1条消息。

我希望它能够首先响应一件事,比如“Hello”,然后第二次当同一个人DM是它时,我希望它回复“How are you?”,然后在所有响应都用完后循环/停止。

下面是它响应DM的代码:

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

谢谢:D


共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
}