From 6327aa4c0fe6f2c95d1061637a679be8f35756bd Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Fri, 10 Jan 2025 21:00:50 +0100 Subject: [PATCH] Add game replace to gamelist --- src/modules/games_list/discord.py | 53 ++++++++++++++++++++++++++-- src/modules/games_list/games_list.py | 6 ++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/modules/games_list/discord.py b/src/modules/games_list/discord.py index aa94c27..3f547ec 100644 --- a/src/modules/games_list/discord.py +++ b/src/modules/games_list/discord.py @@ -66,7 +66,7 @@ class DiscordClient(discord.Client): client = DiscordClient() -@client.tree.command() +@client.tree.command(description="Добавление игры") @app_commands.describe( category="Раздел", customer="Кто заказал", @@ -147,7 +147,7 @@ async def game_list_autocomplete( return game_list.get_choices(current) -@client.tree.command() +@client.tree.command(description="Удаление игры") @app_commands.describe(game="Игра") @app_commands.autocomplete(game=game_list_autocomplete) async def delete(interaction: discord.Interaction, game: str): @@ -193,6 +193,55 @@ async def delete(interaction: discord.Interaction, game: str): await interaction.response.send_message("Игра удалена!", ephemeral=True) +@client.tree.command(description="Замена игры") +@app_commands.describe( + game="Старая игра", + new="Новая игра" +) +@app_commands.autocomplete(game=game_list_autocomplete) +async def replace(interaction: discord.Interaction, game: str, new: str): + if not isinstance(interaction.channel, Messageable): + await interaction.response.send_message( + "Interation not allowed in this channel!", ephemeral=True + ) + return + + streamer = await StreamerConfigRepository.find_one( + integration_discord_guild_id=interaction.guild_id, + integration_discord_games_list_channel_id=interaction.channel_id + ) + + if streamer is None: + await interaction.response.send_message( + "Interation not allowed in this channel!", ephemeral=True + ) + return + + if streamer.integrations.discord is None or streamer.integrations.discord.games_list is None: + await interaction.response.send_message( + "Need setup!", ephemeral=True + ) + return + + game_list = await GameList.get(streamer.twitch.id) + if game_list is None: + await interaction.response.send_message( + "Game list not found!", ephemeral=True + ) + return + + game_list.replace_game(game, new) + + game_list_message = await interaction.channel.fetch_message( + streamer.integrations.discord.games_list.message_id + ) + + await game_list_message.edit(content=str(game_list)) + await game_list.save() + + await interaction.response.send_message("Игра заменена!", ephemeral=True) + + async def start_discord_sevice(): logger.info("Starting Discord service...") diff --git a/src/modules/games_list/games_list.py b/src/modules/games_list/games_list.py index 14d46b7..ee25bea 100644 --- a/src/modules/games_list/games_list.py +++ b/src/modules/games_list/games_list.py @@ -79,6 +79,12 @@ class GameList: if category_item.name == _category: category_item.games.append(game_item) + def replace_game(self, game_name: str, new_game_name: str): + for category in self.data: + for game in category.games: + if game.name.startswith(game_name): + game.name = new_game_name + def delete_game(self, game_name: str): for category in self.data: for game in category.games: