Najmul190
Najmul190

Reputation: 21

How can I get my discord.js bot to respond to DM's, with different messages?

I am coding a bot in discord.js, and I have managed to make the bot respond to a DM, but it only responds with 1 message.

I want it to be able to respond with one thing first, like "Hello", and then the second time when the same person DM's it, I want it to reply "how are you?", and then loop / stop once all responses have been used.

Here's the code for it to respond to DM's:

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

Thanks :D

Upvotes: 1

Views: 878

Answers (1)

Zer0
Zer0

Reputation: 1690

You can just save how often you messaged the author, however you'd need a database for that if you plan on having this going for a long time (since all saves at runtime are deletd when restarting the bot), one option would be a dictionary-wise apporach:

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

Upvotes: 2

Related Questions