diff --git a/src/gismo/appsettings.development.json b/src/gismo/appsettings.development.json index 2b42152..d8dee8c 100644 --- a/src/gismo/appsettings.development.json +++ b/src/gismo/appsettings.development.json @@ -21,10 +21,12 @@ "Prefix": "!dev-g", "Servers": [ { - "Id": "511824600884051979" + "Id": "511824600884051979", + "MessageDeleteTimer": 2 }, { - "Id": "910199451145076828" + "Id": "910199451145076828", + "MessageDeleteTimer": 2 } ] } diff --git a/src/gismo/startup.py b/src/gismo/startup.py index e96b327..cd31396 100644 --- a/src/gismo/startup.py +++ b/src/gismo/startup.py @@ -7,9 +7,10 @@ from cpl_core.dependency_injection import (ServiceCollectionABC, ServiceProviderABC) from cpl_core.environment import ApplicationEnvironment from cpl_core.logging import LoggerABC - from gismo_core.abc.bot_service_abc import BotServiceABC +from gismo_core.abc.message_service_abc import MessageServiceABC from gismo_core.service.bot_service import BotService +from gismo_core.service.message_service import MessageService from modules.boot_log.boot_log import BootLog from modules_core.abc.module_abc import ModuleABC from modules_core.abc.module_service_abc import ModuleServiceABC @@ -41,6 +42,7 @@ class Startup(StartupABC): services.add_singleton(ModuleServiceABC, ModuleService) services.add_singleton(BotServiceABC, BotService) + services.add_transient(MessageServiceABC, MessageService) services.add_transient(ModuleABC, BootLog) diff --git a/src/gismo_core/abc/message_service_abc.py b/src/gismo_core/abc/message_service_abc.py new file mode 100644 index 0000000..5c0ab65 --- /dev/null +++ b/src/gismo_core/abc/message_service_abc.py @@ -0,0 +1,24 @@ +from abc import ABC, abstractmethod +from typing import Union + +import discord +from cpl_query.extension import List +from discord.ext.commands import Context + + +class MessageServiceABC(ABC): + + @abstractmethod + def __init__(self): pass + + @abstractmethod + async def delete_message(self, messages: List[discord.Message]): pass + + @abstractmethod + async def send_channel_message(self, channel: discord.TextChannel, message: str): pass + + @abstractmethod + async def send_dm_message(self, message: str, receiver: Union[discord.User, discord.Member]): pass + + @abstractmethod + async def send_ctx_msg(self, ctx: Context, message: str, file: discord.File = None): pass diff --git a/src/gismo_core/configuration/server_settings.py b/src/gismo_core/configuration/server_settings.py index 6d067b3..0f11b16 100644 --- a/src/gismo_core/configuration/server_settings.py +++ b/src/gismo_core/configuration/server_settings.py @@ -9,15 +9,21 @@ class ServerSettings(ConfigurationModelABC): def __init__(self): ConfigurationModelABC.__init__(self) - self._id: int = '' + self._id: int = 0 + self._message_delete_timer: int = 0 @property def id(self) -> str: return self._id + + @property + def message_delete_timer(self) -> int: + return self._message_delete_timer def from_dict(self, settings: dict): try: - self._id = settings['Id'] + self._id = int(settings['Id']) + self._message_delete_timer = int(settings['MessageDeleteTimer']) except Exception as e: Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in settings') Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}') diff --git a/src/gismo_core/service/message_service.py b/src/gismo_core/service/message_service.py new file mode 100644 index 0000000..f97aca0 --- /dev/null +++ b/src/gismo_core/service/message_service.py @@ -0,0 +1,40 @@ +import asyncio +from typing import Union + +import discord +from cpl_core.configuration.configuration_abc import ConfigurationABC +from cpl_core.logging import LoggerABC +from cpl_query.extension import List +from discord.ext.commands import Context + +from gismo_core.abc.bot_service_abc import BotServiceABC +from gismo_core.abc.message_service_abc import MessageServiceABC +from gismo_core.configuration.server_settings import ServerSettings + + +class MessageService(MessageServiceABC): + + def __init__(self, config: ConfigurationABC, logger: LoggerABC, bot: BotServiceABC): + self._config = config + self._logger = logger + self._bot = bot + + async def delete_message(self, messages: List[discord.Message]): + self._logger.debug(__name__, f'Try to delete {messages.count()} messages') + for message in messages: + message: discord.Message = message + server_st: ServerSettings = self._config.get_configuration(f'DSERVER_{message.g.id}') + await asyncio.sleep(server_st.message_delete_timer) + try: + self._logger.trace(__name__, f'Try to delete message: {message.content}') + await message.delete() + self._logger.trace(__name__, 'Deleted message') + except Exception as e: + self._logger.warn(__name__, f'Deleting message failed') + self._logger.debug(__name__, 'Deleting messages finished') + + async def send_channel_message(self, channel: discord.TextChannel, message: str): pass + + async def send_dm_message(self, message: str, receiver: Union[discord.User, discord.Member]): pass + + async def send_ctx_msg(self, ctx: Context, message: str, file: discord.File = None): pass