11"""An ext to sync the guild when the bot starts up."""
22
33import math
4+ import time
45
56import discord
67from discord .ext import commands
@@ -25,7 +26,7 @@ def __init__(self, bot: Bot) -> None:
2526 self .bot = bot
2627 scheduling .create_task (self .sync_guild ())
2728
28- async def sync_guild (self ) -> None :
29+ async def sync_guild (self ) -> None : # noqa: PLR0914
2930 """Sync all channels and members in the guild."""
3031 await self .bot .wait_until_guild_available ()
3132
@@ -96,16 +97,30 @@ async def sync_guild(self) -> None:
9697 users_updated = 0
9798 guild_member_ids = {str (member .id ) for member in guild .members }
9899 async with async_session () as sess :
100+ start = time .perf_counter ()
99101
100102 stmt = select (models .User ).filter_by (in_guild = True ).options (load_only (models .User .id ))
101103 res = await sess .execute (stmt )
102104 in_guild_users = res .scalars ()
105+ query = time .perf_counter ()
106+
103107 for user in in_guild_users :
104108 if user .id not in guild_member_ids :
105109 users_updated += 1
106110 user .in_guild = False
111+ proc = time .perf_counter ()
112+
107113 await sess .commit ()
108- log .info ("User in_guild sync updated %d users to be off guild" , users_updated )
114+ end = time .perf_counter ()
115+
116+ log .debug (
117+ "in_guild sync: total time %fs, query %fs, processing %fs, commit %fs" ,
118+ end - start ,
119+ query - start ,
120+ proc - query ,
121+ end - proc ,
122+ )
123+ log .info ("User in_guild sync updated %d users to be off guild" , users_updated )
109124 log .info ("User sync complete" )
110125
111126 self .bot .sync_process_complete .set ()
0 commit comments