forked from LeineLab-Public/lab-signal-bot
main: sync_members_as_active_users()
This commit is contained in:
parent
fce9452b72
commit
b58ceecf1a
36
main.py
36
main.py
@ -9,7 +9,9 @@ from websockets.asyncio.client import connect
|
|||||||
import websockets
|
import websockets
|
||||||
import asyncio
|
import asyncio
|
||||||
from apitypes import *
|
from apitypes import *
|
||||||
|
from models import *
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
from sqlmodel import Session, SQLModel, create_engine, select
|
||||||
|
|
||||||
class Config(BaseModel):
|
class Config(BaseModel):
|
||||||
apiurl: str
|
apiurl: str
|
||||||
@ -174,6 +176,28 @@ class LabCleaningBot:
|
|||||||
|
|
||||||
return Ok(other_members)
|
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__":
|
if __name__ == "__main__":
|
||||||
@ -185,14 +209,16 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
bot = LabCleaningBot(api, "group.bm5KT3NJUW5FdkpRNnR2ZGRFa01oOVZBeUYrVkdnd3NNTzFpNWdsR2pwUT0=")
|
bot = LabCleaningBot(api, "group.bm5KT3NJUW5FdkpRNnR2ZGRFa01oOVZBeUYrVkdnd3NNTzFpNWdsR2pwUT0=")
|
||||||
|
|
||||||
|
engine = create_engine("sqlite:///data.db")
|
||||||
|
SQLModel.metadata.create_all(engine)
|
||||||
|
with Session(engine) as session:
|
||||||
while True:
|
while True:
|
||||||
groupinfo = bot.api.get_group(bot.base_group)
|
sync_result = bot.sync_members_as_active_users(session)
|
||||||
|
|
||||||
if is_err(groupinfo):
|
if is_err(sync_result):
|
||||||
print(groupinfo.unwrap_err())
|
print(sync_result.unwrap_err())
|
||||||
continue
|
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user