From 90bfee23b4ac876bb9dd8cb41f59a465737ac96e Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 16 Oct 2022 21:36:08 +0200 Subject: [PATCH] Added logic to load api after bot #70 --- kdb-bot/src/bot/application.py | 5 ++--- kdb-bot/src/bot/module_list.py | 2 +- ...h_user_transformer_abc.py => transformer_abc.py} | 2 +- kdb-bot/src/bot_api/api_module.py | 5 +++++ kdb-bot/src/bot_api/event/bot_api_on_ready_event.py | 13 +++++++++++++ .../bot_api/transformer/auth_user_transformer.py | 4 ++-- 6 files changed, 24 insertions(+), 7 deletions(-) rename kdb-bot/src/bot_api/abc/{auth_user_transformer_abc.py => transformer_abc.py} (90%) create mode 100644 kdb-bot/src/bot_api/event/bot_api_on_ready_event.py diff --git a/kdb-bot/src/bot/application.py b/kdb-bot/src/bot/application.py index 6c65a6b49c..1cfa2caa12 100644 --- a/kdb-bot/src/bot/application.py +++ b/kdb-bot/src/bot/application.py @@ -43,10 +43,9 @@ class Application(DiscordBotApplicationABC): async def main(self): try: self._logger.debug(__name__, f'Starting...') - if self._feature_flags.get_flag(FeatureFlagsEnum.api_module): - self._api.start() - if self._feature_flags.get_flag(FeatureFlagsEnum.api_only) and self._environment.environment_name == 'development': + if self._feature_flags.get_flag(FeatureFlagsEnum.api_module) and self._feature_flags.get_flag(FeatureFlagsEnum.api_only) and self._environment.environment_name == 'development': + self._api.start() self._api.join() return diff --git a/kdb-bot/src/bot/module_list.py b/kdb-bot/src/bot/module_list.py index 866b7d677f..b818ad4bcd 100644 --- a/kdb-bot/src/bot/module_list.py +++ b/kdb-bot/src/bot/module_list.py @@ -21,13 +21,13 @@ class ModuleList: return List(type, [ CoreModule, # has to be first! DataModule, - ApiModule, AdminModule, AutoRoleModule, BaseModule, DatabaseModule, ModeratorModule, PermissionModule, + ApiModule, # has to be last! BootLogModule, CoreExtensionModule, diff --git a/kdb-bot/src/bot_api/abc/auth_user_transformer_abc.py b/kdb-bot/src/bot_api/abc/transformer_abc.py similarity index 90% rename from kdb-bot/src/bot_api/abc/auth_user_transformer_abc.py rename to kdb-bot/src/bot_api/abc/transformer_abc.py index fd273847c5..6ff2f67676 100644 --- a/kdb-bot/src/bot_api/abc/auth_user_transformer_abc.py +++ b/kdb-bot/src/bot_api/abc/transformer_abc.py @@ -5,7 +5,7 @@ from cpl_core.database import TableABC from bot_api.abc.dto_abc import DtoABC -class AuthUserTransformerABC: +class TransformerABC: @staticmethod @abstractmethod diff --git a/kdb-bot/src/bot_api/api_module.py b/kdb-bot/src/bot_api/api_module.py index 6474a3e522..d6d659ecba 100644 --- a/kdb-bot/src/bot_api/api_module.py +++ b/kdb-bot/src/bot_api/api_module.py @@ -4,6 +4,7 @@ from cpl_core.configuration import ConfigurationABC from cpl_core.dependency_injection import ServiceCollectionABC from cpl_core.environment import ApplicationEnvironmentABC from cpl_core.mailing import EMailClientABC, EMailClient +from cpl_discord.discord_event_types_enum import DiscordEventTypesEnum from cpl_discord.service.discord_collection_abc import DiscordCollectionABC from flask import Flask @@ -12,6 +13,7 @@ from bot_api.api import Api from bot_api.api_thread import ApiThread from bot_api.controller.gui_controller import GuiController from bot_api.controller.auth_controller import AuthController +from bot_api.event.bot_api_on_ready_event import BotApiOnReadyEvent from bot_api.service.auth_service import AuthService from bot_core.abc.module_abc import ModuleABC from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum @@ -39,3 +41,6 @@ class ApiModule(ModuleABC): services.add_transient(AuthServiceABC, AuthService) services.add_transient(AuthController) services.add_transient(GuiController) + + # cpl-discord + self._dc.add_event(DiscordEventTypesEnum.on_ready.value, BotApiOnReadyEvent) diff --git a/kdb-bot/src/bot_api/event/bot_api_on_ready_event.py b/kdb-bot/src/bot_api/event/bot_api_on_ready_event.py new file mode 100644 index 0000000000..ca63a03c77 --- /dev/null +++ b/kdb-bot/src/bot_api/event/bot_api_on_ready_event.py @@ -0,0 +1,13 @@ +from cpl_discord.events import OnReadyABC + +from bot_api.api_thread import ApiThread + + +class BotApiOnReadyEvent(OnReadyABC): + + def __init__(self, api: ApiThread): + OnReadyABC.__init__(self) + self._api = api + + async def on_ready(self): + self._api.start() diff --git a/kdb-bot/src/bot_api/transformer/auth_user_transformer.py b/kdb-bot/src/bot_api/transformer/auth_user_transformer.py index b324445b6b..3f84a9399a 100644 --- a/kdb-bot/src/bot_api/transformer/auth_user_transformer.py +++ b/kdb-bot/src/bot_api/transformer/auth_user_transformer.py @@ -1,12 +1,12 @@ from datetime import datetime, timezone -from bot_api.abc.auth_user_transformer_abc import AuthUserTransformerABC +from bot_api.abc.transformer_abc import TransformerABC from bot_api.model.auth_user_dto import AuthUserDTO from bot_data.model.auth_role_enum import AuthRoleEnum from bot_data.model.auth_user import AuthUser -class AuthUserTransformer(AuthUserTransformerABC): +class AuthUserTransformer(TransformerABC): @staticmethod def to_db(dto: AuthUser) -> AuthUser: