Changed config loading from file to db #127
This commit is contained in:
		
							
								
								
									
										1
									
								
								kdb-bot/src/modules/config/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								kdb-bot/src/modules/config/__init__.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| # imports | ||||
							
								
								
									
										46
									
								
								kdb-bot/src/modules/config/config.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								kdb-bot/src/modules/config/config.json
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,46 @@ | ||||
| { | ||||
|   "ProjectSettings": { | ||||
|     "Name": "config", | ||||
|     "Version": { | ||||
|       "Major": "0", | ||||
|       "Minor": "0", | ||||
|       "Micro": "0" | ||||
|     }, | ||||
|     "Author": "", | ||||
|     "AuthorEmail": "", | ||||
|     "Description": "", | ||||
|     "LongDescription": "", | ||||
|     "URL": "", | ||||
|     "CopyrightDate": "", | ||||
|     "CopyrightName": "", | ||||
|     "LicenseName": "", | ||||
|     "LicenseDescription": "", | ||||
|     "Dependencies": [ | ||||
|       "cpl-core>=2023.4.0.post5" | ||||
|     ], | ||||
|     "DevDependencies": [ | ||||
|       "cpl-cli>=2023.4.0.post3" | ||||
|     ], | ||||
|     "PythonVersion": ">=3.10.4", | ||||
|     "PythonPath": { | ||||
|       "linux": "" | ||||
|     }, | ||||
|     "Classifiers": [] | ||||
|   }, | ||||
|   "BuildSettings": { | ||||
|     "ProjectType": "library", | ||||
|     "SourcePath": "", | ||||
|     "OutputPath": "../../dist", | ||||
|     "Main": "config.main", | ||||
|     "EntryPoint": "config", | ||||
|     "IncludePackageData": false, | ||||
|     "Included": [], | ||||
|     "Excluded": [ | ||||
|       "*/__pycache__", | ||||
|       "*/logs", | ||||
|       "*/tests" | ||||
|     ], | ||||
|     "PackageData": {}, | ||||
|     "ProjectReferences": [] | ||||
|   } | ||||
| } | ||||
							
								
								
									
										24
									
								
								kdb-bot/src/modules/config/config_extension.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								kdb-bot/src/modules/config/config_extension.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | ||||
| from cpl_core.application.application_extension_abc import ApplicationExtensionABC | ||||
| from cpl_core.configuration import ConfigurationABC | ||||
| from cpl_core.dependency_injection import ServiceProviderABC | ||||
| from cpl_core.logging import LoggerABC | ||||
|  | ||||
| from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum | ||||
| from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings | ||||
| from bot_data.abc.technician_config_repository_abc import TechnicianConfigRepositoryABC | ||||
| from bot_data.model.technician_config import TechnicianConfig | ||||
|  | ||||
|  | ||||
| class ConfigExtension(ApplicationExtensionABC): | ||||
|     def __init__(self): | ||||
|         pass | ||||
|  | ||||
|     async def run(self, config: ConfigurationABC, services: ServiceProviderABC): | ||||
|         feature_flags: FeatureFlagsSettings = config.get_configuration(FeatureFlagsSettings) | ||||
|         if not feature_flags.get_flag(FeatureFlagsEnum.config_module): | ||||
|             return | ||||
|         logger: LoggerABC = services.get_service(LoggerABC) | ||||
|         logger.debug(__name__, "Config extension started") | ||||
|         technician_config_repo: TechnicianConfigRepositoryABC = services.get_service(TechnicianConfigRepositoryABC) | ||||
|         technician_config = technician_config_repo.get_technician_config() | ||||
|         config.add_configuration(TechnicianConfig, technician_config) | ||||
							
								
								
									
										20
									
								
								kdb-bot/src/modules/config/config_module.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								kdb-bot/src/modules/config/config_module.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| 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 bot_core.configuration.feature_flags_enum import FeatureFlagsEnum | ||||
| from modules.config.events.config_on_ready_event import ConfigOnReadyEvent | ||||
|  | ||||
|  | ||||
| class ConfigModule(ModuleABC): | ||||
|     def __init__(self, dc: DiscordCollectionABC): | ||||
|         ModuleABC.__init__(self, dc, FeatureFlagsEnum.config_module) | ||||
|  | ||||
|     def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC): | ||||
|         pass | ||||
|  | ||||
|     def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): | ||||
|         self._dc.add_event(DiscordEventTypesEnum.on_ready.value, ConfigOnReadyEvent) | ||||
							
								
								
									
										1
									
								
								kdb-bot/src/modules/config/events/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								kdb-bot/src/modules/config/events/__init__.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| # imports | ||||
							
								
								
									
										35
									
								
								kdb-bot/src/modules/config/events/config_on_ready_event.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								kdb-bot/src/modules/config/events/config_on_ready_event.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,35 @@ | ||||
| from cpl_core.configuration import ConfigurationABC | ||||
| from cpl_core.logging import LoggerABC | ||||
| from cpl_discord.container import Guild | ||||
| from cpl_discord.events import OnReadyABC | ||||
| from cpl_discord.service import DiscordBotServiceABC | ||||
|  | ||||
| from bot_data.abc.server_config_repository_abc import ServerConfigRepositoryABC | ||||
| from bot_data.abc.server_repository_abc import ServerRepositoryABC | ||||
|  | ||||
|  | ||||
| class ConfigOnReadyEvent(OnReadyABC): | ||||
|     def __init__( | ||||
|         self, | ||||
|         config: ConfigurationABC, | ||||
|         logger: LoggerABC, | ||||
|         bot: DiscordBotServiceABC, | ||||
|         servers: ServerRepositoryABC, | ||||
|         server_config_repo: ServerConfigRepositoryABC, | ||||
|     ): | ||||
|         OnReadyABC.__init__(self) | ||||
|  | ||||
|         self._config = config | ||||
|         self._logger = logger | ||||
|         self._bot = bot | ||||
|         self._servers = servers | ||||
|         self._server_config_repo = server_config_repo | ||||
|  | ||||
|     async def on_ready(self): | ||||
|         for guild in self._bot.guilds: | ||||
|             guild: Guild = guild | ||||
|             server = self._servers.get_server_by_discord_id(guild.id) | ||||
|             server_config = self._server_config_repo.get_server_config(server.id) | ||||
|             self._config.add_configuration( | ||||
|                 f"{type(server_config).__name__}_{server_config.server.discord_id}", server_config | ||||
|             ) | ||||
		Reference in New Issue
	
	Block a user