Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 + "
 You can start with ``" + prefix + "help``");
}

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

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

if (msg.content.includes('@The Guardian of The Bar#5180')) {
  msg.channel.send("My prefix here is" + prefix + "
 You can start with ``" + prefix + "help``");
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
293 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

On v12 discord.js you now have to use message.mentions.has(bot.user)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...