【问题标题】:How to detect if the bot has been mentioned?如何检测是否提到了机器人?
【发布时间】:2019-08-07 06:32:41
【问题描述】:

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

到目前为止,我尝试了这些:

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

【问题讨论】:

  • 添加一些相关代码,而不是分享图片。
  • @Md.SukelAli 我做到了。你能帮帮我吗?

标签: javascript node.js discord.js


【解决方案1】:

我对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

在 v12 discord.js 上,您现在必须使用 message.mentions.has(bot.user)

【讨论】:

    猜你喜欢
    • 2020-07-29
    • 2021-04-16
    • 2018-10-11
    • 2019-04-24
    • 2021-10-13
    • 2021-07-17
    • 2018-09-14
    • 1970-01-01
    • 2020-06-07
    相关资源
    最近更新 更多