refactor major
This commit is contained in:
59
bot1/users.py
Normal file
59
bot1/users.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from misc import SaveFileMapping, SaveFileObject
|
||||
from dataclasses import dataclass
|
||||
from pytgbot.api_types.receivable.updates import Update
|
||||
|
||||
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):
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user