javascript - 如何检测是否提到了机器人?
问题描述
如何检测是否提到了我的机器人?
到目前为止,我尝试了这些:
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``");
}
解决方案
我对 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
}
});
我想建议您在尝试制作机器人之前完整阅读文档。
在 v12 discord.js 上,您现在必须使用message.mentions.has(bot.user)
推荐阅读
- javascript - 使用 JQuery 从 DIV 随机数据中读取
- java - Java Rock Paper 游戏循环次数过多
- javascript - What does it mean by live bindings?
- c++ - 查找不在 std::set 中的第一个值
最小-最大 - javascript - Figuring out what's wrong with Heroku applications
- angular - Angular:Redux and Lazy loading
- javascript - Get my arrow function output and display it in a table format in render component in React
- javascript - JavaScript display array elements within certain time period
- ios - swift 4在后台自定义本地通知
- python-3.x - 制作一个包含 (i,i*i) 的字典,其中 i 是从 1 到 n