How I fixed an issue with discord bot not caching all users?

How I fixed an issue with discord bot not caching all users?

By Bala | Bala | 9 Nov 2020


For the past few days, I was working on fixing an issue on one of my discord bots. Prior to this issue, HF 24 issues were keeping me busy but this issue was quite strange and it drained my energy to make things right. I wouldn't blame discord so much because it is one of the best-documented applications a developer can get. But it is just that the information was not there in the right place which is what drained my energy to understand and implement the changes. My ignorance also played a vital role in this.

image.png
Prepared using Carbon

v12 was one of the biggest releases that discord.js did recently and that's what came up with lots of changes. One major change was the cache they introduced. I first thought it was simple but while doing a deep dive now, I was able to understand the seriousness of using it right. When I initially upgraded my bot from v11 to v12, I did not notice this issue as the caching was working all fine based on the messages sent by the users after the bot was started. Only a week back I found out about this issue when many users were skipped during giveaways. Discord had documentation on how to upgrade our applications from v11 to v12 but for some reason, I did not find it very helpful especially for the problem that I had.

My problem statement: Whenever the bot is restarted, the cached users and messages are gone.

Solution: Fetch all the members from the guild(server) whenever the bot is started or restarted. By doing this the cache gets updated.

To arrive at that solution, it took me so much time. I first did not believe that introducing the cache feature would no display all the members correctly. If it had not displayed any members from the guild, it would have rung a bell on my head. But the results were partial. Some of the members were displayed and some members were not available. Also, another case was that, if I had used the bot as one particular user, it was all fine and if I use it as another user, the user was not recognized. This strange behavior is what drained my energy looking for options to solve it.

image.png

I finally took some time to read the docs clearly page by page. Of course not every single method or function but at least the ones that were related to the problem that I was facing. Finally, I found out about Gateway intents. I don't know from where I reached there but that was pretty much what I was looking for. That was also not very straight forward. When I found out that I should be fetching all the users when the bot starts, that is when I ran into time out issues.

When I asked Mr. Google why I get a timeout for fetching members of a discord guild, that is when I was directed to this link. This completely explained what I was going through and a solution to that. The solution was very simple. I had to go to the developers portal and enable the intent option for the bot.

image.png

After doing this and after writing a simple code to fetch all the members when the bot is up, all the problems were sorted and my bot was up and running again. You can find the code snippet in the first image of this article. It was both a good and bad experience. Bad because it drained my energy and good because it gave me lots of learning.


500x90standardtemplate.png

image.png

da2424398bd4625ed05e95650fd1fc59638d98e68350cf673d8b393558969644.png


Bala
Bala

Developer | Writer | Blogger | Gamer | Blockchain Enthusiast


Bala
Bala

This is my general blog. I share many different articles I write every single day.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.