Update models to work with multibot

This commit is contained in:
AlberLC
2022-01-11 22:33:40 +01:00
parent 673e608752
commit 0b594c80d3
8 changed files with 31 additions and 64 deletions

View File

@@ -213,6 +213,9 @@ class FlanaBot(MultiBot, ABC):
last_punishment.is_active = False
last_punishment.save()
async def _create_empty_message(self) -> Message:
return Message()
@staticmethod
def _get_grouped_punishments(PunishmentClass: Type[PunishmentBase]) -> tuple[tuple[tuple[int, int], list[PunishmentBase]]]:
sorted_punishments = PunishmentClass.find(sort_keys=('user_id', 'group_id', 'until'))

View File

@@ -2,7 +2,7 @@ from __future__ import annotations # todo0 remove in 3.11
import functools
import os
from typing import Any, Callable
from typing import Callable
import telethon.tl.functions
from flanaapis.weather.constants import WeatherEmoji
@@ -54,15 +54,13 @@ class FlanaTeleBot(TelegramBot, FlanaBot):
@return_if_first_empty(exclude_self_types='FlanaTeleBot', globals_=globals())
async def _create_chat_from_telegram_chat(self, telegram_chat: multibot_constants.TELEGRAM_CHAT) -> Chat | None:
return Chat.from_event_component(await super()._create_chat_from_telegram_chat(telegram_chat))
@return_if_first_empty(exclude_self_types='FlanaTeleBot', globals_=globals())
async def _create_bot_message_from_telegram_bot_message(self, original_message: multibot_constants.TELEGRAM_MESSAGE, message: Message, content: Any = None) -> Message | None:
return Message.from_event_component(await super()._create_bot_message_from_telegram_bot_message(original_message, message))
chat = await super()._create_chat_from_telegram_chat(telegram_chat)
chat.config = Chat.DEFAULT_CONFIG
return Chat.from_dict(chat.to_dict())
@return_if_first_empty(exclude_self_types='FlanaTeleBot', globals_=globals())
async def _create_user_from_telegram_user(self, original_user: multibot_constants.TELEGRAM_USER, group_id: int = None) -> User | None:
return User.from_event_component(await super()._create_user_from_telegram_user(original_user, group_id))
return User.from_dict((await super()._create_user_from_telegram_user(original_user, group_id)).to_dict())
@user_client
async def _get_contacts_ids(self) -> list[int]:

View File

@@ -1,16 +1,18 @@
import asyncio
import os
import flanautils
from flanabot.bots.flana_disc_bot import FlanaDiscBot
from flanabot.bots.flana_tele_bot import FlanaTeleBot
async def main():
flana_disc_bot = FlanaDiscBot()
os.environ |= flanautils.find_environment_variables('../.env')
flana_tele_bot = FlanaTeleBot()
await asyncio.gather(
# flana_disc_bot.start(),
flana_tele_bot.start(),
flana_tele_bot.start()
)

View File

@@ -1,7 +1,10 @@
from dataclasses import dataclass, field
from dataclasses import dataclass
from multibot import Chat as MultiBotChat, EventComponent, T
from multibot import Chat as MultiBotChat
@dataclass(eq=False)
class Chat(MultiBotChat):
DEFAULT_CONFIG = {'auto_clear': False,
'auto_covid_chart': True,
'auto_currency_chart': True,
@@ -9,13 +12,7 @@ DEFAULT_CONFIG = {'auto_clear': False,
'auto_scraping': True,
'auto_weather_chart': True}
@dataclass(eq=False)
class Chat(MultiBotChat):
config: dict[str, bool] = field(default_factory=lambda: DEFAULT_CONFIG)
@classmethod
def from_event_component(cls, event_component: EventComponent) -> T:
chat = super().from_event_component(event_component)
chat.config = DEFAULT_CONFIG
return chat
def __post_init__(self):
super().__post_init__()
if not self.config:
self.config = self.DEFAULT_CONFIG

View File

@@ -1,39 +1,14 @@
from __future__ import annotations # todo0 remove in 3.11
import datetime
from dataclasses import dataclass, field
from typing import Iterable
from flanautils import Media, OrderedSet
from multibot import EventComponent, constants as multibot_constants, db
from multibot import Message as MultiBotMessage
from flanabot.models.chat import Chat
from flanabot.models.user import User
from flanabot.models.weather_chart import WeatherChart
@dataclass(eq=False)
class Message(EventComponent):
collection = db.message
_unique_keys = ('id', 'author')
_nullable_unique_keys = ('id', 'author')
id: int | str = None
author: User = None
text: str = None
button_text: str = None
mentions: Iterable[User] = field(default_factory=list)
chat: Chat = None
replied_message: Message = None
class Message(MultiBotMessage):
weather_chart: WeatherChart = None
last_update: datetime.datetime = None
song_infos: OrderedSet[Media] = field(default_factory=OrderedSet)
is_inline: bool = None
contents: list = field(default_factory=list)
is_deleted: bool = False
original_object: multibot_constants.ORIGINAL_MESSAGE = None
original_event: multibot_constants.MESSAGE_EVENT = None
def save(self, pull_exclude: Iterable[str] = (), pull_database_priority=False, references=True):
self.last_update = datetime.datetime.now()
super().save(pull_exclude, pull_database_priority, references)

View File

@@ -1,12 +1,12 @@
import datetime
from dataclasses import dataclass
from flanautils import FlanaBase, MongoBase
from flanautils import DCMongoBase, FlanaBase
from multibot.models.database import db
@dataclass(eq=False)
class PunishmentBase(MongoBase, FlanaBase):
class PunishmentBase(DCMongoBase, FlanaBase):
user_id: int = None
group_id: int = None
until: datetime.datetime = None

View File

@@ -1,20 +1,12 @@
from dataclasses import dataclass
from multibot import User as MultiBotUser, constants as multibot_constants, db
from multibot import User as MultiBotUser
from flanabot.models.punishments import Mute, Punishment
@dataclass(eq=False)
class User(MultiBotUser):
collection = db.user
_unique_keys = 'id'
id: int = None
name: str = None
is_admin: bool = None
original_object: multibot_constants.ORIGINAL_USER = None
def is_muted_on(self, group_id: int):
return group_id in self.muted_on

Binary file not shown.