Mario
Mario

Reputation: 1488

How to reply to any DMs sent to the bot?

I am trying to make my bot replying to any DM sent to it.

So I currently have this:

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

But unfortunately, it does not reply to any DM.

I've tried to replace msg.channel.DMChannel with msg.channel.type == 'dm' but this didn't work.

I also tried to replace msg.reply with msg.author.send and msg.channel.send but they all didn't work.

Any help would be appreciated.

Thanks!

Upvotes: 3

Views: 15626

Answers (2)

Woohoojin
Woohoojin

Reputation: 724

Your function isn't receiving the proper 'msg' object, try this:

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

Edit: I referenced sample code here to come to this conclusion. Maybe this link will help you in the future. Good luck!

Upvotes: 1

André
André

Reputation: 4497

On the official documentation I don't see mentioned the event client.on('msg') only client.on('message').
With that out of the way:

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

Just tried this and worked without any issues.

Upvotes: 6

Related Questions