diff --git a/main.py b/main.py index db53399..93835fc 100644 --- a/main.py +++ b/main.py @@ -9,7 +9,9 @@ from websockets.asyncio.client import connect import websockets import asyncio from apitypes import * +from models import * from pydantic import BaseModel +from sqlmodel import Session, SQLModel, create_engine, select class Config(BaseModel): apiurl: str @@ -174,6 +176,28 @@ class LabCleaningBot: return Ok(other_members) + def sync_members_as_active_users(self, session) -> Result[None, str]: + group = self.api.get_group(self.base_group) + if group.is_err(): + return Err(group.unwrap_err()) + + members = group.unwrap().members + + for member in members: + maybe_user = get_user_by_name(session, member, only_active=False) + + if is_err(maybe_user): + user = User(name=member) + session.add(user) + else: + user = maybe_user.unwrap() + user.active = True + + for user in session.exec(select(User).where(User.active)): + if user.name not in members: + user.active = False + + session.commit() if __name__ == "__main__": @@ -185,19 +209,21 @@ if __name__ == "__main__": bot = LabCleaningBot(api, "group.bm5KT3NJUW5FdkpRNnR2ZGRFa01oOVZBeUYrVkdnd3NNTzFpNWdsR2pwUT0=") - while True: - groupinfo = bot.api.get_group(bot.base_group) + engine = create_engine("sqlite:///data.db") + SQLModel.metadata.create_all(engine) + with Session(engine) as session: + while True: + sync_result = bot.sync_members_as_active_users(session) - if is_err(groupinfo): - print(groupinfo.unwrap_err()) - continue + if is_err(sync_result): + print(sync_result.unwrap_err()) - groupinfo = groupinfo.unwrap() + exit(0) - res = bot.api.update_group_members("group.TTlKelhpUW1sUVJSU2Z2NDJpdjVWcllMTW93MTBNN2tseEtGaFkzQ1VsZz0=", groupinfo.members) + res = bot.api.update_group_members("group.TTlKelhpUW1sUVJSU2Z2NDJpdjVWcllMTW93MTBNN2tseEtGaFkzQ1VsZz0=", groupinfo.members) - if is_err(res): - print(res.unwrap_err()) - continue + if is_err(res): + print(res.unwrap_err()) + continue - print(res.unwrap()) + print(res.unwrap())