From 8cd67cd68cce4c1a168aa3d3933860a7236ae5c4 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 9 Nov 2022 17:59:55 +0100 Subject: [PATCH] Added stats edit command #46 --- kdb-bot/src/bot/translation/de.json | 4 +++ .../src/bot_core/service/message_service.py | 7 ++-- .../src/modules/stats/command/stats_group.py | 32 +++++++++++++++++-- kdb-bot/src/modules/stats/model/statistic.py | 8 +++++ .../modules/stats/ui/add_statistic_form.py | 27 ++++++++++++++-- 5 files changed, 69 insertions(+), 9 deletions(-) diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index 49f675b6..6dd03c73 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -231,6 +231,10 @@ "add": { "failed": "Statistik kann nicht hinzugefügt werden :(", "success": "Statistik wurde hinzugefügt :D" + }, + "edit": { + "failed": "Statistik kann nicht bearbeitet werden :(", + "success": "Statistik wurde gespeichert :D" } } }, diff --git a/kdb-bot/src/bot_core/service/message_service.py b/kdb-bot/src/bot_core/service/message_service.py index 5d211c59..04f33bfb 100644 --- a/kdb-bot/src/bot_core/service/message_service.py +++ b/kdb-bot/src/bot_core/service/message_service.py @@ -126,12 +126,11 @@ class MessageService(MessageServiceABC): return self._logger.debug(__name__, f'Try to send message\t\t{message}\n\tto: {interaction.channel}') - msg = None try: if isinstance(message, discord.Embed): - msg = await interaction.response.send_message(embed=message) + await interaction.response.send_message(embed=message) else: - msg = await interaction.response.send_message(message) + await interaction.response.send_message(message) except Exception as e: self._logger.error(__name__, f'Send message to channel {interaction.channel.id} failed', e) else: @@ -146,4 +145,4 @@ class MessageService(MessageServiceABC): if is_persistent: return - await self.delete_message(msg, without_tracking) + await self.delete_message(await interaction.original_response(), without_tracking) diff --git a/kdb-bot/src/modules/stats/command/stats_group.py b/kdb-bot/src/modules/stats/command/stats_group.py index b1051209..58105c0b 100644 --- a/kdb-bot/src/modules/stats/command/stats_group.py +++ b/kdb-bot/src/modules/stats/command/stats_group.py @@ -135,16 +135,42 @@ class StatsGroup(DiscordCommandABC): @stats.command() @commands.guild_only() async def add(self, ctx: Context, name: str): - self._logger.debug(__name__, f'Received command stats list {ctx}') + self._logger.debug(__name__, f'Received command stats add {ctx}: {name}') if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): return if not self._permissions.is_member_technician(ctx.author): await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished command stats list') + self._logger.trace(__name__, f'Finished command stats add') return form = AddStatisticForm(stats, name, self._message_service, self._logger, self._t) - self._logger.trace(__name__, f'Finished stats command') + self._logger.trace(__name__, f'Finished stats add command') self._logger.trace(__name__, f'Started stats command form') await ctx.interaction.response.send_modal(form) + + @stats.command() + @commands.guild_only() + async def edit(self, ctx: Context, name: str): + self._logger.debug(__name__, f'Received command stats edit {ctx}: {name}') + if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): + return + + if not self._permissions.is_member_technician(ctx.author): + await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) + self._logger.trace(__name__, f'Finished command stats edit') + return + + try: + statistic = stats.where(lambda s: s.name == name).single() + form = AddStatisticForm(stats, name, self._message_service, self._logger, self._t, code=statistic.code, description=statistic.description) + self._logger.trace(__name__, f'Finished stats edit command') + self._logger.trace(__name__, f'Started stats command form') + await ctx.interaction.response.send_modal(form) + except Exception as e: + self._logger.error(__name__, f'Cannot view statistic {name}', e) + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.stats.edit.failed')) + + @edit.autocomplete('name') + async def edit_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: + return [app_commands.Choice(name=f'{statistic.name}: {statistic.description}', value=statistic.name) for statistic in stats] diff --git a/kdb-bot/src/modules/stats/model/statistic.py b/kdb-bot/src/modules/stats/model/statistic.py index d1b6558d..1ab0afe4 100644 --- a/kdb-bot/src/modules/stats/model/statistic.py +++ b/kdb-bot/src/modules/stats/model/statistic.py @@ -16,6 +16,14 @@ class Statistic: def description(self) -> str: return self._description + @description.setter + def description(self, value: str): + self._description = value + @property def code(self) -> str: return self._code + + @code.setter + def code(self, value: str): + self._code = value diff --git a/kdb-bot/src/modules/stats/ui/add_statistic_form.py b/kdb-bot/src/modules/stats/ui/add_statistic_form.py index 9591fb21..fe053241 100644 --- a/kdb-bot/src/modules/stats/ui/add_statistic_form.py +++ b/kdb-bot/src/modules/stats/ui/add_statistic_form.py @@ -20,6 +20,8 @@ class AddStatisticForm(ui.Modal): message_service: MessageServiceABC, logger: CommandLogger, t: TranslatePipe, + code: str = None, + description: str = None, ): ui.Modal.__init__(self, title=name) @@ -29,7 +31,28 @@ class AddStatisticForm(ui.Modal): self._logger = logger self._t = t + if code is not None: + self.code.default = code + + if description is not None: + self.description.default = description + async def on_submit(self, interaction: discord.Interaction): - self._stats.append(Statistic(self._name, self.description.value, self.code.value)) - await self._message_service.send_interaction_msg(interaction, self._t.transform('modules.stats.add.success')) + statistic = self._stats.where(lambda s: s.name == self._name).single_or_default() + try: + if statistic is None: + self._stats.append(Statistic(self._name, self.description.value, self.code.value)) + await self._message_service.send_interaction_msg(interaction, self._t.transform('modules.stats.add.success')) + return + + statistic.description = self.description.value + statistic.code = self.code.value + await self._message_service.send_interaction_msg(interaction, self._t.transform('modules.stats.edit.success')) + except Exception as e: + self._logger.error(__name__, f'Save statistic {self._name} failed', e) + if statistic is None: + await self._message_service.send_interaction_msg(interaction, self._t.transform('modules.stats.add.failed')) + else: + await self._message_service.send_interaction_msg(interaction, self._t.transform('modules.stats.edit.failed')) + self._logger.trace(__name__, f'Finished stats command form')