Recently, my bot killed itself multiple times as it ran out of memory. My VPS has about 4GB of RAM and my bot was nearly using 70% of it.
After looking into all my active tasks, I found out that sometimes the bot is running thousands of on_presence_update TASKS inside the bot.
Is this code-related or intended when running the function? While going through my code, I didn't encounter a create_task function or else...
I am caching all the server (channels to send a message to and roles to give out) and user settings (whether someone opted-out or not) so I can't really understand this weird behavior lately.
Here's a short snippet:
spotify_settings = await self.get_spotify_settings(after.guild.id)
spotify_channel = None if not spotify_settings else spotify_settings.get('spotify_channel')
spotify_channel = before.guild.get_channel(spotify_channel) if spotify_channel else None
spotify_role = None if not spotify_settings else spotify_settings.get('spotify_role')
spotify_role = before.guild.get_role(spotify_role) if spotify_role else None
###############################################################
twitch_settings = await self.get_twitch_settings(after.guild.id)
twitch_channel = None if not twitch_settings else twitch_settings.get('twitch_channel')
twitch_channel = before.guild.get_channel(twitch_channel) if twitch_channel else None
twitch_role = None if not twitch_settings else twitch_settings.get('twitch_role')
twitch_role = before.guild.get_role(twitch_role) if twitch_role else None
What it basically does is checking if the guild is within a defined dict at the beginning and if not, perform one DB query (which is not visible here)