diff --git a/bot/core/module.py b/bot/core/module.py index daba7dc3..e46f9875 100644 --- a/bot/core/module.py +++ b/bot/core/module.py @@ -40,6 +40,32 @@ def setting_initialisation(client): setting.init_task(client) +@module.launch_task +async def preload_guild_configuration(client): + """ + Loads the plain guild configuration for all guilds the client is part of into data. + """ + guildids = [guild.id for guild in client.guilds] + rows = client.data.guild_config.fetch_rows_where(guildid=guildids) + client.log( + "Preloaded guild configuration for {} guilds.".format(len(rows)), + context="CORE_LOADING" + ) + + +@module.launch_task +async def preload_studying_members(client): + """ + Loads the member data for all members who are currently in voice channels. + """ + userids = list(set(member.id for guild in client.guilds for ch in guild.voice_channels for member in ch.members)) + rows = client.data.lions.fetch_rows_where(userid=userids) + client.log( + "Preloaded member data for {} members.".format(len(rows)), + context="CORE_LOADING" + ) + + @module.launch_task async def launch_lion_sync_loop(client): asyncio.create_task(_lion_sync_loop()) diff --git a/bot/modules/study/admin.py b/bot/modules/study/admin.py index 6a1a9b6b..5861ab95 100644 --- a/bot/modules/study/admin.py +++ b/bot/modules/study/admin.py @@ -1,3 +1,5 @@ +from collections import defaultdict + import settings from settings import GuildSettings from wards import guild_admin @@ -37,6 +39,34 @@ class untracked_channels(settings.ChannelList, settings.ListData, settings.Setti else: return "Study time will now be counted in all channels." + @classmethod + async def launch_task(cls, client): + """ + Launch initialisation step for the `untracked_channels` setting. + + Pre-fill cache for the guilds with currently active voice channels. + """ + active_guildids = [ + guild.id + for guild in client.guilds + if any(channel.members for channel in guild.voice_channels) + ] + if active_guildids: + rows = cls._table_interface.select_where( + guildid=active_guildids + ) + cache = defaultdict(list) + for row in rows: + cache[row['guildid']].append(row['channelid']) + cls._cache.update(cache) + client.log( + "Cached {} untracked channels for {} active guilds.".format( + len(rows), + len(cache) + ), + context="UNTRACKED_CHANNELS" + ) + @GuildSettings.attach_setting class hourly_reward(settings.Integer, settings.GuildSetting): diff --git a/bot/modules/study/time_tracker.py b/bot/modules/study/time_tracker.py index 1bf82b42..26afddd6 100644 --- a/bot/modules/study/time_tracker.py +++ b/bot/modules/study/time_tracker.py @@ -101,6 +101,8 @@ async def _study_tracker(): @module.launch_task async def launch_study_tracker(client): + # First pre-load the untracked channels + await admin.untracked_channels.launch_task(client) asyncio.create_task(_study_tracker())