from __future__ import annotations from dataclasses import dataclass, field, asdict import attr import typing import time @dataclass class User: user_id: int chat_id: int = 0 locale: str = '' start_timestamp: int = field(default_factory=lambda: int(time.time())) last_action_timestamp: int = field(default_factory=lambda: int(time.time())) def dict(self) -> dict: data = asdict(self) data['last_action_timestamp'] = int(time.time()) return data @classmethod def from_dict(cls, data: dict) -> User: data = getattr(data, '__dict__', data) data_ = {key: data[key] for key in data.keys() if key in ['user_id', 'chat_id', 'locale']} self = cls(**data_) return self @classmethod def by_id(cls, user_id: int, bot) -> typing.Optional[User]: data = bot.get_user_from_db({'user_id': user_id}) if data is None: return None return data @property def id(self): return self._id def is_admin(self) -> bool: return self.user_id in [218952152]