如何检测机器人是否已被提及?

时间:2019-03-16 09:05:07

标签: javascript node.js discord.js

如何检测我的机器人是否被提及?

到目前为止,我已经尝试过这些:

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 个答案:

答案 0 :(得分:3)

我没有使用discord.js的经验,但是根据文档中的this,消息对象具有方法message.isMemberMentioned(User or GuildMember)。使用这种方法,可以使用

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
   }
});

我建议您在尝试制作机器人之前,先完整阅读documentation