By Alexander Shatov from Unsplash

How to count Discord Messages

Christoph Krassnigg

--

Most discord users out there are familiar with bots like Mee6, which count all your messages and then give you a score according to your amount of writing messages. But how do they do this? Is there any feature from Discord you missed out on? We will cover all the options you have in this article.

Counting messages with the Discord App

As a regular user, you definitely noticed the search field on the right top corner. You have a simple query builder with which you can search through all the messages on a Discord server. You are provided with options like from, mentions, has a file, has an url, between timestamps, and so on.

If you search for all messages from a specific user, you will see a list of the first messages and how many.

Image where you see how many messages I have written

If you know what you are doing, you may come up with the idea of looking in the developer console networking tab. You will see that discord has an API call for this, and so this will be the solution to integrate it into your bot! At first glance, it looks like this is the solution, but sadly this API call is limited to users only. So we need to find another solution.

Counting messages by looking through all channels

The Discord API provides a way to retrieve the full message history in every channel on a server. With all the messages, you can count how often a user sent a message, but this is very resource-intensive. It takes a long to retrieve all the messages on a larger Discord, and it will take a longer time to update all those values in your bot database.

So this is an option you would rather not choose because of the intensive tasks. If you are counting the messages on a huge discord, then this can take eternities.

Counting messages the right way

This is the way most bot developers are currently counting the messages from a user. It has its pros and cons.

As soon as you register that a user wrote something, get him from the database, increment his message count by one and store it. This is the most efficient way to do this, but it has cons like all good things.

When the bot gets added to a server after there have already been thousands of messages written, the bot will not count them, except you retrieve all messages as mentioned above. If the bot is not offline, it will miss messages, but this is a small price to pay for the most optimal performance.

Conclusion

As soon as you get a message event, increment the number of messages a user has written. This is as the day of writing this article the most optimized way.

If you are also interested in how to make your bot scaleable, then this will be perfect for you.

--

--

Christoph Krassnigg
Christoph Krassnigg

Written by Christoph Krassnigg

Developer at block42. Student. Java fanatic. Loves to write about techy things.

Responses (3)