Files
intern2020/bot1/users.py
2021-01-01 22:20:36 +00:00

60 lines
1.6 KiB
Python

from misc import SaveFileMapping, SaveFileObject
from dataclasses import dataclass
from pytgbot.api_types.receivable.updates import Update
import lazymappingstorage
def get_from_id(update):
if update.message and update.message.to_array():
return str(update.message.to_array()["from"]["id"])
if update.callback_query and update.callback_query.to_array():
return str(update.callback_query.to_array()["from"]["id"])
def get_from(update):
if update.message and update.message.to_array():
return (update.message.to_array()["from"])
if update.callback_query and update.callback_query.to_array():
return (update.callback_query.to_array()["from"])
@dataclass
class User(SaveFileObject):
# pass
id: str
a: int = 0
first_name: str= ''
is_bot: bool = False
fet_user: bool = False
token: str = ""
auth: bool= False
user: str=""
def load_from(self,d):
if isinstance(d, Update):
self.load_from(get_from(d))
return
self.update(d)
class UserManager(SaveFileMapping):
def __init__(self,file="users.yaml"):
super().__init__(file="users.yaml")
self.from_file()
def from_dict(self,d):
for k in d:
self[k]=User(**d[k])
def __getitem__(self, key):
if isinstance(key, Update):
u=self.__getitem__(get_from_id(key))
u.load_from(key)
return u
u=super().__getitem__(key)
if u is None:
u=User(id=key)
self[key]=u
return u