我想通过discord.js判断给定的消息是否提及服务器上的任何用户。
答案 0 :(得分:1)
Message.mentions
是您要查找的内容:您可以选中.users
或.members
。如果这些collections之一中至少有一个元素,则有人提到:
client.on('message', message => {
if (message.mentions.members.first()) // there's at least one mentioned user
else // there's no mentioned user
});
答案 1 :(得分:0)
保留用户地图并与传入消息匹配
const Discord = require('discord.js')
const client = new Discord.Client()
const map = {}
client.on('message', msg => {
if (msg.content.indexOf('@') !== -1) {
const users = msg.content.match(/@[a-z\d]+/ig)
users.forEach((user) => {
if (map[users.slice(1)]) {
console.log(`${users.slice(1)} mentioned in server`)
}
})
}
})
client.on('ready', () => {
setInterval (function (){
for(u in Bot.users){
map[client.users[u].username] = true
}
}, 10000)
})
client.login('token')