From 05fbe7230435c4fd30e912352c86289bba395188 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 3 Oct 2022 01:05:34 +0200 Subject: [PATCH] Improved module support #52 --- src/bot/main.py | 1 + src/bot/startup.py | 2 +- src/bot/startup_discord_extension.py | 49 ++------------------- src/bot/startup_module_extension.py | 12 ++++- src/modules/admin/admin_module.py | 23 ++++++++++ src/modules/boot_log/boot_log_module.py | 22 +++++++++ src/modules/database/database_module.py | 22 +++++++++ src/modules/moderator/moderator_module.py | 21 +++++++++ src/modules/permission/permission_module.py | 24 ++++++++++ 9 files changed, 128 insertions(+), 48 deletions(-) create mode 100644 src/modules/admin/admin_module.py create mode 100644 src/modules/boot_log/boot_log_module.py create mode 100644 src/modules/database/database_module.py create mode 100644 src/modules/moderator/moderator_module.py create mode 100644 src/modules/permission/permission_module.py diff --git a/src/bot/main.py b/src/bot/main.py index 6f8cfd76..b00c9418 100644 --- a/src/bot/main.py +++ b/src/bot/main.py @@ -20,6 +20,7 @@ class Program: self.app: Optional[Application] = None async def start(self): + # discord extension has to be loaded before modules (modules depends on discord stuff) app_builder = ApplicationBuilder(Application) \ .use_extension(StartupSettingsExtension) \ .use_extension(StartupDiscordExtension) \ diff --git a/src/bot/startup.py b/src/bot/startup.py index 177d3a9e..55df0788 100644 --- a/src/bot/startup.py +++ b/src/bot/startup.py @@ -8,6 +8,7 @@ from cpl_core.database import DatabaseSettings from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC from cpl_core.environment import ApplicationEnvironment from cpl_core.logging import LoggerABC +from cpl_discord import get_discord_collection from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC from bot_core.abc.custom_file_logger_abc import CustomFileLoggerABC @@ -57,7 +58,6 @@ class Startup(StartupABC): services.add_singleton(LoggerABC, MessageLogger) services.add_translation() - services.add_db_context(DBContext, self._config.get_configuration(DatabaseSettings)) # general services diff --git a/src/bot/startup_discord_extension.py b/src/bot/startup_discord_extension.py index 70f610bb..e0edbfde 100644 --- a/src/bot/startup_discord_extension.py +++ b/src/bot/startup_discord_extension.py @@ -31,54 +31,11 @@ from modules.permission.events.permission_on_ready_event import PermissionOnRead class StartupDiscordExtension(StartupExtensionABC): def __init__(self): - self._feature_flags: Optional[FeatureFlagsSettings] = None + pass def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC): - self._feature_flags = config.get_configuration(FeatureFlagsSettings) + pass def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): services.add_discord() - dc = get_discord_collection(services) - """modules""" - if self._feature_flags.admin_module: - """ commands """ - dc.add_command(RestartCommand) - dc.add_command(ShutdownCommand) - """ events """ - - # if self._feature_flags.base_module: - # """ commands """ - # dc.add_command(AFKCommand) - # dc.add_command(HelpCommand) - # dc.add_command(InfoCommand) - # dc.add_command(PingCommand) - # dc.add_command(UserInfoCommand) - # """ events """ - # dc.add_event(DiscordEventTypesEnum.on_command.value, BaseOnCommandEvent) - # dc.add_event(DiscordEventTypesEnum.on_command_error.value, BaseOnCommandErrorEvent) - # dc.add_event(DiscordEventTypesEnum.on_member_join.value, BaseOnMemberJoinEvent) - # dc.add_event(DiscordEventTypesEnum.on_member_join.value, BaseOnMemberRemoveEvent) - # dc.add_event(DiscordEventTypesEnum.on_message.value, BaseOnMessageEvent) - # dc.add_event(DiscordEventTypesEnum.on_voice_state_update.value, BaseOnVoiceStateUpdateEvent) - - if self._feature_flags.database_module: - """ commands """ - """ events """ - dc.add_event(DiscordEventTypesEnum.on_ready.value, DatabaseOnReadyEvent) - - if self._feature_flags.moderator_module: - """ commands """ - dc.add_command(PurgeCommand) - """ events """ - - if self._feature_flags.permission_module: - """ commands """ - """ events """ - dc.add_event(DiscordEventTypesEnum.on_ready.value, PermissionOnReadyEvent) - dc.add_event(DiscordEventTypesEnum.on_member_update.value, PermissionOnMemberUpdateEvent) - - # has to be last! - if self._feature_flags.boot_log_module: - """ commands """ - """ events """ - dc.add_event(DiscordEventTypesEnum.on_ready.value, BootLogOnReadyEvent) + dcc = get_discord_collection(services) diff --git a/src/bot/startup_module_extension.py b/src/bot/startup_module_extension.py index d914039b..c93ff29c 100644 --- a/src/bot/startup_module_extension.py +++ b/src/bot/startup_module_extension.py @@ -9,7 +9,12 @@ from cpl_discord.service.discord_collection_abc import DiscordCollectionABC from cpl_query.extension import List from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings +from modules.admin.admin_module import AdminModule from modules.base.base_module import BaseModule +from modules.boot_log.boot_log_module import BootLogModule +from modules.database.database_module import DatabaseModule +from modules.moderator.moderator_module import ModeratorModule +from modules.permission.permission_module import PermissionModule class StartupModuleExtension(StartupExtensionABC): @@ -19,7 +24,12 @@ class StartupModuleExtension(StartupExtensionABC): self._feature_flags: Optional[FeatureFlagsSettings] = None self._modules = List(type, [ - BaseModule + AdminModule, + BaseModule, + DatabaseModule, + ModeratorModule, + PermissionModule, + BootLogModule # has to be last! ]) def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC): diff --git a/src/modules/admin/admin_module.py b/src/modules/admin/admin_module.py new file mode 100644 index 00000000..ae6c86a8 --- /dev/null +++ b/src/modules/admin/admin_module.py @@ -0,0 +1,23 @@ +from cpl_core.configuration import ConfigurationABC +from cpl_core.dependency_injection import ServiceCollectionABC +from cpl_core.environment import ApplicationEnvironmentABC +from cpl_discord.service.discord_collection_abc import DiscordCollectionABC + +from bot_core.abc.module_abc import ModuleABC +from modules.admin.command.restart_command import RestartCommand +from modules.admin.command.shutdown_command import ShutdownCommand + + +class AdminModule(ModuleABC): + + def __init__(self, dc: DiscordCollectionABC): + ModuleABC.__init__(self, dc, lambda x: x.admin_module) + + def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC): + pass + + def configure_services(self, service: ServiceCollectionABC, env: ApplicationEnvironmentABC): + # commands + self._dc.add_command(RestartCommand) + self._dc.add_command(ShutdownCommand) + # events diff --git a/src/modules/boot_log/boot_log_module.py b/src/modules/boot_log/boot_log_module.py new file mode 100644 index 00000000..7f533ce1 --- /dev/null +++ b/src/modules/boot_log/boot_log_module.py @@ -0,0 +1,22 @@ +from cpl_core.configuration import ConfigurationABC +from cpl_core.dependency_injection import ServiceCollectionABC +from cpl_core.environment import ApplicationEnvironmentABC +from cpl_discord.discord_event_types_enum import DiscordEventTypesEnum +from cpl_discord.service.discord_collection_abc import DiscordCollectionABC + +from bot_core.abc.module_abc import ModuleABC +from modules.boot_log.boot_log_on_ready_event import BootLogOnReadyEvent + + +class BootLogModule(ModuleABC): + + def __init__(self, dc: DiscordCollectionABC): + ModuleABC.__init__(self, dc, lambda x: x.boot_log_module) + + def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC): + pass + + def configure_services(self, service: ServiceCollectionABC, env: ApplicationEnvironmentABC): + # commands + # events + self._dc.add_event(DiscordEventTypesEnum.on_ready.value, BootLogOnReadyEvent) diff --git a/src/modules/database/database_module.py b/src/modules/database/database_module.py new file mode 100644 index 00000000..4deedcbf --- /dev/null +++ b/src/modules/database/database_module.py @@ -0,0 +1,22 @@ +from cpl_core.configuration import ConfigurationABC +from cpl_core.dependency_injection import ServiceCollectionABC +from cpl_core.environment import ApplicationEnvironmentABC +from cpl_discord.discord_event_types_enum import DiscordEventTypesEnum +from cpl_discord.service.discord_collection_abc import DiscordCollectionABC + +from bot_core.abc.module_abc import ModuleABC +from modules.database.database_on_ready_event import DatabaseOnReadyEvent + + +class DatabaseModule(ModuleABC): + + def __init__(self, dc: DiscordCollectionABC): + ModuleABC.__init__(self, dc, lambda x: x.database_module) + + def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC): + pass + + def configure_services(self, service: ServiceCollectionABC, env: ApplicationEnvironmentABC): + # commands + # events + self._dc.add_event(DiscordEventTypesEnum.on_ready.value, DatabaseOnReadyEvent) diff --git a/src/modules/moderator/moderator_module.py b/src/modules/moderator/moderator_module.py new file mode 100644 index 00000000..6f0d6013 --- /dev/null +++ b/src/modules/moderator/moderator_module.py @@ -0,0 +1,21 @@ +from cpl_core.configuration import ConfigurationABC +from cpl_core.dependency_injection import ServiceCollectionABC +from cpl_core.environment import ApplicationEnvironmentABC +from cpl_discord.service.discord_collection_abc import DiscordCollectionABC + +from bot_core.abc.module_abc import ModuleABC +from modules.moderator.command.purge_command import PurgeCommand + + +class ModeratorModule(ModuleABC): + + def __init__(self, dc: DiscordCollectionABC): + ModuleABC.__init__(self, dc, lambda x: x.moderator_module) + + def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC): + pass + + def configure_services(self, service: ServiceCollectionABC, env: ApplicationEnvironmentABC): + # commands + self._dc.add_command(PurgeCommand) + # events diff --git a/src/modules/permission/permission_module.py b/src/modules/permission/permission_module.py new file mode 100644 index 00000000..a5f0b5a7 --- /dev/null +++ b/src/modules/permission/permission_module.py @@ -0,0 +1,24 @@ +from cpl_core.configuration import ConfigurationABC +from cpl_core.dependency_injection import ServiceCollectionABC +from cpl_core.environment import ApplicationEnvironmentABC +from cpl_discord.discord_event_types_enum import DiscordEventTypesEnum +from cpl_discord.service.discord_collection_abc import DiscordCollectionABC + +from bot_core.abc.module_abc import ModuleABC +from modules.permission.events.permission_on_member_update_event import PermissionOnMemberUpdateEvent +from modules.permission.events.permission_on_ready_event import PermissionOnReadyEvent + + +class PermissionModule(ModuleABC): + + def __init__(self, dc: DiscordCollectionABC): + ModuleABC.__init__(self, dc, lambda x: x.permission_module) + + def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC): + pass + + def configure_services(self, service: ServiceCollectionABC, env: ApplicationEnvironmentABC): + # commands + # events + self._dc.add_event(DiscordEventTypesEnum.on_ready.value, PermissionOnReadyEvent) + self._dc.add_event(DiscordEventTypesEnum.on_member_update.value, PermissionOnMemberUpdateEvent)