From 3811cf7d747a63e74e1f6b99dc366fb0bd2421c6 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 12 Jan 2023 09:20:14 +0100 Subject: [PATCH 1/5] 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() -- 2.45.2 From c9aeb684d6997e483ebc6041a2512dc76adf5bed Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 12 Jan 2023 09:34:21 +0100 Subject: [PATCH 2/5] Added support to remove xp on message delete #178 --- kdb-bot/src/modules/base/base_module.py | 2 + .../events/base_on_message_delete_event.py | 83 +++++++++++++++++++ .../base/events/base_on_message_event.py | 4 +- 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 kdb-bot/src/modules/base/events/base_on_message_delete_event.py diff --git a/kdb-bot/src/modules/base/base_module.py b/kdb-bot/src/modules/base/base_module.py index 7477d701..7ae72781 100644 --- a/kdb-bot/src/modules/base/base_module.py +++ b/kdb-bot/src/modules/base/base_module.py @@ -19,6 +19,7 @@ from modules.base.events.base_on_command_error_event import BaseOnCommandErrorEv from modules.base.events.base_on_command_event import BaseOnCommandEvent from modules.base.events.base_on_member_join_event import BaseOnMemberJoinEvent from modules.base.events.base_on_member_remove_event import BaseOnMemberRemoveEvent +from modules.base.events.base_on_message_delete_event import BaseOnMessageDeleteEvent from modules.base.events.base_on_message_event import BaseOnMessageEvent from modules.base.events.base_on_raw_reaction_add import BaseOnRawReactionAddEvent from modules.base.events.base_on_raw_reaction_remove import BaseOnRawReactionRemoveEvent @@ -55,6 +56,7 @@ class BaseModule(ModuleABC): self._dc.add_event(DiscordEventTypesEnum.on_member_join.value, BaseOnMemberJoinEvent) self._dc.add_event(DiscordEventTypesEnum.on_member_remove.value, BaseOnMemberRemoveEvent) self._dc.add_event(DiscordEventTypesEnum.on_message.value, BaseOnMessageEvent) + self._dc.add_event(DiscordEventTypesEnum.on_message_delete.value, BaseOnMessageDeleteEvent) self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_add.value, BaseOnRawReactionAddEvent) self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_remove.value, BaseOnRawReactionRemoveEvent) self._dc.add_event(DiscordEventTypesEnum.on_voice_state_update.value, BaseOnVoiceStateUpdateEvent) diff --git a/kdb-bot/src/modules/base/events/base_on_message_delete_event.py b/kdb-bot/src/modules/base/events/base_on_message_delete_event.py new file mode 100644 index 00000000..07980b1d --- /dev/null +++ b/kdb-bot/src/modules/base/events/base_on_message_delete_event.py @@ -0,0 +1,83 @@ +from typing import Optional + +import discord +from cpl_core.database.context import DatabaseContextABC +from cpl_discord.events import OnMessageDeleteABC +from cpl_discord.service import DiscordBotServiceABC + +from bot_core.helper.log_message_helper import LogMessageHelper +from bot_core.logging.message_logger import MessageLogger +from bot_data.abc.client_repository_abc import ClientRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_repository_abc import UserRepositoryABC +from bot_data.model.user import User +from modules.base.abc.base_helper_abc import BaseHelperABC +from modules.base.configuration.base_server_settings import BaseServerSettings + + +class BaseOnMessageDeleteEvent(OnMessageDeleteABC): + + def __init__( + self, + logger: MessageLogger, + bhs: BaseHelperABC, + db: DatabaseContextABC, + bot: DiscordBotServiceABC, + users: UserRepositoryABC, + clients: ClientRepositoryABC, + servers: ServerRepositoryABC, + ): + OnMessageDeleteABC.__init__(self) + self._logger = logger + self._base_helper = bhs + self._db = db + self._bot = bot + self._users = users + self._clients = clients + self._servers = servers + + def _append_received_message_count(self, g_id: int): + try: + self._clients.append_deleted_message_count(self._bot.user.id, g_id, 1) + self._db.save_changes() + except Exception as e: + self._logger.error(__name__, f'Cannot edit client {self._bot.user.id}@{g_id}', e) + + def _handle_message_delete(self, message: discord.Message): + dc_user_id = message.author.id + try: + server = self._servers.get_server_by_discord_id(message.guild.id) + except Exception as e: + self._logger.error(__name__, f'Cannot get server {message.guild.id}', e) + return + + user: Optional[User] = None + try: + user = self._users.find_user_by_discord_id_and_server_id(dc_user_id, server.server_id) + except Exception as e: + self._logger.error(__name__, f'Cannot get user {dc_user_id}', e) + return + + if user is None: + self._logger.error(__name__, f'User not found {dc_user_id}') + return + + settings: BaseServerSettings = self._base_helper.get_config(message.guild.id) + old_xp = user.xp + user.xp -= settings.xp_per_message + self._users.update_user(user) + self._db.save_changes() + + self._logger.debug(__name__, f'Removed message from user {user}. xp: from {old_xp} to {user.xp}') + + async def on_message_delete(self, message: discord.Message): + self._logger.debug(__name__, f'Module {type(self)} started') + if message is None or message.guild is None: + return + + self._logger.info(__name__, f'Received message: {LogMessageHelper.get_log_string(message)}') + self._append_received_message_count(message.guild.id) + + if not message.author.bot: + self._handle_message_delete(message) + self._logger.debug(__name__, f'Module {type(self)} stopped') diff --git a/kdb-bot/src/modules/base/events/base_on_message_event.py b/kdb-bot/src/modules/base/events/base_on_message_event.py index d9a19641..01fe9ade 100644 --- a/kdb-bot/src/modules/base/events/base_on_message_event.py +++ b/kdb-bot/src/modules/base/events/base_on_message_event.py @@ -83,10 +83,12 @@ class BaseOnMessageEvent(OnMessageABC): @EventChecks.check_is_ready() async def on_message(self, message: discord.Message): self._logger.debug(__name__, f'Module {type(self)} started') - self._logger.info(__name__, f'Received message: {LogMessageHelper.get_log_string(message)}') if message is None or message.guild is None: return + + self._logger.info(__name__, f'Received message: {LogMessageHelper.get_log_string(message)}') self._append_received_message_count(message.guild.id) if not message.author.bot: self._handle_message_for_xp(message) + self._logger.debug(__name__, f'Module {type(self)} stopped') -- 2.45.2 From 1b15e541997426ac016e473c13356eb5d3bd0338 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 12 Jan 2023 09:36:33 +0100 Subject: [PATCH 3/5] Improved formatting #178 --- kdb-bot/src/bot/application.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kdb-bot/src/bot/application.py b/kdb-bot/src/bot/application.py index 8e50e173..1e905c94 100644 --- a/kdb-bot/src/bot/application.py +++ b/kdb-bot/src/bot/application.py @@ -44,8 +44,9 @@ 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 -- 2.45.2 From 00b4e2ab662ac97cb4baddfe7e1d7bc350572c50 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 12 Jan 2023 13:51:36 +0100 Subject: [PATCH 4/5] Updated cpl-discord #178 --- kdb-bot/src/bot/bot.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kdb-bot/src/bot/bot.json b/kdb-bot/src/bot/bot.json index 8f6dd34d..34159820 100644 --- a/kdb-bot/src/bot/bot.json +++ b/kdb-bot/src/bot/bot.json @@ -19,7 +19,7 @@ "cpl-core==2022.12.1.post2", "cpl-translation==2022.12.1", "cpl-query==2022.12.2.post1", - "cpl-discord==2022.12.1.post1", + "cpl-discord==2022.12.1.post2", "Flask==2.2.2", "Flask-Classful==0.14.2", "Flask-Cors==3.0.10", -- 2.45.2 From 17b336623d8a1d856c2ff34d586ede3e61c2c24e Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 13 Jan 2023 22:59:48 +0100 Subject: [PATCH 5/5] Renamed function #178 --- .../src/modules/base/events/base_on_message_delete_event.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kdb-bot/src/modules/base/events/base_on_message_delete_event.py b/kdb-bot/src/modules/base/events/base_on_message_delete_event.py index 07980b1d..355506ef 100644 --- a/kdb-bot/src/modules/base/events/base_on_message_delete_event.py +++ b/kdb-bot/src/modules/base/events/base_on_message_delete_event.py @@ -36,7 +36,7 @@ class BaseOnMessageDeleteEvent(OnMessageDeleteABC): self._clients = clients self._servers = servers - def _append_received_message_count(self, g_id: int): + def _append_deleted_message_count(self, g_id: int): try: self._clients.append_deleted_message_count(self._bot.user.id, g_id, 1) self._db.save_changes() @@ -76,7 +76,7 @@ class BaseOnMessageDeleteEvent(OnMessageDeleteABC): return self._logger.info(__name__, f'Received message: {LogMessageHelper.get_log_string(message)}') - self._append_received_message_count(message.guild.id) + self._append_deleted_message_count(message.guild.id) if not message.author.bot: self._handle_message_delete(message) -- 2.45.2