How to detect if the bot has been mentioned?

送分小仙女□ 提交于 2020-01-14 07:01:34

问题


How can I detect if my bot is mentioned?

I tried these thus far:

if (msg.content.toLowerCase().includes('@The Guardian of The Bar#5180')) {
  msg.channel.send("My prefix here is" + prefix + "\n You can start with ``" + prefix + "help``");
}

if (msg.content.toLowerCase().includes('@The Guardian of The Bar')) {
  msg.channel.send("My prefix here is" + prefix + "\n You can start with ``" + prefix + "help``");
}

if (msg.content.includes('@The Guardian of The Bar#5180')) {
  msg.channel.send("My prefix here is" + prefix + "\n You can start with ``" + prefix + "help``");
}

if (msg.content.includes('@The Guardian of The Bar#5180')) {
  msg.channel.send("My prefix here is" + prefix + "\n You can start with ``" + prefix + "help``");
}

回答1:


I'm not experienced in discord.js, but according to this in the documentation, a message object has a method message.isMemberMentioned(User or GuildMember). Using this method, one can use

client.on('message', message => { //this event is fired, whenever the bot sees a new message
   if (message.isMemberMentioned(client.user)) { //we check, whether the bot is mentioned, client.user returns the user that the client is logged in as
      //this is where you put what you want to do now
   }
});

I would like to recommend, that you read the documentation fully, before you try to make a bot.



来源:https://stackoverflow.com/questions/55195128/how-to-detect-if-the-bot-has-been-mentioned

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