如何获取频道中用户的所有提及 discord.js

How to get all the mentions from an user in a channel discord.js

所以,我正在尝试创建一个简单的机器人,当用户写入时:

!howmanypoints {user}

应该去特定的频道,搜索在特定的频道中提到了多少次。

截至目前,我遇到了 fetchMentions() 函数,但它似乎已被弃用,并且 API 不喜欢当机器人尝试执行此操作时..

任何输入或至少更好的方法。也许我执着于让它发挥作用。

const Discord = require('discord.js')
const client = new Discord.Client()


client.on('message', msg => {
  if (msg.content.includes('!howmanypoints') ||      msg.content.includes('!cuantospuntos')) {

    const canal_de_share = client.channels.find('name', 'dev')
    const id_user = client.user.id
    client.user.fetchMentions({guild: id_user })
    .then(console.log)
    .catch(console.error);
  }
})

ClientUser.fetchMentions() 已弃用,仅适用于用户帐户。

作为替代方案,您可以获取频道的所有消息并遍历每条消息,检查是否有提及。

示例:

let mentions = 0;

const channel = client.channels.find(c => c.name === 'dev');
const userID = client.user.id;

channel.fetchMessages()
  .then(messages => {
    for (let message of messages) {
      if (message.mentions.users.get(userID)) mentions++;
    }

    console.log(mentions);
  })
  .catch(console.error);