Random Coffee alternative - random meetings for Telegram chats
https://t.me/ranteabot
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.1 KiB
40 lines
1.1 KiB
from __future__ import annotations |
|
from dataclasses import dataclass, field, asdict |
|
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', 'start_timestamp', 'last_action_timestamp']} |
|
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]
|
|
|