diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index 49f675b69e..6dd03c73c9 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 5d211c595b..04f33bfbaf 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 b105120955..58105c0b48 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 d1b6558dfc..1ab0afe434 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 9591fb2179..fe05324100 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')