From 6509f628ca39b22a6a130d36df1c3bcf8457e998 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 17 Nov 2021 19:43:00 +0100 Subject: [PATCH] Added on_message handling --- src/gismo/appsettings.development.json | 2 +- src/modules/boot_log/boot_log.py | 8 +++++--- src/modules_core/abc/module_abc.py | 7 +------ src/modules_core/abc/on_message_abc.py | 11 +++++++++++ src/modules_core/abc/on_ready_abc.py | 10 ++++++++++ src/modules_core/service/module_service.py | 21 +++++++++++++++++---- 6 files changed, 45 insertions(+), 14 deletions(-) create mode 100644 src/modules_core/abc/on_message_abc.py create mode 100644 src/modules_core/abc/on_ready_abc.py diff --git a/src/gismo/appsettings.development.json b/src/gismo/appsettings.development.json index 19d2377..1bacb79 100644 --- a/src/gismo/appsettings.development.json +++ b/src/gismo/appsettings.development.json @@ -8,7 +8,7 @@ "LoggingSettings": { "Path": "logs/", - "Filename": "log_$start_time.log", + "Filename": "log_dev.log", "ConsoleLogLevel": "TRACE", "FileLogLevel": "TRACE" }, diff --git a/src/modules/boot_log/boot_log.py b/src/modules/boot_log/boot_log.py index 42ea00f..df54145 100644 --- a/src/modules/boot_log/boot_log.py +++ b/src/modules/boot_log/boot_log.py @@ -1,17 +1,19 @@ from datetime import datetime +import discord +from discord import guild +from async_timeout import asyncio from cpl_core.configuration import ConfigurationABC from cpl_core.console import Console from cpl_core.logging import LoggerABC, LoggingLevelEnum, LoggingSettings -from discord import guild - 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 from modules_core.abc.module_abc import ModuleABC +from modules_core.abc.on_ready_abc import OnReadyABC -class BootLog(ModuleABC): +class BootLog(ModuleABC, OnReadyABC): def __init__( self, diff --git a/src/modules_core/abc/module_abc.py b/src/modules_core/abc/module_abc.py index 836c29a..a9645a7 100644 --- a/src/modules_core/abc/module_abc.py +++ b/src/modules_core/abc/module_abc.py @@ -1,13 +1,8 @@ from abc import ABC, abstractmethod -from cpl_query.extension import List -from modules_core.events_enum import EventsEnum +import discord class ModuleABC(ABC): @abstractmethod def __init__(self): pass - - # @property - # @abstractmethod - # def events(self) -> List[EventsEnum]: pass diff --git a/src/modules_core/abc/on_message_abc.py b/src/modules_core/abc/on_message_abc.py new file mode 100644 index 0000000..08e3816 --- /dev/null +++ b/src/modules_core/abc/on_message_abc.py @@ -0,0 +1,11 @@ +from abc import ABC, abstractmethod +import discord + + +class OnMessageABC(ABC): + + @abstractmethod + def __init__(self): pass + + @abstractmethod + async def on_message(self, message: discord.Message): pass diff --git a/src/modules_core/abc/on_ready_abc.py b/src/modules_core/abc/on_ready_abc.py new file mode 100644 index 0000000..053bf79 --- /dev/null +++ b/src/modules_core/abc/on_ready_abc.py @@ -0,0 +1,10 @@ +from abc import ABC, abstractmethod + + +class OnReadyABC(ABC): + + @abstractmethod + def __init__(self): pass + + @abstractmethod + async def on_ready(self): pass diff --git a/src/modules_core/service/module_service.py b/src/modules_core/service/module_service.py index 94a4e17..6e41724 100644 --- a/src/modules_core/service/module_service.py +++ b/src/modules_core/service/module_service.py @@ -1,3 +1,4 @@ +from async_timeout import asyncio import discord from discord.ext import commands @@ -7,7 +8,8 @@ from cpl_core.logging import LoggerABC from cpl_query.extension import List from modules_core.abc.module_abc import ModuleABC from modules_core.abc.module_service_abc import ModuleServiceABC -from modules_core.events_enum import EventsEnum +from modules_core.abc.on_message_abc import OnMessageABC +from modules_core.abc.on_ready_abc import OnReadyABC class ModuleService(ModuleServiceABC): @@ -21,10 +23,21 @@ class ModuleService(ModuleServiceABC): async def on_ready(self): self._logger.debug(__name__, 'Start on_ready modules') - modules = self._modules.where(lambda m: hasattr(m, EventsEnum.on_ready.value) and callable(m.on_ready)) + modules = self._modules.where(lambda m: issubclass(m, OnReadyABC)) for module_type in modules: - module = self._services.get_service(module_type) + module_type = module_type + module: OnReadyABC = self._services.get_service(module_type) await module.on_ready() + + self._logger.debug(__name__, 'Stopped on_ready modules') async def on_message(self, message: discord.Message): - pass \ No newline at end of file + self._logger.debug(__name__, 'Start on_message modules') + modules = self._modules.where(lambda m: issubclass(m, OnMessageABC)) + + for module_type in modules: + module_type = module_type + module: OnMessageABC = self._services.get_service(module_type) + await module.on_message(message) + + self._logger.debug(__name__, f'Stopped on_message modules') \ No newline at end of file