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.is_active = False
last_punishment.save() last_punishment.save()
async def _create_empty_message(self) -> Message:
return Message()
@staticmethod @staticmethod
def _get_grouped_punishments(PunishmentClass: Type[PunishmentBase]) -> tuple[tuple[tuple[int, int], list[PunishmentBase]]]: 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')) 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 functools
import os import os
from typing import Any, Callable from typing import Callable
import telethon.tl.functions import telethon.tl.functions
from flanaapis.weather.constants import WeatherEmoji from flanaapis.weather.constants import WeatherEmoji
@@ -54,15 +54,13 @@ class FlanaTeleBot(TelegramBot, FlanaBot):
@return_if_first_empty(exclude_self_types='FlanaTeleBot', globals_=globals()) @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: 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)) chat = await super()._create_chat_from_telegram_chat(telegram_chat)
chat.config = Chat.DEFAULT_CONFIG
@return_if_first_empty(exclude_self_types='FlanaTeleBot', globals_=globals()) return Chat.from_dict(chat.to_dict())
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))
@return_if_first_empty(exclude_self_types='FlanaTeleBot', globals_=globals()) @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: 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 @user_client
async def _get_contacts_ids(self) -> list[int]: async def _get_contacts_ids(self) -> list[int]:

View File

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

View File

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

View File

@@ -1,39 +1,14 @@
from __future__ import annotations # todo0 remove in 3.11 from __future__ import annotations # todo0 remove in 3.11
import datetime
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import Iterable
from flanautils import Media, OrderedSet 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 from flanabot.models.weather_chart import WeatherChart
@dataclass(eq=False) @dataclass(eq=False)
class Message(EventComponent): class Message(MultiBotMessage):
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
weather_chart: WeatherChart = None weather_chart: WeatherChart = None
last_update: datetime.datetime = None
song_infos: OrderedSet[Media] = field(default_factory=OrderedSet) 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 import datetime
from dataclasses import dataclass from dataclasses import dataclass
from flanautils import FlanaBase, MongoBase from flanautils import DCMongoBase, FlanaBase
from multibot.models.database import db from multibot.models.database import db
@dataclass(eq=False) @dataclass(eq=False)
class PunishmentBase(MongoBase, FlanaBase): class PunishmentBase(DCMongoBase, FlanaBase):
user_id: int = None user_id: int = None
group_id: int = None group_id: int = None
until: datetime.datetime = None until: datetime.datetime = None

View File

@@ -1,20 +1,12 @@
from dataclasses import dataclass 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 from flanabot.models.punishments import Mute, Punishment
@dataclass(eq=False) @dataclass(eq=False)
class User(MultiBotUser): 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): def is_muted_on(self, group_id: int):
return group_id in self.muted_on return group_id in self.muted_on

Binary file not shown.