redbrain
redbrain

Reputation: 45

get count of a user's mentions in a channel

I'm trying to count the amount of times a user has been mentioned in a channel. The code I have is:

message.channel.messages.fetch()
    .then(messages => console.log(`${messages.filter(m => m.mentions.users[1] === 'msg.author').size} messages`))
    .catch(console.error);

When I ran this in a channel I had 27 mentions in that channel's history. This returned 0 messages.

How do I accomplish this?

Upvotes: 0

Views: 227

Answers (1)

user13429955
user13429955

Reputation:

error is with your filter,

m.mentions.users is a collection mapped by the users id, when you do [1] it checks for a user with the id of 1 and then checks it to the string msg.author

message.channel.messages.fetch()
 .then(messages => {
   //.has() checks if the collection includes the key
   const size = messages.filter(m => m.mentions.users.has(message.author.id)).size;
   console.log(size, "messages");
});
 .catch(console.error);

Upvotes: 1

Related Questions