From 3811cf7d747a63e74e1f6b99dc366fb0bd2421c6 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 12 Jan 2023 09:20:14 +0100 Subject: [PATCH] Added support to set cached max_message by config #178 --- kdb-bot/src/bot/application.py | 3 ++- kdb-bot/src/bot/bot.json | 10 +++++----- kdb-bot/src/bot/extension/__init__.py | 0 .../src/bot/extension/init_bot_extension.py | 20 +++++++++++++++++++ kdb-bot/src/bot/main.py | 2 ++ .../bot_core/configuration/bot_settings.py | 10 ++++++++++ 6 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 kdb-bot/src/bot/extension/__init__.py create mode 100644 kdb-bot/src/bot/extension/init_bot_extension.py diff --git a/kdb-bot/src/bot/application.py b/kdb-bot/src/bot/application.py index 1cfa2caa..8e50e173 100644 --- a/kdb-bot/src/bot/application.py +++ b/kdb-bot/src/bot/application.py @@ -44,7 +44,8 @@ class Application(DiscordBotApplicationABC): try: self._logger.debug(__name__, f'Starting...') - if self._feature_flags.get_flag(FeatureFlagsEnum.api_module) and 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/bot.json b/kdb-bot/src/bot/bot.json index 84dbac92..8f6dd34d 100644 --- a/kdb-bot/src/bot/bot.json +++ b/kdb-bot/src/bot/bot.json @@ -16,10 +16,10 @@ "LicenseName": "MIT", "LicenseDescription": "MIT, see LICENSE for more details.", "Dependencies": [ - "cpl-core==2022.12.0", - "cpl-translation==2022.10.0.post2", - "cpl-query==2022.12.2", - "cpl-discord==2022.12.0", + "cpl-core==2022.12.1.post2", + "cpl-translation==2022.12.1", + "cpl-query==2022.12.2.post1", + "cpl-discord==2022.12.1.post1", "Flask==2.2.2", "Flask-Classful==0.14.2", "Flask-Cors==3.0.10", @@ -31,7 +31,7 @@ "icmplib==3.0.3" ], "DevDependencies": [ - "cpl-cli==2022.12.0" + "cpl-cli==2022.12.1.post2" ], "PythonVersion": ">=3.10.4", "PythonPath": {}, diff --git a/kdb-bot/src/bot/extension/__init__.py b/kdb-bot/src/bot/extension/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/kdb-bot/src/bot/extension/init_bot_extension.py b/kdb-bot/src/bot/extension/init_bot_extension.py new file mode 100644 index 00000000..b001b5ab --- /dev/null +++ b/kdb-bot/src/bot/extension/init_bot_extension.py @@ -0,0 +1,20 @@ +from cpl_core.application import ApplicationExtensionABC +from cpl_core.configuration import ConfigurationABC +from cpl_core.dependency_injection import ServiceProviderABC +from cpl_discord.service import DiscordBotServiceABC + +from bot_core.configuration.bot_settings import BotSettings + + +class InitBotExtension(ApplicationExtensionABC): + + def __init__(self): + ApplicationExtensionABC.__init__(self) + + async def run(self, config: ConfigurationABC, services: ServiceProviderABC): + settings = config.get_configuration(BotSettings) + + bot: DiscordBotServiceABC = services.get_service( + DiscordBotServiceABC, + max_messages=settings.cache_max_messages + ) diff --git a/kdb-bot/src/bot/main.py b/kdb-bot/src/bot/main.py index b5e2a6c2..72fcf7e7 100644 --- a/kdb-bot/src/bot/main.py +++ b/kdb-bot/src/bot/main.py @@ -6,6 +6,7 @@ from cpl_core.application import ApplicationBuilder from cpl_core.console import Console from bot.application import Application +from bot.extension.init_bot_extension import InitBotExtension from bot.startup import Startup from bot.startup_discord_extension import StartupDiscordExtension from bot.startup_migration_extension import StartupMigrationExtension @@ -29,6 +30,7 @@ class Program: .use_extension(StartupDiscordExtension) \ .use_extension(StartupModuleExtension) \ .use_extension(StartupMigrationExtension) \ + .use_extension(InitBotExtension) \ .use_extension(BootLogExtension) \ .use_extension(DatabaseExtension) \ .use_extension(AppApiExtension) \ diff --git a/kdb-bot/src/bot_core/configuration/bot_settings.py b/kdb-bot/src/bot_core/configuration/bot_settings.py index fbac1dfc..175effb1 100644 --- a/kdb-bot/src/bot_core/configuration/bot_settings.py +++ b/kdb-bot/src/bot_core/configuration/bot_settings.py @@ -16,6 +16,7 @@ class BotSettings(ConfigurationModelABC): self._technicians: List[int] = List(int) self._wait_for_restart = 2 self._wait_for_shutdown = 2 + self._cache_max_messages = 1000 @property def servers(self) -> List[ServerSettings]: @@ -33,6 +34,10 @@ class BotSettings(ConfigurationModelABC): def wait_for_shutdown(self) -> int: return self._wait_for_shutdown + @property + def cache_max_messages(self) -> int: + return self._cache_max_messages + def from_dict(self, settings: dict): try: self._technicians = settings["Technicians"] @@ -41,6 +46,11 @@ class BotSettings(ConfigurationModelABC): settings.pop("Technicians") settings.pop("WaitForRestart") settings.pop("WaitForShutdown") + + if 'CacheMaxMessages' in settings: + self._cache_max_messages = settings["CacheMaxMessages"] + settings.pop("CacheMaxMessages") + servers = List(ServerSettings) for s in settings: st = ServerSettings()