Load config from db #127
This commit is contained in:
parent
efbe6efc20
commit
527d28ae39
@ -10,7 +10,6 @@
|
|||||||
"auto-role": "src/modules/auto_role/auto-role.json",
|
"auto-role": "src/modules/auto_role/auto-role.json",
|
||||||
"base": "src/modules/base/base.json",
|
"base": "src/modules/base/base.json",
|
||||||
"boot-log": "src/modules/boot_log/boot-log.json",
|
"boot-log": "src/modules/boot_log/boot-log.json",
|
||||||
"database": "src/modules/database/database.json",
|
|
||||||
"level": "src/modules/level/level.json",
|
"level": "src/modules/level/level.json",
|
||||||
"permission": "src/modules/permission/permission.json",
|
"permission": "src/modules/permission/permission.json",
|
||||||
"stats": "src/modules/stats/stats.json",
|
"stats": "src/modules/stats/stats.json",
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
"Version": {
|
"Version": {
|
||||||
"Major": "0",
|
"Major": "0",
|
||||||
"Minor": "3",
|
"Minor": "3",
|
||||||
"Micro": "1"
|
"Micro": "dev127"
|
||||||
},
|
},
|
||||||
"Author": "Sven Heidemann",
|
"Author": "Sven Heidemann",
|
||||||
"AuthorEmail": "sven.heidemann@sh-edraft.de",
|
"AuthorEmail": "sven.heidemann@sh-edraft.de",
|
||||||
@ -32,7 +32,8 @@
|
|||||||
"cpl-discord==2022.12.2"
|
"cpl-discord==2022.12.2"
|
||||||
],
|
],
|
||||||
"DevDependencies": [
|
"DevDependencies": [
|
||||||
"cpl-cli==2022.12.1.post3"
|
"cpl-cli==2022.12.1.post3",
|
||||||
|
"pygount==1.5.1"
|
||||||
],
|
],
|
||||||
"PythonVersion": ">=3.10.4",
|
"PythonVersion": ">=3.10.4",
|
||||||
"PythonPath": {},
|
"PythonPath": {},
|
||||||
@ -60,7 +61,6 @@
|
|||||||
"../modules/auto_role/auto-role.json",
|
"../modules/auto_role/auto-role.json",
|
||||||
"../modules/base/base.json",
|
"../modules/base/base.json",
|
||||||
"../modules/boot_log/boot-log.json",
|
"../modules/boot_log/boot-log.json",
|
||||||
"../modules/database/database.json",
|
|
||||||
"../modules/level/level.json",
|
"../modules/level/level.json",
|
||||||
"../modules/permission/permission.json",
|
"../modules/permission/permission.json",
|
||||||
"../modules/stats/stats.json",
|
"../modules/stats/stats.json",
|
||||||
|
@ -14,8 +14,8 @@ from bot.startup_module_extension import StartupModuleExtension
|
|||||||
from bot.startup_settings_extension import StartupSettingsExtension
|
from bot.startup_settings_extension import StartupSettingsExtension
|
||||||
from bot_api.app_api_extension import AppApiExtension
|
from bot_api.app_api_extension import AppApiExtension
|
||||||
from bot_core.core_extension.core_extension import CoreExtension
|
from bot_core.core_extension.core_extension import CoreExtension
|
||||||
|
from bot_data.data_extension.database_extension import DatabaseExtension
|
||||||
from modules.boot_log.boot_log_extension import BootLogExtension
|
from modules.boot_log.boot_log_extension import BootLogExtension
|
||||||
from modules.database.database_extension import DatabaseExtension
|
|
||||||
|
|
||||||
|
|
||||||
class Program:
|
class Program:
|
||||||
|
@ -8,7 +8,6 @@ from bot_graphql.graphql_module import GraphQLModule
|
|||||||
from modules.auto_role.auto_role_module import AutoRoleModule
|
from modules.auto_role.auto_role_module import AutoRoleModule
|
||||||
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.boot_log.boot_log_module import BootLogModule
|
||||||
from modules.database.database_module import DatabaseModule
|
|
||||||
from modules.level.level_module import LevelModule
|
from modules.level.level_module import LevelModule
|
||||||
from modules.permission.permission_module import PermissionModule
|
from modules.permission.permission_module import PermissionModule
|
||||||
from modules.stats.stats_module import StatsModule
|
from modules.stats.stats_module import StatsModule
|
||||||
@ -26,7 +25,6 @@ class ModuleList:
|
|||||||
DataModule,
|
DataModule,
|
||||||
GraphQLModule,
|
GraphQLModule,
|
||||||
PermissionModule,
|
PermissionModule,
|
||||||
DatabaseModule,
|
|
||||||
AutoRoleModule,
|
AutoRoleModule,
|
||||||
BaseModule,
|
BaseModule,
|
||||||
LevelModule,
|
LevelModule,
|
||||||
|
@ -8,6 +8,7 @@ from bot_data.migration.api_key_migration import ApiKeyMigration
|
|||||||
from bot_data.migration.api_migration import ApiMigration
|
from bot_data.migration.api_migration import ApiMigration
|
||||||
from bot_data.migration.auto_role_fix1_migration import AutoRoleFix1Migration
|
from bot_data.migration.auto_role_fix1_migration import AutoRoleFix1Migration
|
||||||
from bot_data.migration.auto_role_migration import AutoRoleMigration
|
from bot_data.migration.auto_role_migration import AutoRoleMigration
|
||||||
|
from bot_data.migration.config_migration import ConfigMigration
|
||||||
from bot_data.migration.initial_migration import InitialMigration
|
from bot_data.migration.initial_migration import InitialMigration
|
||||||
from bot_data.migration.level_migration import LevelMigration
|
from bot_data.migration.level_migration import LevelMigration
|
||||||
from bot_data.migration.stats_migration import StatsMigration
|
from bot_data.migration.stats_migration import StatsMigration
|
||||||
@ -36,3 +37,4 @@ class StartupMigrationExtension(StartupExtensionABC):
|
|||||||
services.add_transient(MigrationABC, UserMessageCountPerHourMigration) # 11.01.2023 #168 - 0.3.1
|
services.add_transient(MigrationABC, UserMessageCountPerHourMigration) # 11.01.2023 #168 - 0.3.1
|
||||||
services.add_transient(MigrationABC, ApiKeyMigration) # 09.02.2023 #162 - 1.0.0
|
services.add_transient(MigrationABC, ApiKeyMigration) # 09.02.2023 #162 - 1.0.0
|
||||||
services.add_transient(MigrationABC, UserJoinedGameServerMigration) # 12.02.2023 #181 - 1.0.0
|
services.add_transient(MigrationABC, UserJoinedGameServerMigration) # 12.02.2023 #181 - 1.0.0
|
||||||
|
services.add_transient(MigrationABC, ConfigMigration) # 18.02.2023 #127 - 1.0.0
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Callable, Type, Optional
|
|
||||||
|
|
||||||
from cpl_core.application import StartupExtensionABC
|
from cpl_core.application import StartupExtensionABC
|
||||||
from cpl_core.configuration import ConfigurationABC
|
from cpl_core.configuration import ConfigurationABC
|
||||||
@ -8,11 +7,7 @@ from cpl_core.dependency_injection import ServiceCollectionABC
|
|||||||
from cpl_core.environment import ApplicationEnvironmentABC
|
from cpl_core.environment import ApplicationEnvironmentABC
|
||||||
|
|
||||||
from bot_core.configuration.bot_logging_settings import BotLoggingSettings
|
from bot_core.configuration.bot_logging_settings import BotLoggingSettings
|
||||||
from bot_core.configuration.bot_settings import BotSettings
|
from bot_core.helper.settings_helper import SettingsHelper
|
||||||
from modules.base.configuration.base_settings import BaseSettings
|
|
||||||
from modules.boot_log.configuration.boot_log_settings import BootLogSettings
|
|
||||||
from modules.level.configuration.level_settings import LevelSettings
|
|
||||||
from modules.permission.configuration.permission_settings import PermissionSettings
|
|
||||||
|
|
||||||
|
|
||||||
class StartupSettingsExtension(StartupExtensionABC):
|
class StartupSettingsExtension(StartupExtensionABC):
|
||||||
@ -34,27 +29,9 @@ class StartupSettingsExtension(StartupExtensionABC):
|
|||||||
configuration.add_json_file(f"config/feature-flags.{environment.host_name}.json", optional=True)
|
configuration.add_json_file(f"config/feature-flags.{environment.host_name}.json", optional=True)
|
||||||
|
|
||||||
configuration.add_configuration("Startup_StartTime", str(self._start_time))
|
configuration.add_configuration("Startup_StartTime", str(self._start_time))
|
||||||
self._configure_settings_with_sub_settings(configuration, BotSettings, lambda x: x.servers, lambda x: x.id)
|
SettingsHelper.configure_settings_with_sub_settings(
|
||||||
self._configure_settings_with_sub_settings(configuration, BaseSettings, lambda x: x.servers, lambda x: x.id)
|
|
||||||
self._configure_settings_with_sub_settings(configuration, BootLogSettings, lambda x: x.servers, lambda x: x.id)
|
|
||||||
self._configure_settings_with_sub_settings(configuration, LevelSettings, lambda x: x.servers, lambda x: x.id)
|
|
||||||
self._configure_settings_with_sub_settings(
|
|
||||||
configuration, PermissionSettings, lambda x: x.servers, lambda x: x.id
|
|
||||||
)
|
|
||||||
self._configure_settings_with_sub_settings(
|
|
||||||
configuration, BotLoggingSettings, lambda x: x.files, lambda x: x.key
|
configuration, BotLoggingSettings, lambda x: x.files, lambda x: x.key
|
||||||
)
|
)
|
||||||
|
|
||||||
def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC):
|
def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _configure_settings_with_sub_settings(
|
|
||||||
config: ConfigurationABC, settings: Type, list_atr: Callable, atr: Callable
|
|
||||||
):
|
|
||||||
settings: Optional[settings] = config.get_configuration(settings)
|
|
||||||
if settings is None:
|
|
||||||
return
|
|
||||||
|
|
||||||
for sub_settings in list_atr(settings):
|
|
||||||
config.add_configuration(f"{type(sub_settings).__name__}_{atr(sub_settings)}", sub_settings)
|
|
||||||
|
17
kdb-bot/src/bot_core/helper/settings_helper.py
Normal file
17
kdb-bot/src/bot_core/helper/settings_helper.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
from typing import Type, Callable, Optional
|
||||||
|
|
||||||
|
from cpl_core.configuration import ConfigurationABC
|
||||||
|
from cpl_core.type import T
|
||||||
|
|
||||||
|
|
||||||
|
class SettingsHelper:
|
||||||
|
@staticmethod
|
||||||
|
def configure_settings_with_sub_settings(
|
||||||
|
config: ConfigurationABC, settings: Type[T], list_atr: Callable, atr: Callable
|
||||||
|
):
|
||||||
|
settings: Optional[settings] = config.get_configuration(settings)
|
||||||
|
if settings is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
for sub_settings in list_atr(settings):
|
||||||
|
config.add_configuration(f"{type(sub_settings).__name__}_{atr(sub_settings)}", sub_settings)
|
@ -4,7 +4,7 @@
|
|||||||
"Version": {
|
"Version": {
|
||||||
"Major": "0",
|
"Major": "0",
|
||||||
"Minor": "3",
|
"Minor": "3",
|
||||||
"Micro": "1"
|
"Micro": "dev127"
|
||||||
},
|
},
|
||||||
"Author": "Sven Heidemann",
|
"Author": "Sven Heidemann",
|
||||||
"AuthorEmail": "sven.heidemann@sh-edraft.de",
|
"AuthorEmail": "sven.heidemann@sh-edraft.de",
|
||||||
|
71
kdb-bot/src/bot_data/config_seeder.py
Normal file
71
kdb-bot/src/bot_data/config_seeder.py
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
from cpl_core.configuration import ConfigurationABC
|
||||||
|
from cpl_core.database.context import DatabaseContextABC
|
||||||
|
from cpl_discord.service import DiscordBotServiceABC
|
||||||
|
|
||||||
|
from bot_core.configuration.bot_settings import BotSettings
|
||||||
|
from bot_core.helper.settings_helper import SettingsHelper
|
||||||
|
from bot_core.logging.database_logger import DatabaseLogger
|
||||||
|
from bot_data.abc.data_seeder_abc import DataSeederABC
|
||||||
|
from bot_data.service.db_config import DBConfigService
|
||||||
|
from bot_data.service.seeder_service import SeederService
|
||||||
|
from modules.base.configuration.base_settings import BaseSettings
|
||||||
|
from modules.boot_log.configuration.boot_log_settings import BootLogSettings
|
||||||
|
from modules.level.configuration.level_settings import LevelSettings
|
||||||
|
from modules.permission.configuration.permission_settings import PermissionSettings
|
||||||
|
|
||||||
|
|
||||||
|
class ConfigSeeder(DataSeederABC):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
config: ConfigurationABC,
|
||||||
|
logger: DatabaseLogger,
|
||||||
|
seeder: SeederService,
|
||||||
|
bot: DiscordBotServiceABC,
|
||||||
|
db_context: DatabaseContextABC,
|
||||||
|
db_config: DBConfigService,
|
||||||
|
):
|
||||||
|
DataSeederABC.__init__(self)
|
||||||
|
self._config = config
|
||||||
|
|
||||||
|
self._logger = logger
|
||||||
|
self._seeder = seeder
|
||||||
|
self._bot = bot
|
||||||
|
self._db_context = db_context
|
||||||
|
self._db_config = db_config
|
||||||
|
|
||||||
|
async def seed(self):
|
||||||
|
try:
|
||||||
|
cfg = self._db_config.get_st_bot()
|
||||||
|
|
||||||
|
bot_settings = BotSettings()
|
||||||
|
bot_settings.from_dict(cfg)
|
||||||
|
|
||||||
|
base_settings = BaseSettings()
|
||||||
|
base_settings.from_dict(cfg)
|
||||||
|
|
||||||
|
boot_log_settings = BootLogSettings()
|
||||||
|
boot_log_settings.from_dict(cfg)
|
||||||
|
|
||||||
|
level_settings = LevelSettings()
|
||||||
|
level_settings.from_dict(cfg)
|
||||||
|
|
||||||
|
permission_settings = PermissionSettings()
|
||||||
|
permission_settings.from_dict(cfg)
|
||||||
|
|
||||||
|
SettingsHelper.configure_settings_with_sub_settings(
|
||||||
|
self._config, BotSettings, lambda x: x.servers, lambda x: x.id
|
||||||
|
)
|
||||||
|
SettingsHelper.configure_settings_with_sub_settings(
|
||||||
|
self._config, BaseSettings, lambda x: x.servers, lambda x: x.id
|
||||||
|
)
|
||||||
|
SettingsHelper.configure_settings_with_sub_settings(
|
||||||
|
self._config, BootLogSettings, lambda x: x.servers, lambda x: x.id
|
||||||
|
)
|
||||||
|
SettingsHelper.configure_settings_with_sub_settings(
|
||||||
|
self._config, LevelSettings, lambda x: x.servers, lambda x: x.id
|
||||||
|
)
|
||||||
|
SettingsHelper.configure_settings_with_sub_settings(
|
||||||
|
self._config, PermissionSettings, lambda x: x.servers, lambda x: x.id
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
self._logger.fatal(__name__, "Cannot load config from db", e)
|
26
kdb-bot/src/bot_data/data_extension/__init__.py
Normal file
26
kdb-bot/src/bot_data/data_extension/__init__.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
"""
|
||||||
|
bot Keksdose bot
|
||||||
|
~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Discord bot for the Keksdose discord Server
|
||||||
|
|
||||||
|
:copyright: (c) 2022 - 2023 sh-edraft.de
|
||||||
|
:license: MIT, see LICENSE for more details.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
__title__ = "bot_data.data_extension"
|
||||||
|
__author__ = "Sven Heidemann"
|
||||||
|
__license__ = "MIT"
|
||||||
|
__copyright__ = "Copyright (c) 2022 - 2023 sh-edraft.de"
|
||||||
|
__version__ = "0.3.dev127"
|
||||||
|
|
||||||
|
from collections import namedtuple
|
||||||
|
|
||||||
|
|
||||||
|
# imports:
|
||||||
|
|
||||||
|
VersionInfo = namedtuple("VersionInfo", "major minor micro")
|
||||||
|
version_info = VersionInfo(major="0", minor="3", micro="dev127")
|
@ -1,6 +1,7 @@
|
|||||||
from cpl_core.configuration import ConfigurationABC
|
from cpl_core.configuration import ConfigurationABC
|
||||||
from cpl_core.dependency_injection import ServiceCollectionABC
|
from cpl_core.dependency_injection import ServiceCollectionABC
|
||||||
from cpl_core.environment import ApplicationEnvironmentABC
|
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 cpl_discord.service.discord_collection_abc import DiscordCollectionABC
|
||||||
|
|
||||||
from bot_core.abc.module_abc import ModuleABC
|
from bot_core.abc.module_abc import ModuleABC
|
||||||
@ -9,6 +10,7 @@ from bot_data.abc.api_key_repository_abc import ApiKeyRepositoryABC
|
|||||||
from bot_data.abc.auth_user_repository_abc import AuthUserRepositoryABC
|
from bot_data.abc.auth_user_repository_abc import AuthUserRepositoryABC
|
||||||
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
|
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
|
||||||
from bot_data.abc.client_repository_abc import ClientRepositoryABC
|
from bot_data.abc.client_repository_abc import ClientRepositoryABC
|
||||||
|
from bot_data.abc.data_seeder_abc import DataSeederABC
|
||||||
from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC
|
from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC
|
||||||
from bot_data.abc.level_repository_abc import LevelRepositoryABC
|
from bot_data.abc.level_repository_abc import LevelRepositoryABC
|
||||||
from bot_data.abc.server_repository_abc import ServerRepositoryABC
|
from bot_data.abc.server_repository_abc import ServerRepositoryABC
|
||||||
@ -22,10 +24,13 @@ from bot_data.abc.user_message_count_per_hour_repository_abc import (
|
|||||||
UserMessageCountPerHourRepositoryABC,
|
UserMessageCountPerHourRepositoryABC,
|
||||||
)
|
)
|
||||||
from bot_data.abc.user_repository_abc import UserRepositoryABC
|
from bot_data.abc.user_repository_abc import UserRepositoryABC
|
||||||
|
from bot_data.config_seeder import ConfigSeeder
|
||||||
|
from bot_data.events.database_on_ready_event import DatabaseOnReadyEvent
|
||||||
from bot_data.service.api_key_repository_service import ApiKeyRepositoryService
|
from bot_data.service.api_key_repository_service import ApiKeyRepositoryService
|
||||||
from bot_data.service.auth_user_repository_service import AuthUserRepositoryService
|
from bot_data.service.auth_user_repository_service import AuthUserRepositoryService
|
||||||
from bot_data.service.auto_role_repository_service import AutoRoleRepositoryService
|
from bot_data.service.auto_role_repository_service import AutoRoleRepositoryService
|
||||||
from bot_data.service.client_repository_service import ClientRepositoryService
|
from bot_data.service.client_repository_service import ClientRepositoryService
|
||||||
|
from bot_data.service.db_config import DBConfigService
|
||||||
from bot_data.service.known_user_repository_service import KnownUserRepositoryService
|
from bot_data.service.known_user_repository_service import KnownUserRepositoryService
|
||||||
from bot_data.service.level_repository_service import LevelRepositoryService
|
from bot_data.service.level_repository_service import LevelRepositoryService
|
||||||
from bot_data.service.seeder_service import SeederService
|
from bot_data.service.seeder_service import SeederService
|
||||||
@ -69,4 +74,8 @@ class DataModule(ModuleABC):
|
|||||||
UserMessageCountPerHourRepositoryService,
|
UserMessageCountPerHourRepositoryService,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
services.add_transient(DBConfigService)
|
||||||
|
services.add_transient(DataSeederABC, ConfigSeeder)
|
||||||
services.add_transient(SeederService)
|
services.add_transient(SeederService)
|
||||||
|
|
||||||
|
self._dc.add_event(DiscordEventTypesEnum.on_ready.value, DatabaseOnReadyEvent)
|
||||||
|
@ -11,11 +11,11 @@ Discord bot for the Keksdose discord Server
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__title__ = "modules.database"
|
__title__ = "bot_data.events"
|
||||||
__author__ = "Sven Heidemann"
|
__author__ = "Sven Heidemann"
|
||||||
__license__ = "MIT"
|
__license__ = "MIT"
|
||||||
__copyright__ = "Copyright (c) 2022 - 2023 sh-edraft.de"
|
__copyright__ = "Copyright (c) 2022 - 2023 sh-edraft.de"
|
||||||
__version__ = "0.3.1"
|
__version__ = "0.3.dev127"
|
||||||
|
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
@ -23,4 +23,4 @@ from collections import namedtuple
|
|||||||
# imports:
|
# imports:
|
||||||
|
|
||||||
VersionInfo = namedtuple("VersionInfo", "major minor micro")
|
VersionInfo = namedtuple("VersionInfo", "major minor micro")
|
||||||
version_info = VersionInfo(major="0", minor="3", micro="1")
|
version_info = VersionInfo(major="0", minor="3", micro="dev127")
|
133
kdb-bot/src/bot_data/migration/config_migration.py
Normal file
133
kdb-bot/src/bot_data/migration/config_migration.py
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
from bot_core.logging.database_logger import DatabaseLogger
|
||||||
|
from bot_data.abc.migration_abc import MigrationABC
|
||||||
|
from bot_data.db_context import DBContext
|
||||||
|
|
||||||
|
|
||||||
|
class ConfigMigration(MigrationABC):
|
||||||
|
name = "1.0_ConfigMigration"
|
||||||
|
|
||||||
|
def __init__(self, logger: DatabaseLogger, db: DBContext):
|
||||||
|
MigrationABC.__init__(self)
|
||||||
|
self._logger = logger
|
||||||
|
self._db = db
|
||||||
|
self._cursor = db.cursor
|
||||||
|
|
||||||
|
def upgrade(self):
|
||||||
|
self._logger.debug(__name__, "Running upgrade")
|
||||||
|
|
||||||
|
self._cursor.execute(
|
||||||
|
f"""
|
||||||
|
CREATE TABLE IF NOT EXISTS `ST_Bot` (
|
||||||
|
`Id` BIGINT NOT NULL AUTO_INCREMENT,
|
||||||
|
`WaitForRestart` BIGINT NOT NULL DEFAULT 8,
|
||||||
|
`WaitForShutdown` BIGINT NOT NULL DEFAULT 8,
|
||||||
|
`CacheMaxMessages` BIGINT NOT NULL DEFAULT 1000000,
|
||||||
|
PRIMARY KEY(`Id`)
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
self._cursor.execute(
|
||||||
|
f"""
|
||||||
|
CREATE TABLE IF NOT EXISTS `ST_Technicians` (
|
||||||
|
`Id` BIGINT NOT NULL AUTO_INCREMENT,
|
||||||
|
`ST_BotId` BIGINT NOT NULL,
|
||||||
|
`DiscordId` BIGINT NOT NULL,
|
||||||
|
PRIMARY KEY(`Id`),
|
||||||
|
FOREIGN KEY (`ST_BotId`) REFERENCES `ST_Bot`(`Id`)
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
self._cursor.execute(
|
||||||
|
f"""
|
||||||
|
CREATE TABLE IF NOT EXISTS `ST_Servers` (
|
||||||
|
`Id` BIGINT NOT NULL AUTO_INCREMENT,
|
||||||
|
`ServerId` BIGINT NOT NULL,
|
||||||
|
`MessageDeleteTimer` BIGINT NOT NULL,
|
||||||
|
-- base
|
||||||
|
`MaxVoiceStateHours` BIGINT NOT NULL DEFAULT 6,
|
||||||
|
`XpPerMessage` BIGINT NOT NULL DEFAULT 1,
|
||||||
|
`XpPerReaction` BIGINT NOT NULL DEFAULT 1,
|
||||||
|
`MaxMessageXpPerHour` BIGINT NOT NULL DEFAULT 40,
|
||||||
|
`XpPerOntimeHour` BIGINT NOT NULL DEFAULT 20,
|
||||||
|
`XpPerEventParticipation` BIGINT NOT NULL DEFAULT 20,
|
||||||
|
`AFKCommandChannelId` BIGINT NOT NULL DEFAULT 1,
|
||||||
|
`HelpCommandReferenceUrl` VARCHAR(512) NOT NULL DEFAULT 'https://git.sh-edraft.de/sh-edraft.de/kd_discord_bot/wiki/Befehle',
|
||||||
|
`HelpVoiceChannelId` BIGINT NOT NULL DEFAULT 1,
|
||||||
|
-- boot log
|
||||||
|
`LoginMessageChannelId` BIGINT NOT NULL,
|
||||||
|
-- level
|
||||||
|
`ChangedLevelNotificationChannelId` BIGINT NOT NULL,
|
||||||
|
PRIMARY KEY(`Id`),
|
||||||
|
FOREIGN KEY (`ServerId`) REFERENCES `Servers`(`ServerId`)
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
self._cursor.execute(
|
||||||
|
f"""
|
||||||
|
CREATE TABLE IF NOT EXISTS `ST_Servers_AFKChannelIds` (
|
||||||
|
`Id` BIGINT NOT NULL AUTO_INCREMENT,
|
||||||
|
`ST_Server` BIGINT NOT NULL,
|
||||||
|
`ChannelId` BIGINT NOT NULL,
|
||||||
|
PRIMARY KEY(`Id`),
|
||||||
|
FOREIGN KEY (`ST_Server`) REFERENCES `ST_Servers`(`Id`)
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
self._cursor.execute(
|
||||||
|
f"""
|
||||||
|
CREATE TABLE IF NOT EXISTS `ST_Servers_PingURLs` (
|
||||||
|
`Id` BIGINT NOT NULL AUTO_INCREMENT,
|
||||||
|
`ST_Server` BIGINT NOT NULL,
|
||||||
|
`URL` VARCHAR(512) NOT NULL,
|
||||||
|
PRIMARY KEY(`Id`),
|
||||||
|
FOREIGN KEY (`ST_Server`) REFERENCES `ST_Servers`(`Id`)
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
self._cursor.execute(
|
||||||
|
f"""
|
||||||
|
CREATE TABLE IF NOT EXISTS `ST_Servers_Permissions` (
|
||||||
|
`Id` BIGINT NOT NULL AUTO_INCREMENT,
|
||||||
|
`ST_Server` BIGINT NOT NULL,
|
||||||
|
`RoleId` BIGINT NOT NULL,
|
||||||
|
`RoleType` BIGINT NOT NULL DEFAULT 1,
|
||||||
|
PRIMARY KEY(`Id`),
|
||||||
|
FOREIGN KEY (`ST_Server`) REFERENCES `ST_Servers`(`Id`)
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
self._cursor.execute(
|
||||||
|
f"""
|
||||||
|
CREATE TABLE IF NOT EXISTS `ST_RoleTypes` (
|
||||||
|
`Id` BIGINT NOT NULL AUTO_INCREMENT,
|
||||||
|
`Name` VARCHAR(10) NOT NULL,
|
||||||
|
PRIMARY KEY(`Id`)
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
self._cursor.execute(
|
||||||
|
f"""
|
||||||
|
INSERT INTO `ST_RoleTypes`
|
||||||
|
(`Id`, `Name`)
|
||||||
|
VALUES
|
||||||
|
(1, 'Moderator'),
|
||||||
|
(2, 'Admin');
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
def downgrade(self):
|
||||||
|
self._logger.debug(__name__, "Running downgrade")
|
||||||
|
self._cursor.execute("DROP TABLE `ST_Bot`;")
|
||||||
|
self._cursor.execute("DROP TABLE `ST_Technicians`;")
|
||||||
|
self._cursor.execute("DROP TABLE `ST_Servers`;")
|
||||||
|
self._cursor.execute("DROP TABLE `ST_Servers_AFKChannelIds`;")
|
||||||
|
self._cursor.execute("DROP TABLE `ST_Servers_PingURLs`;")
|
||||||
|
self._cursor.execute("DROP TABLE `ST_Servers_Permissions`;")
|
||||||
|
self._cursor.execute("DROP TABLE `ST_RoleTypes`;")
|
@ -252,10 +252,10 @@ class AuthUser(TableABC):
|
|||||||
`EMail` = '{self._email}',
|
`EMail` = '{self._email}',
|
||||||
`Password` = '{self._password}',
|
`Password` = '{self._password}',
|
||||||
`PasswordSalt` = '{self._password_salt}',
|
`PasswordSalt` = '{self._password_salt}',
|
||||||
`RefreshToken` = '{'null' if self._refresh_token is None else f'{self._refresh_token}'}',
|
{f"`RefreshToken` = ''," if self._refresh_token is not None else ''}
|
||||||
`ConfirmationId` = '{'null' if self._confirmation_id is None else f'{self._confirmation_id}'}',
|
{f"`ConfirmationId` = ''," if self._confirmation_id is not None else ''}
|
||||||
`ForgotPasswordId` = '{'null' if self._forgot_password_id is None else f'{self._forgot_password_id}'}',
|
{f"`ForgotPasswordId` = ''," if self._forgot_password_id is not None else ''}
|
||||||
`OAuthId` = '{'null' if self._oauth_id is None else f'{self._oauth_id}'}',
|
{f"`OAuthId` = ''," if self._oauth_id is not None else ''}
|
||||||
`RefreshTokenExpiryTime` = '{self._refresh_token_expire_time.isoformat()}',
|
`RefreshTokenExpiryTime` = '{self._refresh_token_expire_time.isoformat()}',
|
||||||
`AuthRole` = {self._auth_role_id.value},
|
`AuthRole` = {self._auth_role_id.value},
|
||||||
`LastModifiedAt` = '{self._modified_at}'
|
`LastModifiedAt` = '{self._modified_at}'
|
||||||
|
@ -168,7 +168,7 @@ class User(TableABC):
|
|||||||
f"""
|
f"""
|
||||||
UPDATE `Users`
|
UPDATE `Users`
|
||||||
SET `XP` = {self._xp},
|
SET `XP` = {self._xp},
|
||||||
`MinecraftId` = {'null' if self.minecraft_id is None else f'{self._minecraft_id}'},
|
{f"`MinecraftId` = '{self._minecraft_id}'," if self._minecraft_id is not None else ''}
|
||||||
`LastModifiedAt` = '{self._modified_at}'
|
`LastModifiedAt` = '{self._modified_at}'
|
||||||
WHERE `UserId` = {self._user_id};
|
WHERE `UserId` = {self._user_id};
|
||||||
"""
|
"""
|
||||||
|
140
kdb-bot/src/bot_data/service/db_config.py
Normal file
140
kdb-bot/src/bot_data/service/db_config.py
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from cpl_core.database.context import DatabaseContextABC
|
||||||
|
|
||||||
|
from bot_core.configuration.bot_settings import BotSettings
|
||||||
|
from bot_core.logging.database_logger import DatabaseLogger
|
||||||
|
|
||||||
|
|
||||||
|
class DBConfigService:
|
||||||
|
def __init__(self, logger: DatabaseLogger, db_context: DatabaseContextABC):
|
||||||
|
self._logger = logger
|
||||||
|
self._context = db_context
|
||||||
|
|
||||||
|
def get_st_bot(self) -> Optional[dict]:
|
||||||
|
result = self._context.select(
|
||||||
|
f"""
|
||||||
|
SELECT `Id`, `WaitForRestart`, `WaitForShutdown`, `CacheMaxMessages`
|
||||||
|
FROM ST_Bot
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(result) == 0:
|
||||||
|
return None
|
||||||
|
|
||||||
|
# get first element
|
||||||
|
result = result[0]
|
||||||
|
|
||||||
|
technicians = self._context.select(
|
||||||
|
f"""
|
||||||
|
SELECT `DiscordId`
|
||||||
|
FROM ST_Technicians
|
||||||
|
WHERE ST_BotId = {result[0]};
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
technician_ids = []
|
||||||
|
for technician in technicians:
|
||||||
|
technician_ids.append(technician[0])
|
||||||
|
|
||||||
|
result_as_dict = {
|
||||||
|
"WaitForRestart": result[1],
|
||||||
|
"WaitForShutdown": result[2],
|
||||||
|
"CacheMaxMessages": result[3],
|
||||||
|
"Technicians": technician_ids,
|
||||||
|
}
|
||||||
|
|
||||||
|
servers = self._context.select(
|
||||||
|
f"""
|
||||||
|
SELECT
|
||||||
|
Servers.`ServerId`,
|
||||||
|
`DiscordServerId`,
|
||||||
|
`MessageDeleteTimer`,
|
||||||
|
`MaxVoiceStateHours`,
|
||||||
|
`XpPerMessage`,
|
||||||
|
`XpPerReaction`,
|
||||||
|
`MaxMessageXpPerHour`,
|
||||||
|
`XpPerOntimeHour`,
|
||||||
|
`XpPerEventParticipation`,
|
||||||
|
`AFKCommandChannelId`,
|
||||||
|
`HelpCommandReferenceUrl`,
|
||||||
|
`HelpVoiceChannelId`,
|
||||||
|
`LoginMessageChannelId`,
|
||||||
|
`ChangedLevelNotificationChannelId`
|
||||||
|
FROM ST_Servers
|
||||||
|
INNER JOIN Servers ON
|
||||||
|
ST_Servers.ServerId = Servers.ServerId;
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
for server in servers:
|
||||||
|
afk_channels = self._context.select(
|
||||||
|
f"""
|
||||||
|
SELECT `ChannelId`
|
||||||
|
FROM ST_Servers_AFKChannelIds
|
||||||
|
WHERE ST_Server = {server[0]};
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
afk_channel_ids = []
|
||||||
|
for afk_channel in afk_channels:
|
||||||
|
afk_channel_ids.append(afk_channel[0])
|
||||||
|
|
||||||
|
ping_urls_result = self._context.select(
|
||||||
|
f"""
|
||||||
|
SELECT `URL`
|
||||||
|
FROM ST_Servers_PingURLs
|
||||||
|
WHERE ST_Server = {server[0]};
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
ping_urls = []
|
||||||
|
for url in ping_urls_result:
|
||||||
|
ping_urls.append(url[0])
|
||||||
|
|
||||||
|
admin_roles = self._context.select(
|
||||||
|
f"""
|
||||||
|
SELECT `RoleId`
|
||||||
|
FROM ST_Servers_Permissions
|
||||||
|
WHERE ST_Server = {server[0]}
|
||||||
|
AND RoleType = 2;
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
admin_role_ids = []
|
||||||
|
for role in admin_roles:
|
||||||
|
admin_role_ids.append(role[0])
|
||||||
|
|
||||||
|
moderator_roles = self._context.select(
|
||||||
|
f"""
|
||||||
|
SELECT `RoleId`
|
||||||
|
FROM ST_Servers_Permissions
|
||||||
|
WHERE ST_Server = {server[0]}
|
||||||
|
AND RoleType = 1;
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
moderator_role_ids = []
|
||||||
|
for role in moderator_roles:
|
||||||
|
moderator_role_ids.append(role[0])
|
||||||
|
|
||||||
|
result_as_dict[server[1]] = {
|
||||||
|
"MessageDeleteTimer": server[2],
|
||||||
|
"MaxVoiceStateHours": server[3],
|
||||||
|
"XpPerMessage": server[4],
|
||||||
|
"XpPerReaction": server[5],
|
||||||
|
"MaxMessageXpPerHour": server[6],
|
||||||
|
"XpPerOntimeHour": server[7],
|
||||||
|
"XpPerEventParticipation": server[8],
|
||||||
|
"AFKCommandChannelId": server[9],
|
||||||
|
"HelpCommandReferenceUrl": server[10],
|
||||||
|
"HelpVoiceChannelId": server[11],
|
||||||
|
"LoginMessageChannelId": server[12],
|
||||||
|
"ChangedLevelNotificationChannelId": server[13],
|
||||||
|
"AFKChannelIds": afk_channel_ids,
|
||||||
|
"PingURLs": ping_urls,
|
||||||
|
"AdminRoleIds": admin_role_ids,
|
||||||
|
"ModeratorRoleIds": moderator_role_ids,
|
||||||
|
}
|
||||||
|
|
||||||
|
return result_as_dict
|
||||||
|
|
||||||
|
def get_bot_settings(self) -> BotSettings:
|
||||||
|
settings = BotSettings()
|
||||||
|
settings.from_dict({})
|
||||||
|
return settings
|
@ -3,8 +3,8 @@
|
|||||||
"Name": "bot-data",
|
"Name": "bot-data",
|
||||||
"Version": {
|
"Version": {
|
||||||
"Major": "0",
|
"Major": "0",
|
||||||
"Minor": "1",
|
"Minor": "3",
|
||||||
"Micro": "0"
|
"Micro": "dev127"
|
||||||
},
|
},
|
||||||
"Author": "Sven Heidemann",
|
"Author": "Sven Heidemann",
|
||||||
"AuthorEmail": "sven.heidemann@sh-edraft.de",
|
"AuthorEmail": "sven.heidemann@sh-edraft.de",
|
||||||
@ -16,10 +16,10 @@
|
|||||||
"LicenseName": "MIT",
|
"LicenseName": "MIT",
|
||||||
"LicenseDescription": "MIT, see LICENSE for more details.",
|
"LicenseDescription": "MIT, see LICENSE for more details.",
|
||||||
"Dependencies": [
|
"Dependencies": [
|
||||||
"cpl-core>=2022.12.1"
|
"cpl-core>=0.3.dev127"
|
||||||
],
|
],
|
||||||
"DevDependencies": [
|
"DevDependencies": [
|
||||||
"cpl-cli>=2022.12.1"
|
"cpl-cli>=0.3.dev127"
|
||||||
],
|
],
|
||||||
"PythonVersion": ">=3.10.4",
|
"PythonVersion": ">=3.10.4",
|
||||||
"PythonPath": {},
|
"PythonPath": {},
|
||||||
|
@ -1,44 +0,0 @@
|
|||||||
{
|
|
||||||
"ProjectSettings": {
|
|
||||||
"Name": "database",
|
|
||||||
"Version": {
|
|
||||||
"Major": "0",
|
|
||||||
"Minor": "3",
|
|
||||||
"Micro": "1"
|
|
||||||
},
|
|
||||||
"Author": "Sven Heidemann",
|
|
||||||
"AuthorEmail": "sven.heidemann@sh-edraft.de",
|
|
||||||
"Description": "Keksdose bot - db module",
|
|
||||||
"LongDescription": "Discord bot for the Keksdose discord Server - database module",
|
|
||||||
"URL": "https://www.sh-edraft.de",
|
|
||||||
"CopyrightDate": "2022",
|
|
||||||
"CopyrightName": "sh-edraft.de",
|
|
||||||
"LicenseName": "MIT",
|
|
||||||
"LicenseDescription": "MIT, see LICENSE for more details.",
|
|
||||||
"Dependencies": [
|
|
||||||
"cpl-core==2022.12.0"
|
|
||||||
],
|
|
||||||
"DevDependencies": [
|
|
||||||
"cpl-cli==2022.12.0"
|
|
||||||
],
|
|
||||||
"PythonVersion": ">=3.10.4",
|
|
||||||
"PythonPath": {},
|
|
||||||
"Classifiers": []
|
|
||||||
},
|
|
||||||
"BuildSettings": {
|
|
||||||
"ProjectType": "library",
|
|
||||||
"SourcePath": "",
|
|
||||||
"OutputPath": "../../dist",
|
|
||||||
"Main": "database.main",
|
|
||||||
"EntryPoint": "database",
|
|
||||||
"IncludePackageData": false,
|
|
||||||
"Included": [],
|
|
||||||
"Excluded": [
|
|
||||||
"*/__pycache__",
|
|
||||||
"*/logs",
|
|
||||||
"*/tests"
|
|
||||||
],
|
|
||||||
"PackageData": {},
|
|
||||||
"ProjectReferences": []
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
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.database.database_on_ready_event import DatabaseOnReadyEvent
|
|
||||||
|
|
||||||
|
|
||||||
class DatabaseModule(ModuleABC):
|
|
||||||
def __init__(self, dc: DiscordCollectionABC):
|
|
||||||
ModuleABC.__init__(self, dc, FeatureFlagsEnum.database_module)
|
|
||||||
|
|
||||||
def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC):
|
|
||||||
# commands
|
|
||||||
# events
|
|
||||||
self._dc.add_event(DiscordEventTypesEnum.on_ready.value, DatabaseOnReadyEvent)
|
|
Loading…
Reference in New Issue
Block a user