diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index 82842240..4fc3366e 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -21,6 +21,7 @@ } }, "common": { + "feature_not_activated": "Diese Funktion ist deaktiviert", "bot_has_no_permission_message": "Ey!!!\nWas soll das?\nIch habe keine Berechtigungen :(\nScheiß System...", "colors": { "blue": "Blau", @@ -170,6 +171,11 @@ "error": { "nothing_found": "Keine Gameserver gefunden." }, + "list_members": { + "title": "Mitglieder", + "description": "Konfigurierte Mitglieder:", + "users": "Mitglieder" + }, "list": { "title": "Gameserver", "description": "Konfigurierte Gameserver:", diff --git a/kdb-bot/src/bot_core/configuration/feature_flags_enum.py b/kdb-bot/src/bot_core/configuration/feature_flags_enum.py index 2ef3f70f..e00934cf 100644 --- a/kdb-bot/src/bot_core/configuration/feature_flags_enum.py +++ b/kdb-bot/src/bot_core/configuration/feature_flags_enum.py @@ -20,3 +20,4 @@ class FeatureFlagsEnum(Enum): api_only = "ApiOnly" presence = "Presence" version_in_presence = "VersionInPresence" + game_server = "GameServer" diff --git a/kdb-bot/src/bot_core/configuration/feature_flags_settings.py b/kdb-bot/src/bot_core/configuration/feature_flags_settings.py index f132cc03..654a5d9e 100644 --- a/kdb-bot/src/bot_core/configuration/feature_flags_settings.py +++ b/kdb-bot/src/bot_core/configuration/feature_flags_settings.py @@ -22,6 +22,7 @@ class FeatureFlagsSettings(ConfigurationModelABC): FeatureFlagsEnum.api_only.value: False, # 13.10.2022 #70 FeatureFlagsEnum.presence.value: True, # 03.10.2022 #56 FeatureFlagsEnum.version_in_presence.value: False, # 21.03.2023 #253 + FeatureFlagsEnum.game_server.value: False, # 25.09.2023 #366 } def __init__(self, **kwargs: dict): diff --git a/kdb-bot/src/bot_data/abc/user_game_ident_repository_abc.py b/kdb-bot/src/bot_data/abc/user_game_ident_repository_abc.py index fbaa0831..ba45e02b 100644 --- a/kdb-bot/src/bot_data/abc/user_game_ident_repository_abc.py +++ b/kdb-bot/src/bot_data/abc/user_game_ident_repository_abc.py @@ -14,6 +14,10 @@ class UserGameIdentRepositoryABC(ABC): def get_user_game_idents(self) -> List[UserGameIdent]: pass + @abstractmethod + def get_user_game_idents_by_game_server_id(self, id: int) -> List[UserGameIdent]: + pass + @abstractmethod def get_user_game_ident_by_id(self, id: int) -> UserGameIdent: pass diff --git a/kdb-bot/src/bot_data/model/user_game_ident.py b/kdb-bot/src/bot_data/model/user_game_ident.py index 22e7946f..79468d13 100644 --- a/kdb-bot/src/bot_data/model/user_game_ident.py +++ b/kdb-bot/src/bot_data/model/user_game_ident.py @@ -49,6 +49,15 @@ class UserGameIdent(TableABC): """ ) + @staticmethod + def get_select_by_game_server_id_string(id: int) -> str: + return str( + f""" + SELECT * FROM `UserGameIdents` + WHERE `GameServerId` = {id}; + """ + ) + @staticmethod def get_select_by_id_string(id: int) -> str: return str( diff --git a/kdb-bot/src/bot_data/service/user_game_ident_repository_service.py b/kdb-bot/src/bot_data/service/user_game_ident_repository_service.py index 5967d78b..d0dfc38b 100644 --- a/kdb-bot/src/bot_data/service/user_game_ident_repository_service.py +++ b/kdb-bot/src/bot_data/service/user_game_ident_repository_service.py @@ -51,6 +51,19 @@ class UserGameIdentRepositoryService(UserGameIdentRepositoryABC): return joins + def get_user_game_idents_by_game_server_id(self, game_server_id: int) -> List[UserGameIdent]: + joins = List(UserGameIdent) + self._logger.trace( + __name__, + f"Send SQL command: {UserGameIdent.get_select_by_game_server_id_string(game_server_id)}", + ) + results = self._context.select(UserGameIdent.get_select_by_game_server_id_string(game_server_id)) + for result in results: + self._logger.trace(__name__, f"Get UserGameIdent with id {result[0]}") + joins.append(self._from_result(result)) + + return joins + def get_user_game_ident_by_id(self, id: int) -> UserGameIdent: self._logger.trace( __name__, diff --git a/kdb-bot/src/modules/base/command/game_server_group.py b/kdb-bot/src/modules/base/command/game_server_group.py index 7d3073be..b822f3de 100644 --- a/kdb-bot/src/modules/base/command/game_server_group.py +++ b/kdb-bot/src/modules/base/command/game_server_group.py @@ -1,6 +1,7 @@ from typing import List as TList import discord +from cpl_core.configuration import ConfigurationABC from cpl_core.database.context import DatabaseContextABC from cpl_discord.command import DiscordCommandABC from cpl_discord.service import DiscordBotServiceABC @@ -11,38 +12,49 @@ from discord.ext.commands import Context from bot_core.abc.client_utils_abc import ClientUtilsABC from bot_core.abc.message_service_abc import MessageServiceABC +from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum +from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings from bot_core.helper.command_checks import CommandChecks from bot_core.logging.command_logger import CommandLogger from bot_data.abc.api_key_repository_abc import ApiKeyRepositoryABC from bot_data.abc.game_server_repository_abc import GameServerRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_game_ident_repository_abc import UserGameIdentRepositoryABC +from bot_data.abc.user_repository_abc import UserRepositoryABC from bot_data.model.game_server import GameServer +from bot_data.model.server_config import ServerConfig from modules.permission.abc.permission_service_abc import PermissionServiceABC class GameServerGroup(DiscordCommandABC): def __init__( self, + config: ConfigurationABC, logger: CommandLogger, message_service: MessageServiceABC, bot: DiscordBotServiceABC, client_utils: ClientUtilsABC, translate: TranslatePipe, servers: ServerRepositoryABC, + user: UserRepositoryABC, game_servers: GameServerRepositoryABC, + user_game_idents: UserGameIdentRepositoryABC, api_keys: ApiKeyRepositoryABC, db: DatabaseContextABC, permission_service: PermissionServiceABC, ): DiscordCommandABC.__init__(self) + self._config = config self._logger = logger self._message_service = message_service self._bot = bot self._client_utils = client_utils self._t = translate self._servers = servers + self._user = user self._game_servers = game_servers + self._user_game_idents = user_game_idents self._api_keys = api_keys self._db = db self._permissions = permission_service @@ -64,6 +76,11 @@ class GameServerGroup(DiscordCommandABC): if ctx.guild is None: return + settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{ctx.guild.id}") + if not FeatureFlagsSettings.get_flag_from_dict(settings.feature_flags, FeatureFlagsEnum.game_server): + await self._message_service.send_ctx_msg(ctx, self._t.transform("common.feature_not_activated")) + return + server = self._servers.get_server_by_discord_id(ctx.guild.id) game_servers = self._game_servers.get_game_servers_by_server_id(server.id) if game_servers.count() < 1: @@ -102,6 +119,14 @@ class GameServerGroup(DiscordCommandABC): async def add(self, ctx: Context, name: str, api_key_id: int): self._logger.debug(__name__, f"Received command game-server add {ctx}: {name} {api_key_id}") + if ctx.guild is None: + return + + settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{ctx.guild.id}") + if not FeatureFlagsSettings.get_flag_from_dict(settings.feature_flags, FeatureFlagsEnum.game_server): + await self._message_service.send_ctx_msg(ctx, self._t.transform("common.feature_not_activated")) + return + server = self._servers.get_server_by_discord_id(ctx.guild.id) api_key = self._api_keys.get_api_key_by_id(api_key_id) game_server = GameServer(name, server, api_key) @@ -133,6 +158,14 @@ class GameServerGroup(DiscordCommandABC): async def remove(self, ctx: Context, id: int): self._logger.debug(__name__, f"Received command game-server remove {ctx}: {id}") + if ctx.guild is None: + return + + settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{ctx.guild.id}") + if not FeatureFlagsSettings.get_flag_from_dict(settings.feature_flags, FeatureFlagsEnum.game_server): + await self._message_service.send_ctx_msg(ctx, self._t.transform("common.feature_not_activated")) + return + game_server = self._game_servers.get_game_server_by_id(id) self._game_servers.delete_game_server(game_server) self._db.save_changes() @@ -149,3 +182,44 @@ class GameServerGroup(DiscordCommandABC): app_commands.Choice(name=gs.name, value=gs.id) for gs in self._client_utils.get_auto_complete_list(game_servers, current, lambda x: x.name) ] + + @game_server.command(name="list-members") + @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_moderator() + async def list_members(self, ctx: Context, id: int, wait: int = None): + self._logger.debug(__name__, f"Received command game-server remove {ctx}: {id}") + + if ctx.guild is None: + return + + settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{ctx.guild.id}") + if not FeatureFlagsSettings.get_flag_from_dict(settings.feature_flags, FeatureFlagsEnum.game_server): + await self._message_service.send_ctx_msg(ctx, self._t.transform("common.feature_not_activated")) + return + + game_server = self._game_servers.get_game_server_by_id(id) + game_idents = self._user_game_idents.get_user_game_idents_by_game_server_id(game_server.id) + users = "" + for game_ident in game_idents: + users += f"\n{game_ident.user.name}" + + embed = discord.Embed( + title=self._t.transform("modules.base.game_server.list_members.title"), + description=self._t.transform("modules.base.game_server.list_members.description"), + color=int("ef9d0d", 16), + ) + embed.add_field(name=self._t.transform("modules.base.game_server.list_members.users"), value=users, inline=True) + await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait) + + self._logger.trace(__name__, f"Finished command game-server remove") + + @list_members.autocomplete("id") + async def id_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: + server = self._servers.get_server_by_discord_id(interaction.guild.id) + game_servers = self._game_servers.get_game_servers_by_server_id(server.id) + + return [ + app_commands.Choice(name=gs.name, value=gs.id) + for gs in self._client_utils.get_auto_complete_list(game_servers, current, lambda x: x.name) + ]