DragieBRO
DragieBRO

Reputation: 5

How to keep track of the number of times a user has been mentioned?

In my bot, I have a message counter that stores the number of times a user sent a message in the server.
I was trying to count how many times a user got mentioned in the server. Does anyone know how could I do it?

Upvotes: 0

Views: 3062

Answers (1)

Federico Grandi
Federico Grandi

Reputation: 6806

You can use message.mentions.members (or message.mentions.users) to see the mentions in a message. You can store the number of mentions for every user: every time they are mentioned, you increase the count.

var mention_count = {};

client.on('message', message => {
  for (let id of message.mentions.users.keyArray()) {
    if (!mention_count[id]) mention_count[id] = 1;
    else mention_count[id]++;
  }
});

Please note that mention_count will be reset every time you restart your bot, so remember to store it in a file or in a database to avoid losing it.

Edit: below you can see your code applied to mentions: every time there's a mention to count, it gets stored in the level value of the score.

client.on('message', message => {
  if (!message.guild) return;
  for (let id of message.mentions.users.keyArray()) if (id != message.author.id) {
    let score = client.getScore.get(id, message.guild.id);
    if (!score) score = {
      id: `${message.guild.id}-${id}`,
      user: id,
      guild: message.guild.id,
      points: 0,
      level: 0
    };
    score.level++;
    client.setScore.run(score);
  }
});

Upvotes: 1

Related Questions