0.2 - Module aufgeräumt (#52) #53
@ -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
|
||||
@ -19,9 +20,11 @@ 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) \
|
||||
.use_extension(StartupModuleExtension) \
|
||||
.use_extension(StartupMigrationExtension) \
|
||||
.use_extension(BootLogExtension) \
|
||||
.use_extension(DatabaseExtension) \
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
52
src/bot/startup_module_extension.py
Normal file
52
src/bot/startup_module_extension.py
Normal file
@ -0,0 +1,52 @@
|
||||
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.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):
|
||||
|
||||
def __init__(self):
|
||||
self._config: Optional[ConfigurationABC] = None
|
||||
self._feature_flags: Optional[FeatureFlagsSettings] = None
|
||||
|
||||
self._modules = List(type, [
|
||||
AdminModule,
|
||||
BaseModule,
|
||||
DatabaseModule,
|
||||
ModeratorModule,
|
||||
PermissionModule,
|
||||
BootLogModule # has to be last!
|
||||
])
|
||||
|
||||
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)
|
19
src/bot_core/abc/module_abc.py
Normal file
19
src/bot_core/abc/module_abc.py
Normal 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
|
23
src/modules/admin/admin_module.py
Normal file
23
src/modules/admin/admin_module.py
Normal 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
|
42
src/modules/base/base_module.py
Normal file
42
src/modules/base/base_module.py
Normal 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)
|
22
src/modules/boot_log/boot_log_module.py
Normal file
22
src/modules/boot_log/boot_log_module.py
Normal 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)
|
22
src/modules/database/database_module.py
Normal file
22
src/modules/database/database_module.py
Normal 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)
|
21
src/modules/moderator/moderator_module.py
Normal file
21
src/modules/moderator/moderator_module.py
Normal 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
|
24
src/modules/permission/permission_module.py
Normal file
24
src/modules/permission/permission_module.py
Normal 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)
|
Loading…
Reference in New Issue
Block a user