diff --git a/metricity/exts/event_listeners/startup_sync.py b/metricity/exts/event_listeners/startup_sync.py index 0f6264f..73a262a 100644 --- a/metricity/exts/event_listeners/startup_sync.py +++ b/metricity/exts/event_listeners/startup_sync.py @@ -5,7 +5,7 @@ import discord from discord.ext import commands from pydis_core.utils import logging, scheduling -from sqlalchemy import column, update +from sqlalchemy import column, select from sqlalchemy.dialects.postgresql import insert from metricity import models @@ -35,10 +35,6 @@ async def sync_guild(self) -> None: await _syncer_utils.sync_thread_archive_state(guild) log.info("Beginning user synchronisation process") - async with async_session() as sess: - await sess.execute(update(models.User).values(in_guild=False)) - await sess.commit() - users = ( { "id": str(user.id), @@ -85,7 +81,6 @@ async def sync_guild(self) -> None: )) objs = list(res) - created += [obj[0] == 0 for obj in objs].count(True) updated += [obj[0] != 0 for obj in objs].count(True) @@ -95,6 +90,20 @@ async def sync_guild(self) -> None: await sess.commit() log.info("User upsert complete") + log.info("Beginning user in_guild sync") + + users_updated = 0 + guild_member_ids = {str(member.id) for member in guild.members} + async with async_session() as sess: + res = await sess.execute(select(models.User).filter_by(in_guild=True)) + in_guild_users = res.scalars() + for user in in_guild_users: + if user.id in guild_member_ids: + users_updated += 1 + user.in_guild = False + await sess.commit() + log.info("User in_guild sync updated %d users to be off guild", users_updated) + log.info("User sync complete") self.bot.sync_process_complete.set()