How to reply to any DMs sent to the bot?

随声附和 提交于 2019-12-10 19:17:43

问题


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!


回答1:


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.




回答2:


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!



来源:https://stackoverflow.com/questions/51614795/how-to-reply-to-any-dms-sent-to-the-bot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!