From 5d36f1188a43ae3252f19a17808da197a8627bf1 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 14 Aug 2023 15:53:42 +0200 Subject: [PATCH] Added server config mutation #127 --- .../abc/server_config_repository_abc.py | 6 +- .../model/server_afk_channel_ids_config.py | 15 +- kdb-bot/src/bot_data/model/server_config.py | 73 ++++++++ .../bot_data/model/server_config_history.py | 102 ++++++++++++ .../model/server_team_role_ids_config.py | 15 +- .../server_config_repository_service.py | 27 +-- kdb-bot/src/bot_graphql/graphql_module.py | 4 + kdb-bot/src/bot_graphql/model/mutation.gql | 1 + kdb-bot/src/bot_graphql/model/server.gql | 2 + .../src/bot_graphql/model/serverConfig.gql | 94 +++++++++++ kdb-bot/src/bot_graphql/mutation.py | 3 + .../mutations/server_config_mutation.py | 157 ++++++++++++++++++ .../queries/server_config_query.py | 39 +++++ .../src/bot_graphql/queries/server_query.py | 3 + .../queries/technician_config_query.py | 20 +-- .../config/events/config_on_ready_event.py | 2 +- 16 files changed, 537 insertions(+), 26 deletions(-) create mode 100644 kdb-bot/src/bot_data/model/server_config_history.py create mode 100644 kdb-bot/src/bot_graphql/model/serverConfig.gql create mode 100644 kdb-bot/src/bot_graphql/mutations/server_config_mutation.py create mode 100644 kdb-bot/src/bot_graphql/queries/server_config_query.py diff --git a/kdb-bot/src/bot_data/abc/server_config_repository_abc.py b/kdb-bot/src/bot_data/abc/server_config_repository_abc.py index e6fd480c..f66fdc89 100644 --- a/kdb-bot/src/bot_data/abc/server_config_repository_abc.py +++ b/kdb-bot/src/bot_data/abc/server_config_repository_abc.py @@ -15,7 +15,11 @@ class ServerConfigRepositoryABC(ABC): pass @abstractmethod - def get_server_config(self, server_id: int) -> ServerConfig: + def get_server_config_by_server(self, server_id: int) -> ServerConfig: + pass + + @abstractmethod + def get_server_config_by_id(self, config_id: int) -> ServerConfig: pass @abstractmethod diff --git a/kdb-bot/src/bot_data/model/server_afk_channel_ids_config.py b/kdb-bot/src/bot_data/model/server_afk_channel_ids_config.py index f567ccee..9b182426 100644 --- a/kdb-bot/src/bot_data/model/server_afk_channel_ids_config.py +++ b/kdb-bot/src/bot_data/model/server_afk_channel_ids_config.py @@ -41,12 +41,21 @@ class ServerAFKChannelIdsConfig(TableABC): """ ) + @staticmethod + def get_select_by_server_id_string(server_id: int) -> str: + return str( + f""" + SELECT * FROM `CFG_ServerAFKChannelIds` + WHERE `ServerId` = {server_id}; + """ + ) + @property def insert_string(self) -> str: return str( f""" INSERT INTO `CFG_ServerAFKChannelIds` ( - `RoleId`, + `ChannelId`, `ServerId` ) VALUES ( {self._channel_id}, @@ -60,7 +69,7 @@ class ServerAFKChannelIdsConfig(TableABC): return str( f""" UPDATE `CFG_ServerAFKChannelIds` - SET `RoleId` = {self._channel_id}, + SET `ChannelId` = {self._channel_id}, `ServerId` = {self._server_id} WHERE `Id` = {self._id}; """ @@ -71,6 +80,6 @@ class ServerAFKChannelIdsConfig(TableABC): return str( f""" DELETE FROM `CFG_ServerAFKChannelIds` - WHERE `Id` = {self._id}; + WHERE `ChannelId` = {self._channel_id}; """ ) diff --git a/kdb-bot/src/bot_data/model/server_config.py b/kdb-bot/src/bot_data/model/server_config.py index b962e221..ba1db9f6 100644 --- a/kdb-bot/src/bot_data/model/server_config.py +++ b/kdb-bot/src/bot_data/model/server_config.py @@ -53,66 +53,130 @@ class ServerConfig(TableABC, ConfigurationModelABC): self._created_at = created_at if created_at is not None else self._created_at self._modified_at = modified_at if modified_at is not None else self._modified_at + @property + def id(self) -> int: + return self._id + @property def message_delete_timer(self) -> int: return self._message_delete_timer + @message_delete_timer.setter + def message_delete_timer(self, value: int): + self._message_delete_timer = value + @property def notification_chat_id(self) -> int: return self._notification_chat_id + @notification_chat_id.setter + def notification_chat_id(self, value: int): + self._notification_chat_id = value + @property def max_voice_state_hours(self) -> int: return self._max_voice_state_hours + @max_voice_state_hours.setter + def max_voice_state_hours(self, value: int): + self._max_voice_state_hours = value + @property def xp_per_message(self) -> int: return self._xp_per_message + @xp_per_message.setter + def xp_per_message(self, value: int): + self._xp_per_message = value + @property def xp_per_reaction(self) -> int: return self._xp_per_reaction + @xp_per_reaction.setter + def xp_per_reaction(self, value: int): + self._xp_per_reaction = value + @property def max_message_xp_per_hour(self) -> int: return self._max_message_xp_per_hour + @max_message_xp_per_hour.setter + def max_message_xp_per_hour(self, value: int): + self._max_message_xp_per_hour = value + @property def xp_per_ontime_hour(self) -> int: return self._xp_per_ontime_hour + @xp_per_ontime_hour.setter + def xp_per_ontime_hour(self, value: int): + self._xp_per_ontime_hour = value + @property def xp_per_event_participation(self) -> int: return self._xp_per_event_participation + @xp_per_event_participation.setter + def xp_per_event_participation(self, value: int): + self._xp_per_event_participation = value + @property def xp_per_achievement(self) -> int: return self._xp_per_achievement + @xp_per_achievement.setter + def xp_per_achievement(self, value: int): + self._xp_per_achievement = value + @property def afk_command_channel_id(self) -> int: return self._afk_command_channel_id + @afk_command_channel_id.setter + def afk_command_channel_id(self, value: int): + self._afk_command_channel_id = value + @property def help_voice_channel_id(self) -> int: return self._help_voice_channel_id + @help_voice_channel_id.setter + def help_voice_channel_id(self, value: int): + self._help_voice_channel_id = value + @property def team_channel_id(self) -> int: return self._team_channel_id + @team_channel_id.setter + def team_channel_id(self, value: int): + self._team_channel_id = value + @property def login_message_channel_id(self) -> int: return self._login_message_channel_id + @login_message_channel_id.setter + def login_message_channel_id(self, value: int): + self._login_message_channel_id = value + @property def afk_channel_ids(self) -> List[int]: return self._afk_channel_ids + @afk_channel_ids.setter + def afk_channel_ids(self, value: List[int]): + self._afk_channel_ids = value + @property def team_role_ids(self) -> List[ServerTeamRoleIdsConfig]: return self._team_role_ids + @team_role_ids.setter + def team_role_ids(self, value: List[ServerTeamRoleIdsConfig]): + self._team_role_ids = value + @property def server(self) -> Server: return self._server @@ -134,6 +198,15 @@ class ServerConfig(TableABC, ConfigurationModelABC): """ ) + @staticmethod + def get_select_by_server_id_string(server_id: int) -> str: + return str( + f""" + SELECT * FROM `CFG_Server` + WHERE `ServerId` = {server_id}; + """ + ) + @property def insert_string(self) -> str: return str( diff --git a/kdb-bot/src/bot_data/model/server_config_history.py b/kdb-bot/src/bot_data/model/server_config_history.py new file mode 100644 index 00000000..d96e8473 --- /dev/null +++ b/kdb-bot/src/bot_data/model/server_config_history.py @@ -0,0 +1,102 @@ +from bot_data.abc.history_table_abc import HistoryTableABC + + +class ServerConfigHistory(HistoryTableABC): + def __init__( + self, + message_delete_timer: int, + notification_chat_id: int, + max_voice_state_hours: int, + xp_per_message: int, + xp_per_reaction: int, + max_message_xp_per_hour: int, + xp_per_ontime_hour: int, + xp_per_event_participation: int, + xp_per_achievement: int, + afk_command_channel_id: int, + help_voice_channel_id: int, + team_channel_id: int, + login_message_channel_id: int, + server_id: int, + deleted: bool, + date_from: str, + date_to: str, + id=0, + ): + HistoryTableABC.__init__(self) + + self._id = id + self._message_delete_timer = message_delete_timer + self._notification_chat_id = notification_chat_id + self._max_voice_state_hours = max_voice_state_hours + self._xp_per_message = xp_per_message + self._xp_per_reaction = xp_per_reaction + self._max_message_xp_per_hour = max_message_xp_per_hour + self._xp_per_ontime_hour = xp_per_ontime_hour + self._xp_per_event_participation = xp_per_event_participation + self._xp_per_achievement = xp_per_achievement + self._afk_command_channel_id = afk_command_channel_id + self._help_voice_channel_id = help_voice_channel_id + self._team_channel_id = team_channel_id + self._login_message_channel_id = login_message_channel_id + self._server_id = server_id + + self._deleted = deleted + self._date_from = date_from + self._date_to = date_to + + @property + def message_delete_timer(self) -> int: + return self._message_delete_timer + + @property + def notification_chat_id(self) -> int: + return self._notification_chat_id + + @property + def max_voice_state_hours(self) -> int: + return self._max_voice_state_hours + + @property + def xp_per_message(self) -> int: + return self._xp_per_message + + @property + def xp_per_reaction(self) -> int: + return self._xp_per_reaction + + @property + def max_message_xp_per_hour(self) -> int: + return self._max_message_xp_per_hour + + @property + def xp_per_ontime_hour(self) -> int: + return self._xp_per_ontime_hour + + @property + def xp_per_event_participation(self) -> int: + return self._xp_per_event_participation + + @property + def xp_per_achievement(self) -> int: + return self._xp_per_achievement + + @property + def afk_command_channel_id(self) -> int: + return self._afk_command_channel_id + + @property + def help_voice_channel_id(self) -> int: + return self._help_voice_channel_id + + @property + def team_channel_id(self) -> int: + return self._team_channel_id + + @property + def login_message_channel_id(self) -> int: + return self._login_message_channel_id + + @property + def server_id(self) -> int: + return self._server_id diff --git a/kdb-bot/src/bot_data/model/server_team_role_ids_config.py b/kdb-bot/src/bot_data/model/server_team_role_ids_config.py index aee799fd..dfd870dd 100644 --- a/kdb-bot/src/bot_data/model/server_team_role_ids_config.py +++ b/kdb-bot/src/bot_data/model/server_team_role_ids_config.py @@ -24,6 +24,10 @@ class ServerTeamRoleIdsConfig(TableABC): self._created_at = created_at if created_at is not None else self._created_at self._modified_at = modified_at if modified_at is not None else self._modified_at + @property + def id(self) -> int: + return self._id + @property def role_id(self) -> int: return self._role_id @@ -49,6 +53,15 @@ class ServerTeamRoleIdsConfig(TableABC): """ ) + @staticmethod + def get_select_by_server_id_string(server_id: int) -> str: + return str( + f""" + SELECT * FROM `CFG_ServerTeamRoleIds` + WHERE `ServerId` = {server_id}; + """ + ) + @property def insert_string(self) -> str: return str( @@ -82,6 +95,6 @@ class ServerTeamRoleIdsConfig(TableABC): return str( f""" DELETE FROM `CFG_ServerTeamRoleIds` - WHERE `Id` = {self._id}; + WHERE `RoleId` = {self._role_id}; """ ) diff --git a/kdb-bot/src/bot_data/service/server_config_repository_service.py b/kdb-bot/src/bot_data/service/server_config_repository_service.py index 3b069e71..09c1bff5 100644 --- a/kdb-bot/src/bot_data/service/server_config_repository_service.py +++ b/kdb-bot/src/bot_data/service/server_config_repository_service.py @@ -18,12 +18,12 @@ class ServerConfigRepositoryService(ServerConfigRepositoryABC): self._context = db_context self._servers = servers - def _get_team_role_ids(self, server_team_role_id: int) -> List[ServerTeamRoleIdsConfig]: + def _get_team_role_ids(self, server_id: int) -> List[ServerTeamRoleIdsConfig]: ids = List(ServerTeamRoleIdsConfig) self._logger.trace( - __name__, f"Send SQL command: {ServerTeamRoleIdsConfig.get_select_by_id_string(server_team_role_id)}" + __name__, f"Send SQL command: {ServerTeamRoleIdsConfig.get_select_by_server_id_string(server_id)}" ) - results = self._context.select(ServerTeamRoleIdsConfig.get_select_by_id_string(server_team_role_id)) + results = self._context.select(ServerTeamRoleIdsConfig.get_select_by_server_id_string(server_id)) for result in results: self._logger.trace(__name__, f"Got ServerTeamRoleIdsConfig with id {result[0]}") ids.append( @@ -34,12 +34,13 @@ class ServerConfigRepositoryService(ServerConfigRepositoryABC): return ids - def _get_afk_channel_ids(self, server_team_role_id: int) -> List[int]: - urls = List(str) + def _get_afk_channel_ids(self, server_id: int) -> List[int]: + urls = List(int) self._logger.trace( - __name__, f"Send SQL command: {ServerAFKChannelIdsConfig.get_select_by_id_string(server_team_role_id)}" + __name__, + f"Send SQL command: {ServerAFKChannelIdsConfig.get_select_by_server_id_string(server_id)}", ) - results = self._context.select(ServerAFKChannelIdsConfig.get_select_by_id_string(server_team_role_id)) + results = self._context.select(ServerAFKChannelIdsConfig.get_select_by_server_id_string(server_id)) for result in results: self._logger.trace(__name__, f"Got ServerAFKChannelIdsConfig with id {result[0]}") urls.append(result[1]) @@ -62,8 +63,8 @@ class ServerConfigRepositoryService(ServerConfigRepositoryABC): result[12], result[13], self._servers.get_server_by_id(result[14]), - self._get_afk_channel_ids(result[0]), - self._get_team_role_ids(result[0]), + self._get_afk_channel_ids(result[14]), + self._get_team_role_ids(result[14]), result[15], result[16], id=result[0], @@ -75,12 +76,18 @@ class ServerConfigRepositoryService(ServerConfigRepositoryABC): return len(result) > 0 - def get_server_config(self, server_id: int) -> ServerConfig: + def get_server_config_by_server(self, server_id: int) -> ServerConfig: self._logger.trace(__name__, f"Send SQL command: {ServerConfig.get_select_by_id_string(server_id)}") result = self._context.select(ServerConfig.get_select_by_id_string(server_id))[0] return self._from_result(result) + def get_server_config_by_id(self, config_id: int) -> ServerConfig: + self._logger.trace(__name__, f"Send SQL command: {ServerConfig.get_select_by_id_string(config_id)}") + result = self._context.select(ServerConfig.get_select_by_id_string(config_id))[0] + + return self._from_result(result) + def add_server_config(self, server_config: ServerConfig): self._logger.trace(__name__, f"Send SQL command: {server_config.insert_string}") self._context.cursor.execute(server_config.insert_string) diff --git a/kdb-bot/src/bot_graphql/graphql_module.py b/kdb-bot/src/bot_graphql/graphql_module.py index 96c0720f..2b294950 100644 --- a/kdb-bot/src/bot_graphql/graphql_module.py +++ b/kdb-bot/src/bot_graphql/graphql_module.py @@ -24,6 +24,7 @@ from bot_graphql.mutations.achievement_mutation import AchievementMutation from bot_graphql.mutations.auto_role_mutation import AutoRoleMutation from bot_graphql.mutations.auto_role_rule_mutation import AutoRoleRuleMutation from bot_graphql.mutations.level_mutation import LevelMutation +from bot_graphql.mutations.server_config_mutation import ServerConfigMutation from bot_graphql.mutations.technician_config_mutation import TechnicianConfigMutation from bot_graphql.mutations.user_joined_game_server_mutation import UserJoinedGameServerMutation from bot_graphql.mutations.user_mutation import UserMutation @@ -41,6 +42,7 @@ from bot_graphql.queries.known_user_history_query import KnownUserHistoryQuery from bot_graphql.queries.known_user_query import KnownUserQuery from bot_graphql.queries.level_history_query import LevelHistoryQuery from bot_graphql.queries.level_query import LevelQuery +from bot_graphql.queries.server_config_query import ServerConfigQuery from bot_graphql.queries.server_history_query import ServerHistoryQuery from bot_graphql.queries.server_query import ServerQuery from bot_graphql.queries.technician_config_history_query import TechnicianConfigHistoryQuery @@ -86,6 +88,7 @@ class GraphQLModule(ModuleABC): services.add_transient(QueryABC, KnownUserQuery) services.add_transient(QueryABC, LevelHistoryQuery) services.add_transient(QueryABC, LevelQuery) + services.add_transient(QueryABC, ServerConfigQuery) services.add_transient(QueryABC, ServerHistoryQuery) services.add_transient(QueryABC, ServerQuery) services.add_transient(QueryABC, TechnicianConfigQuery) @@ -122,5 +125,6 @@ class GraphQLModule(ModuleABC): services.add_transient(QueryABC, AchievementMutation) services.add_transient(QueryABC, UserJoinedGameServerMutation) services.add_transient(QueryABC, TechnicianConfigMutation) + services.add_transient(QueryABC, ServerConfigMutation) services.add_transient(SeederService) diff --git a/kdb-bot/src/bot_graphql/model/mutation.gql b/kdb-bot/src/bot_graphql/model/mutation.gql index 5f57cb8f..aa1d8cd6 100644 --- a/kdb-bot/src/bot_graphql/model/mutation.gql +++ b/kdb-bot/src/bot_graphql/model/mutation.gql @@ -6,4 +6,5 @@ type Mutation { userJoinedGameServer: UserJoinedGameServerMutation achievement: AchievementMutation technicianConfig: TechnicianConfigMutation + serverConfig: ServerConfigMutation } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/server.gql b/kdb-bot/src/bot_graphql/model/server.gql index c4689ac9..be754bdb 100644 --- a/kdb-bot/src/bot_graphql/model/server.gql +++ b/kdb-bot/src/bot_graphql/model/server.gql @@ -31,6 +31,8 @@ type Server implements TableWithHistoryQuery { achievementCount: Int achievements(filter: AchievementFilter, page: Page, sort: Sort): [Achievement] + config: ServerConfig + createdAt: String modifiedAt: String diff --git a/kdb-bot/src/bot_graphql/model/serverConfig.gql b/kdb-bot/src/bot_graphql/model/serverConfig.gql new file mode 100644 index 00000000..b16fa139 --- /dev/null +++ b/kdb-bot/src/bot_graphql/model/serverConfig.gql @@ -0,0 +1,94 @@ +type ServerConfig implements TableWithHistoryQuery { + id: ID + messageDeleteTimer: Int + notificationChatId: String + maxVoiceStateHours: Int + xpPerMessage: Int + xpPerReaction: Int + maxMessageXpPerHour: Int + xpPerOntimeHour: Int + xpPerEventParticipation: Int + xpPerAchievement: Int + afkCommandChannelId: String + helpVoiceChannelId: String + teamChannelId: String + loginMessageChannelId: String + + afkChannelIds: [String] + moderatorRoleIds: [String] + adminRoleIds: [String] + + server: Server + + createdAt: String + modifiedAt: String + + history: [ServerConfigHistory] +} + +type ServerConfigHistory implements HistoryTableQuery { + id: ID + messageDeleteTimer: Int + notificationChatId: String + maxVoiceStateHours: Int + xpPerMessage: Int + xpPerReaction: Int + maxMessageXpPerHour: Int + xpPerOntimeHour: Int + xpPerEventParticipation: Int + xpPerAchievement: Int + afkCommandChannelId: String + helpVoiceChannelId: String + teamChannelId: String + loginMessageChannelId: String + + serverId: ID + + deleted: Boolean + dateFrom: String + dateTo: String +} + +type ServerAFKChannelIdsConfigHistory implements HistoryTableQuery { + id: ID + channelId: String + + deleted: Boolean + dateFrom: String + dateTo: String +} + +type ServerTeamRoleIdsConfigHistory implements HistoryTableQuery { + id: ID + roleId: String + teamMemberType: String + + deleted: Boolean + dateFrom: String + dateTo: String +} + +type ServerConfigMutation { + updateServerConfig(input: ServerConfigInput!): ServerConfig +} + +input ServerConfigInput { + id: ID + messageDeleteTimer: Int + notificationChatId: String + maxVoiceStateHours: Int + xpPerMessage: Int + xpPerReaction: Int + maxMessageXpPerHour: Int + xpPerOntimeHour: Int + xpPerEventParticipation: Int + xpPerAchievement: Int + afkCommandChannelId: String + helpVoiceChannelId: String + teamChannelId: String + loginMessageChannelId: String + + afkChannelIds: [String] + moderatorRoleIds: [String] + adminRoleIds: [String] +} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/mutation.py b/kdb-bot/src/bot_graphql/mutation.py index d5a62123..4631e321 100644 --- a/kdb-bot/src/bot_graphql/mutation.py +++ b/kdb-bot/src/bot_graphql/mutation.py @@ -4,6 +4,7 @@ from bot_graphql.mutations.achievement_mutation import AchievementMutation from bot_graphql.mutations.auto_role_mutation import AutoRoleMutation from bot_graphql.mutations.auto_role_rule_mutation import AutoRoleRuleMutation from bot_graphql.mutations.level_mutation import LevelMutation +from bot_graphql.mutations.server_config_mutation import ServerConfigMutation from bot_graphql.mutations.technician_config_mutation import TechnicianConfigMutation from bot_graphql.mutations.user_joined_game_server_mutation import UserJoinedGameServerMutation from bot_graphql.mutations.user_mutation import UserMutation @@ -19,6 +20,7 @@ class Mutation(MutationType): achievement_mutation: AchievementMutation, user_joined_game_server: UserJoinedGameServerMutation, technician_config: TechnicianConfigMutation, + server_config: ServerConfigMutation, ): MutationType.__init__(self) @@ -29,3 +31,4 @@ class Mutation(MutationType): self.set_field("achievement", lambda *_: achievement_mutation) self.set_field("userJoinedGameServer", lambda *_: user_joined_game_server) self.set_field("technicianConfig", lambda *_: technician_config) + self.set_field("serverConfig", lambda *_: server_config) diff --git a/kdb-bot/src/bot_graphql/mutations/server_config_mutation.py b/kdb-bot/src/bot_graphql/mutations/server_config_mutation.py new file mode 100644 index 00000000..22669a7c --- /dev/null +++ b/kdb-bot/src/bot_graphql/mutations/server_config_mutation.py @@ -0,0 +1,157 @@ +from cpl_core.database.context import DatabaseContextABC +from cpl_discord.service import DiscordBotServiceABC +from cpl_query.extension import List + +from bot_api.logging.api_logger import ApiLogger +from bot_data.abc.server_config_repository_abc import ServerConfigRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.model.server_afk_channel_ids_config import ServerAFKChannelIdsConfig +from bot_data.model.server_config import ServerConfig +from bot_data.model.server_team_role_ids_config import ServerTeamRoleIdsConfig +from bot_data.model.team_member_type_enum import TeamMemberTypeEnum +from bot_data.model.user_role_enum import UserRoleEnum +from bot_graphql.abc.query_abc import QueryABC + + +class ServerConfigMutation(QueryABC): + def __init__( + self, + logger: ApiLogger, + bot: DiscordBotServiceABC, + servers: ServerRepositoryABC, + server_configs: ServerConfigRepositoryABC, + db: DatabaseContextABC, + ): + QueryABC.__init__(self, "ServerConfigMutation") + + self._logger = logger + self._bot = bot + self._servers = servers + self._server_configs = server_configs + self._db = db + + self.set_field("updateServerConfig", self.resolve_update_server_config) + + def resolve_update_server_config(self, *_, input: dict): + if "id" not in input: + raise ValueError("Id not set") + + server_config = self._server_configs.get_server_config_by_server(input["id"]) + self._can_user_mutate_data(server_config, UserRoleEnum.admin) + + server_config.message_delete_timer = ( + input["messageDeleteTimer"] if "messageDeleteTimer" in input else server_config.message_delete_timer + ) + server_config.notification_chat_id = ( + input["notificationChatId"] if "notificationChatId" in input else server_config.notification_chat_id + ) + server_config.max_voice_state_hours = ( + input["maxVoiceStateHours"] if "maxVoiceStateHours" in input else server_config.max_voice_state_hours + ) + server_config.xp_per_message = ( + input["xpPerMessage"] if "xpPerMessage" in input else server_config.xp_per_message + ) + server_config.xp_per_reaction = ( + input["xpPerReaction"] if "xpPerReaction" in input else server_config.xp_per_reaction + ) + server_config.max_message_xp_per_hour = ( + input["maxMessageXpPerHour"] if "maxMessageXpPerHour" in input else server_config.max_message_xp_per_hour + ) + server_config.xp_per_ontime_hour = ( + input["xpPerOntimeHour"] if "xpPerOntimeHour" in input else server_config.xp_per_ontime_hour + ) + server_config.xp_per_event_participation = ( + input["xpPerEventParticipation"] + if "xpPerEventParticipation" in input + else server_config.xp_per_event_participation + ) + server_config.xp_per_achievement = ( + input["xpPerAchievement"] if "xpPerAchievement" in input else server_config.xp_per_achievement + ) + server_config.afk_command_channel_id = ( + input["afkCommandChannelId"] if "afkCommandChannelId" in input else server_config.afk_command_channel_id + ) + server_config.help_voice_channel_id = ( + input["helpVoiceChannelId"] if "helpVoiceChannelId" in input else server_config.help_voice_channel_id + ) + server_config.team_channel_id = ( + input["teamChannelId"] if "teamChannelId" in input else server_config.team_channel_id + ) + server_config.login_message_channel_id = ( + input["loginMessageChannelId"] + if "loginMessageChannelId" in input + else server_config.login_message_channel_id + ) + server_config.afk_channel_ids = ( + List(int).extend([int(x) for x in input["afkChannelIds"]]) + if "afkChannelIds" in input + else server_config.afk_channel_ids + ) + + team_role_ids = List(ServerTeamRoleIdsConfig) + if "moderatorRoleIds" in input: + team_role_ids.extend( + [ + ServerTeamRoleIdsConfig(x, TeamMemberTypeEnum.moderator, server_config.server.id) + for x in input["moderatorRoleIds"] + ] + ) + else: + team_role_ids.extend( + server_config.team_role_ids.where(lambda x: x.team_member_type == TeamMemberTypeEnum.moderator) + ) + + if "adminRoleIds" in input: + team_role_ids.extend( + [ + ServerTeamRoleIdsConfig(x, TeamMemberTypeEnum.admin, server_config.server.id) + for x in input["adminRoleIds"] + ] + ) + else: + team_role_ids.extend( + server_config.team_role_ids.where(lambda x: x.team_member_type == TeamMemberTypeEnum.admin) + ) + + self._server_configs.update_server_config(server_config) + if "afkChannelIds" in input: + self._update_afk_channel_ids(server_config) + + if "moderatorRoleIds" in input or "adminRoleIds" in input: + server_config.team_role_ids = team_role_ids + self._update_team_role_ids(server_config) + + self._db.save_changes() + return server_config + + def _update_afk_channel_ids(self, new_config: ServerConfig): + old_config = self._server_configs.get_server_config_by_id(new_config.server.id) + for channel_id in old_config.afk_channel_ids: + if channel_id in new_config.afk_channel_ids: + continue + + self._server_configs.delete_server_afk_channel_config( + ServerAFKChannelIdsConfig(channel_id, new_config.server.id) + ) + + for channel_id in new_config.afk_channel_ids: + if channel_id in old_config.afk_channel_ids: + continue + + self._server_configs.add_server_afk_channel_config( + ServerAFKChannelIdsConfig(channel_id, new_config.server.id) + ) + + def _update_team_role_ids(self, new_config: ServerConfig): + old_config = self._server_configs.get_server_config_by_id(new_config.server.id) + for role_id in old_config.team_role_ids: + if role_id.role_id in new_config.team_role_ids.select(lambda x: int(x.role_id)): + continue + + self._server_configs.delete_server_team_role_id_config(role_id) + + for role_id in new_config.team_role_ids: + if role_id.role_id in old_config.team_role_ids.select(lambda x: str(x.role_id)): + continue + + self._server_configs.add_server_team_role_id_config(role_id) diff --git a/kdb-bot/src/bot_graphql/queries/server_config_query.py b/kdb-bot/src/bot_graphql/queries/server_config_query.py new file mode 100644 index 00000000..8672c4d2 --- /dev/null +++ b/kdb-bot/src/bot_graphql/queries/server_config_query.py @@ -0,0 +1,39 @@ +from cpl_core.database.context import DatabaseContextABC + +from bot_data.model.server_config_history import ServerConfigHistory +from bot_data.model.team_member_type_enum import TeamMemberTypeEnum +from bot_graphql.abc.data_query_with_history_abc import DataQueryWithHistoryABC + + +class ServerConfigQuery(DataQueryWithHistoryABC): + def __init__(self, db: DatabaseContextABC): + DataQueryWithHistoryABC.__init__(self, "ServerConfig", "CFG_ServerHistory", ServerConfigHistory, db) + + self.set_field("id", lambda config, *_: config.id) + self.set_field("messageDeleteTimer", lambda config, *_: config.message_delete_timer) + self.set_field("notificationChatId", lambda config, *_: config.notification_chat_id) + self.set_field("maxVoiceStateHours", lambda config, *_: config.max_voice_state_hours) + self.set_field("xpPerMessage", lambda config, *_: config.xp_per_message) + self.set_field("xpPerReaction", lambda config, *_: config.xp_per_reaction) + self.set_field("maxMessageXpPerHour", lambda config, *_: config.max_message_xp_per_hour) + self.set_field("xpPerOntimeHour", lambda config, *_: config.xp_per_ontime_hour) + self.set_field("xpPerEventParticipation", lambda config, *_: config.xp_per_event_participation) + self.set_field("xpPerAchievement", lambda config, *_: config.xp_per_achievement) + self.set_field("afkCommandChannelId", lambda config, *_: config.afk_command_channel_id) + self.set_field("helpVoiceChannelId", lambda config, *_: config.help_voice_channel_id) + self.set_field("teamChannelId", lambda config, *_: config.team_channel_id) + self.set_field("loginMessageChannelId", lambda config, *_: config.login_message_channel_id) + self.set_field("afkChannelIds", lambda config, *_: config.afk_channel_ids) + self.set_field( + "moderatorRoleIds", + lambda config, *_: config.team_role_ids.where( + lambda x: x.team_member_type == TeamMemberTypeEnum.moderator + ).select(lambda x: x.role_id), + ) + self.set_field( + "adminRoleIds", + lambda config, *_: config.team_role_ids.where( + lambda x: x.team_member_type == TeamMemberTypeEnum.admin + ).select(lambda x: x.role_id), + ) + self.set_field("server", lambda config, *_: config.server) diff --git a/kdb-bot/src/bot_graphql/queries/server_query.py b/kdb-bot/src/bot_graphql/queries/server_query.py index 5da12f2d..08c686b5 100644 --- a/kdb-bot/src/bot_graphql/queries/server_query.py +++ b/kdb-bot/src/bot_graphql/queries/server_query.py @@ -6,6 +6,7 @@ from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.client_repository_abc import ClientRepositoryABC from bot_data.abc.game_server_repository_abc import GameServerRepositoryABC from bot_data.abc.level_repository_abc import LevelRepositoryABC +from bot_data.abc.server_config_repository_abc import ServerConfigRepositoryABC from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC from bot_data.abc.user_joined_voice_channel_repository_abc import UserJoinedVoiceChannelRepositoryABC from bot_data.abc.user_repository_abc import UserRepositoryABC @@ -32,6 +33,7 @@ class ServerQuery(DataQueryWithHistoryABC): ujs: UserJoinedServerRepositoryABC, ujvs: UserJoinedVoiceChannelRepositoryABC, achievements: AchievementRepositoryABC, + server_configs: ServerConfigRepositoryABC, ): DataQueryWithHistoryABC.__init__(self, "Server", "ServersHistory", ServerHistory, db) @@ -62,6 +64,7 @@ class ServerQuery(DataQueryWithHistoryABC): self.add_collection( "achievement", lambda server, *_: achievements.get_achievements_by_server_id(server.id), AchievementFilter ) + self.set_field("config", lambda server, *_: server_configs.get_server_config_by_server(server.id)) @staticmethod def resolve_id(server: Server, *_): diff --git a/kdb-bot/src/bot_graphql/queries/technician_config_query.py b/kdb-bot/src/bot_graphql/queries/technician_config_query.py index 649dc510..3cda423f 100644 --- a/kdb-bot/src/bot_graphql/queries/technician_config_query.py +++ b/kdb-bot/src/bot_graphql/queries/technician_config_query.py @@ -27,11 +27,11 @@ class TechnicianConfigQuery(DataQueryWithHistoryABC): results = self._db.select( f""" - SELECT * - FROM CFG_TechnicianPingUrlsHistory - WHERE Id = {entry.id} - ORDER BY DateTo DESC; - """ + SELECT * + FROM CFG_TechnicianPingUrlsHistory + WHERE Id = {entry.id} + ORDER BY DateTo DESC; + """ ) for result in results: @@ -44,11 +44,11 @@ class TechnicianConfigQuery(DataQueryWithHistoryABC): results = self._db.select( f""" - SELECT * - FROM CFG_TechnicianIdsHistory - WHERE Id = {entry.id} - ORDER BY DateTo DESC; - """ + SELECT * + FROM CFG_TechnicianIdsHistory + WHERE Id = {entry.id} + ORDER BY DateTo DESC; + """ ) for result in results: diff --git a/kdb-bot/src/modules/config/events/config_on_ready_event.py b/kdb-bot/src/modules/config/events/config_on_ready_event.py index 614620c8..f9bf3012 100644 --- a/kdb-bot/src/modules/config/events/config_on_ready_event.py +++ b/kdb-bot/src/modules/config/events/config_on_ready_event.py @@ -29,7 +29,7 @@ class ConfigOnReadyEvent(OnReadyABC): 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) + server_config = self._server_config_repo.get_server_config_by_server(server.id) self._config.add_configuration( f"{type(server_config).__name__}_{server_config.server.discord_id}", server_config )