diff --git a/src/gismo/startup.py b/src/gismo/startup.py index 9155d35..020f76f 100644 --- a/src/gismo/startup.py +++ b/src/gismo/startup.py @@ -32,7 +32,7 @@ class Startup(StartupABC): services.add_singleton(ModuleServiceABC, ModuleService) services.add_singleton(BotServiceABC, BotService) - - services.add_singleton(ModuleABC, BootLog) + + services.add_transient(ModuleABC, BootLog) return services.build_service_provider() diff --git a/src/gismo_core/service/bot_service.py b/src/gismo_core/service/bot_service.py index 3baba01..b284d59 100644 --- a/src/gismo_core/service/bot_service.py +++ b/src/gismo_core/service/bot_service.py @@ -22,13 +22,12 @@ class BotService(BotServiceABC, commands.Bot): commands.Bot.__init__(self, command_prefix=bot_settings.prefix, help_command=None) async def start_async(self): - self._logger.trace(__name__, 'Loading modules:') - self._modules.start_modules() - self._logger.trace(__name__, 'Finished loading modules') - self._logger.trace(__name__, 'Try to connect to discord') self.run(self._discord_settings.token) # continue at on_ready + + async def on_ready(self): + await self._modules.start_modules() async def stop_async(self): try: @@ -36,3 +35,4 @@ class BotService(BotServiceABC, commands.Bot): # save data except Exception as e: self._logger.error(__name__, 'Stop failed', e) + diff --git a/src/modules/boot_log/main.py b/src/modules/boot_log/main.py index 70634f0..6a46716 100644 --- a/src/modules/boot_log/main.py +++ b/src/modules/boot_log/main.py @@ -7,12 +7,12 @@ from modules_core.abc.module_service_abc import ModuleServiceABC class BootLog(ModuleABC): - def __init__(self, logger: LoggerABC, modules: ModuleServiceABC, bot: BotServiceABC): + def __init__(self, logger: LoggerABC, bot: BotServiceABC): self._logger = logger - self._modules = modules self._bot = bot + + self._logger.info(__name__, 'LOADED') + ModuleABC.__init__(self) - modules.register(self) - - def on_ready(self): + async def on_ready(self): self._logger.info(__name__, f'Bot started: {self._bot}') diff --git a/src/modules_core/abc/module_abc.py b/src/modules_core/abc/module_abc.py index bdf1b63..836c29a 100644 --- a/src/modules_core/abc/module_abc.py +++ b/src/modules_core/abc/module_abc.py @@ -1,7 +1,13 @@ from abc import ABC, abstractmethod +from cpl_query.extension import List +from modules_core.events_enum import EventsEnum class ModuleABC(ABC): @abstractmethod def __init__(self): pass + + # @property + # @abstractmethod + # def events(self) -> List[EventsEnum]: pass diff --git a/src/modules_core/abc/module_service_abc.py b/src/modules_core/abc/module_service_abc.py index b510857..0d906ca 100644 --- a/src/modules_core/abc/module_service_abc.py +++ b/src/modules_core/abc/module_service_abc.py @@ -8,7 +8,4 @@ class ModuleServiceABC(ABC): def __init__(self): pass @abstractmethod - def register(self): pass - - @abstractmethod - def start_modules(self): pass + async def start_modules(self): pass diff --git a/src/modules_core/events_enum.py b/src/modules_core/events_enum.py new file mode 100644 index 0000000..fafe57e --- /dev/null +++ b/src/modules_core/events_enum.py @@ -0,0 +1,6 @@ +from enum import Enum + + +class EventsEnum(Enum): + + on_ready = 'on_ready' diff --git a/src/modules_core/service/module_service.py b/src/modules_core/service/module_service.py index de14dd7..359d9e6 100644 --- a/src/modules_core/service/module_service.py +++ b/src/modules_core/service/module_service.py @@ -1,7 +1,13 @@ +from os import path + +import discord +from cpl_core.dependency_injection.service_provider_abc import \ + ServiceProviderABC from cpl_core.environment.application_environment_abc import \ ApplicationEnvironmentABC from cpl_core.logging import LoggerABC from cpl_query.extension import List +from discord.ext import commands from modules_core.abc.module_abc import ModuleABC from modules_core.abc.module_service_abc import ModuleServiceABC @@ -9,13 +15,17 @@ from modules_core.abc.module_service_abc import ModuleServiceABC class ModuleService(ModuleServiceABC): - def __init__(self, logger: LoggerABC, env: ApplicationEnvironmentABC): + def __init__(self, logger: LoggerABC, services: ServiceProviderABC, env: ApplicationEnvironmentABC): self._logger = logger + self._services = services self._env = env self._modules: List[ModuleABC] = List() + self._modules.extend(ModuleABC.__subclasses__()) - def register(self, module: ModuleABC): - self._modules.append(module) - - def start_modules(self): - self._modules.for_each(lambda m: m.echo()) + async def start_modules(self): + self._logger.info(__name__, self._modules) + modules = self._modules.where(lambda m: hasattr(m, 'on_ready')) + self._logger.info(__name__, f'HIER: {modules}') + for module_type in modules: + module = self._services.get_service(module_type) + await module.on_ready()