Lev
3 years ago
8 changed files with 131 additions and 15 deletions
@ -0,0 +1,57 @@
|
||||
from __future__ import annotations |
||||
from dataclasses import dataclass, field, asdict |
||||
import typing |
||||
from datetime import date |
||||
from telebot.types import ChatMember, Chat |
||||
import time |
||||
from threading import Thread |
||||
|
||||
if typing.TYPE_CHECKING: |
||||
from bot import Bot |
||||
|
||||
|
||||
@dataclass |
||||
class Community: |
||||
chat_id: int # Telegram chat id |
||||
name: str = '' |
||||
members: typing.List[int] = field(default_factory=list) |
||||
pool: typing.List[typing.Tuple[int, int]] = field(default_factory=list) # (user_id, number_of_copies) |
||||
scheduled_meetings: typing.List[typing.Tuple[int, int, date]] = field( |
||||
default_factory=list) # (user_id_1, user_id_2, meeting date) |
||||
archived_meetings: typing.List[typing.Tuple[int, int, date]] = field(default_factory=list) |
||||
start_timestamp: int = field(default_factory=lambda: int(time.time())) |
||||
|
||||
def dict(self) -> dict: |
||||
data = asdict(self) |
||||
return data |
||||
|
||||
@classmethod |
||||
def from_dict(cls, data: dict) -> Community: |
||||
data = getattr(data, '__dict__', data) |
||||
data_ = {key: data[key] for key in data.keys() if |
||||
key in ['chat_id', 'name', 'members', 'pool', 'scheduled_meetings', 'archived_meetings', |
||||
'start_timestamp']} |
||||
self = cls(**data_) |
||||
return self |
||||
|
||||
@classmethod |
||||
def by_id(cls, chat_id: int, bot: Bot, create_if_not_exists=True) -> typing.Optional[Community]: |
||||
data = bot.get_community_from_db({'chat_id': chat_id}, create_if_not_exists) |
||||
if data is None: |
||||
return None |
||||
|
||||
def upd_data(): |
||||
chat: Chat = bot.get_chat(chat_id) |
||||
data.name = chat.title |
||||
bot.save_community(data) |
||||
|
||||
Thread(target=upd_data).start() |
||||
return data |
||||
|
||||
def add_member(self, user_id: int, bot: Bot) -> bool: |
||||
chat_member_info: ChatMember = bot.get_chat_member(self.chat_id, user_id) |
||||
if chat_member_info.is_member or chat_member_info.status in ['creator', 'administrator', 'admin']: |
||||
self.members.append(user_id) |
||||
bot.save_community(self) |
||||
return True |
||||
return False |
Loading…
Reference in new issue