SHARK
SHARK

Reputation: 7

How to check how many messages a user has sent in a server

I've been trying to do this for an XP system that runs by how many messages sent. Each message is 5 XP but I haven't figured out how to find the number of messages sent by a user (in a server). Can anybody help?

Upvotes: 0

Views: 1506

Answers (1)

Federico Grandi
Federico Grandi

Reputation: 6796

I'm afraid the only way to do this is to look at every message in the guild and count them one by one.
There are a few optimization tricks you can use, like not looking at messages sent before the user joined (GuildMember.joinedAt), but it's still a huge operation because you'll ultimately need to fetch every message in the guild.

The search feature in Discord could be useful in this situation, but it's not currently available in Discord.js (or to bot users at all).

I think the best thing for you to do is not to do it at all: it's better if you keep a database with every user's exp level, and update their exp on every message (or once a day, if you prefer).
Not keeping a database at all is not a viable solution, since you'd need to fetch the entire guild every time (and I think you'd get rate-limited at that point).
If you were trying to do this only once to establish the initial level for everyone then it's better if you do something based on the time since they've joined the guild, or just make start everyone from 0. There's an exception though: if you think your guild is small enough you can fetch all the messages just one time but at that point, you may just make everyone start at 0, it's much simpler.

Upvotes: 1

Related Questions