Added module support

This commit is contained in:
Sven Heidemann 2022-10-03 00:28:22 +02:00
parent 03497b6d5a
commit b8874a6eab
5 changed files with 119 additions and 14 deletions

View File

@ -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) \

View File

@ -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 """

View File

@ -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)

View File

@ -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

View File

@ -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)