Improved module support #52

This commit is contained in:
Sven Heidemann 2022-10-03 01:05:34 +02:00
parent b8874a6eab
commit 05fbe72304
9 changed files with 128 additions and 48 deletions

View File

@ -20,6 +20,7 @@ class Program:
self.app: Optional[Application] = None self.app: Optional[Application] = None
async def start(self): async def start(self):
# discord extension has to be loaded before modules (modules depends on discord stuff)
app_builder = ApplicationBuilder(Application) \ app_builder = ApplicationBuilder(Application) \
.use_extension(StartupSettingsExtension) \ .use_extension(StartupSettingsExtension) \
.use_extension(StartupDiscordExtension) \ .use_extension(StartupDiscordExtension) \

View File

@ -8,6 +8,7 @@ from cpl_core.database import DatabaseSettings
from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC
from cpl_core.environment import ApplicationEnvironment from cpl_core.environment import ApplicationEnvironment
from cpl_core.logging import LoggerABC 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.client_utils_service_abc import ClientUtilsServiceABC
from bot_core.abc.custom_file_logger_abc import CustomFileLoggerABC from bot_core.abc.custom_file_logger_abc import CustomFileLoggerABC
@ -57,7 +58,6 @@ class Startup(StartupABC):
services.add_singleton(LoggerABC, MessageLogger) services.add_singleton(LoggerABC, MessageLogger)
services.add_translation() services.add_translation()
services.add_db_context(DBContext, self._config.get_configuration(DatabaseSettings)) services.add_db_context(DBContext, self._config.get_configuration(DatabaseSettings))
# general services # general services

View File

@ -31,54 +31,11 @@ from modules.permission.events.permission_on_ready_event import PermissionOnRead
class StartupDiscordExtension(StartupExtensionABC): class StartupDiscordExtension(StartupExtensionABC):
def __init__(self): def __init__(self):
self._feature_flags: Optional[FeatureFlagsSettings] = None pass
def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC): def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC):
self._feature_flags = config.get_configuration(FeatureFlagsSettings) pass
def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC):
services.add_discord() services.add_discord()
dc = get_discord_collection(services) dcc = 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)

View File

@ -9,7 +9,12 @@ from cpl_discord.service.discord_collection_abc import DiscordCollectionABC
from cpl_query.extension import List from cpl_query.extension import List
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings 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.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): class StartupModuleExtension(StartupExtensionABC):
@ -19,7 +24,12 @@ class StartupModuleExtension(StartupExtensionABC):
self._feature_flags: Optional[FeatureFlagsSettings] = None self._feature_flags: Optional[FeatureFlagsSettings] = None
self._modules = List(type, [ self._modules = List(type, [
BaseModule AdminModule,
BaseModule,
DatabaseModule,
ModeratorModule,
PermissionModule,
BootLogModule # has to be last!
]) ])
def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC): def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC):

View File

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

View File

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

View File

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

View File

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

View File

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