From b8874a6eab7ad4f007c8de758628dfbd85210970 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 3 Oct 2022 00:28:22 +0200 Subject: [PATCH 1/2] Added module support --- src/bot/main.py | 2 ++ src/bot/startup_discord_extension.py | 28 +++++++++---------- src/bot/startup_module_extension.py | 42 ++++++++++++++++++++++++++++ src/bot_core/abc/module_abc.py | 19 +++++++++++++ src/modules/base/base_module.py | 42 ++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 14 deletions(-) create mode 100644 src/bot/startup_module_extension.py create mode 100644 src/bot_core/abc/module_abc.py create mode 100644 src/modules/base/base_module.py diff --git a/src/bot/main.py b/src/bot/main.py index 29c81fc1..6f8cfd76 100644 --- a/src/bot/main.py +++ b/src/bot/main.py @@ -8,6 +8,7 @@ from bot.application import Application from bot.startup import Startup from bot.startup_discord_extension import StartupDiscordExtension from bot.startup_migration_extension import StartupMigrationExtension +from bot.startup_module_extension import StartupModuleExtension from bot.startup_settings_extension import StartupSettingsExtension from modules.boot_log.boot_log_extension import BootLogExtension from modules.database.database_extension import DatabaseExtension @@ -22,6 +23,7 @@ class Program: app_builder = ApplicationBuilder(Application) \ .use_extension(StartupSettingsExtension) \ .use_extension(StartupDiscordExtension) \ + .use_extension(StartupModuleExtension) \ .use_extension(StartupMigrationExtension) \ .use_extension(BootLogExtension) \ .use_extension(DatabaseExtension) \ diff --git a/src/bot/startup_discord_extension.py b/src/bot/startup_discord_extension.py index dbf3281d..70f610bb 100644 --- a/src/bot/startup_discord_extension.py +++ b/src/bot/startup_discord_extension.py @@ -46,20 +46,20 @@ class StartupDiscordExtension(StartupExtensionABC): 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.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 """ diff --git a/src/bot/startup_module_extension.py b/src/bot/startup_module_extension.py new file mode 100644 index 00000000..d914039b --- /dev/null +++ b/src/bot/startup_module_extension.py @@ -0,0 +1,42 @@ +from typing import Optional + +from cpl_core.application import StartupExtensionABC +from cpl_core.configuration import ConfigurationABC +from cpl_core.console import Console, ForegroundColorEnum +from cpl_core.dependency_injection import ServiceCollectionABC +from cpl_core.environment import ApplicationEnvironmentABC +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.base.base_module import BaseModule + + +class StartupModuleExtension(StartupExtensionABC): + + def __init__(self): + self._config: Optional[ConfigurationABC] = None + self._feature_flags: Optional[FeatureFlagsSettings] = None + + self._modules = List(type, [ + BaseModule + ]) + + def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC): + self._config = config + self._feature_flags = config.get_configuration(FeatureFlagsSettings) + + def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): + provider = services.build_service_provider() + dc_collection: DiscordCollectionABC = provider.get_service(DiscordCollectionABC) + + for module_type in self._modules: + module = module_type(dc_collection) + if not module.feature_flag(self._feature_flags): + continue + + Console.set_foreground_color(ForegroundColorEnum.green) + Console.write_line(f'[{__name__}] Loaded module: {module_type}') + Console.color_reset() + module.configure_configuration(self._config, env) + module.configure_services(services, env) diff --git a/src/bot_core/abc/module_abc.py b/src/bot_core/abc/module_abc.py new file mode 100644 index 00000000..af52acf0 --- /dev/null +++ b/src/bot_core/abc/module_abc.py @@ -0,0 +1,19 @@ +from abc import abstractmethod +from typing import Callable + +from cpl_core.application import StartupExtensionABC +from cpl_discord.service.discord_collection_abc import DiscordCollectionABC + + +class ModuleABC(StartupExtensionABC): + + @abstractmethod + def __init__(self, dc: DiscordCollectionABC, feature_flag: Callable = None): + StartupExtensionABC.__init__(self) + + self._dc = dc + self._feature_flag = feature_flag + + @property + def feature_flag(self) -> Callable: + return self._feature_flag diff --git a/src/modules/base/base_module.py b/src/modules/base/base_module.py new file mode 100644 index 00000000..08d43748 --- /dev/null +++ b/src/modules/base/base_module.py @@ -0,0 +1,42 @@ +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.base.command.afk_command import AFKCommand +from modules.base.command.help_command import HelpCommand +from modules.base.command.info_command import InfoCommand +from modules.base.command.ping_command import PingCommand +from modules.base.command.user_info_command import UserInfoCommand +from modules.base.events.base_on_command_error_event import BaseOnCommandErrorEvent +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_event import BaseOnMessageEvent +from modules.base.events.base_on_voice_state_update_event import BaseOnVoiceStateUpdateEvent + + +class BaseModule(ModuleABC): + + def __init__(self, dc: DiscordCollectionABC): + ModuleABC.__init__(self, dc, lambda x: x.base_module) + + def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC): + pass + + def configure_services(self, service: ServiceCollectionABC, env: ApplicationEnvironmentABC): + # commands + self._dc.add_command(AFKCommand) + self._dc.add_command(HelpCommand) + self._dc.add_command(InfoCommand) + self._dc.add_command(PingCommand) + self._dc.add_command(UserInfoCommand) + # events + self._dc.add_event(DiscordEventTypesEnum.on_command.value, BaseOnCommandEvent) + self._dc.add_event(DiscordEventTypesEnum.on_command_error.value, BaseOnCommandErrorEvent) + self._dc.add_event(DiscordEventTypesEnum.on_member_join.value, BaseOnMemberJoinEvent) + self._dc.add_event(DiscordEventTypesEnum.on_member_join.value, BaseOnMemberRemoveEvent) + self._dc.add_event(DiscordEventTypesEnum.on_message.value, BaseOnMessageEvent) + self._dc.add_event(DiscordEventTypesEnum.on_voice_state_update.value, BaseOnVoiceStateUpdateEvent) -- 2.45.2 From 05fbe7230435c4fd30e912352c86289bba395188 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 3 Oct 2022 01:05:34 +0200 Subject: [PATCH 2/2] 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) -- 2.45.2