From ac09b3da0f7178a53e0fdfdca158f9f74bfd64fe Mon Sep 17 00:00:00 2001 From: skelmis Date: Sun, 1 Sep 2024 13:37:01 +1200 Subject: [PATCH] feat: allow /clear'ing queued suggestions Resolves BT-56 --- suggestions/bot.py | 2 +- suggestions/cogs/suggestion_cog.py | 37 +++++++++++++++++++----- suggestions/objects/queued_suggestion.py | 6 ++-- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/suggestions/bot.py b/suggestions/bot.py index a7ca7a1..a22745d 100644 --- a/suggestions/bot.py +++ b/suggestions/bot.py @@ -715,7 +715,7 @@ async def on_slash_command_error( "Command failed", "I've failed to find something, please retry whatever you were doing.\n" f"If this error persists please contact support.\n\nGuild ID: `{gid}`", - error_code=ErrorCode.GENERIC_NOT_FOUND.value, + error_code=ErrorCode.GENERIC_NOT_FOUND, error=error, ), ephemeral=True, diff --git a/suggestions/cogs/suggestion_cog.py b/suggestions/cogs/suggestion_cog.py index 95199a9..37695bc 100644 --- a/suggestions/cogs/suggestion_cog.py +++ b/suggestions/cogs/suggestion_cog.py @@ -18,6 +18,7 @@ ErrorHandled, MissingPermissionsToAccessQueueChannel, MissingQueueLogsChannel, + SuggestionNotFound, ) from suggestions.interaction_handler import InteractionHandler from suggestions.objects import Suggestion, GuildConfig, QueuedSuggestion @@ -435,15 +436,34 @@ async def clear( response: str {{CLEAR_ARG_RESPONSE}} """ await interaction.response.defer(ephemeral=True) - suggestion: Suggestion = await Suggestion.from_id( - suggestion_id, interaction.guild_id, self.state - ) - if suggestion.channel_id and suggestion.message_id: - await self.bot.delete_message( - message_id=suggestion.message_id, channel_id=suggestion.channel_id + try: + suggestion_type = "suggestion" + suggestion: Suggestion = await Suggestion.from_id( + suggestion_id, interaction.guild_id, self.state + ) + if suggestion.channel_id and suggestion.message_id: + await self.bot.delete_message( + message_id=suggestion.message_id, channel_id=suggestion.channel_id + ) + + await suggestion.mark_cleared_by(self.state, interaction.user.id, response) + except SuggestionNotFound: + # Maybe its a queued suggestion? + suggestion_type = "queued_suggestion" + suggestion: QueuedSuggestion = await QueuedSuggestion.from_id( + suggestion_id, interaction.guild_id, self.state + ) + if suggestion.channel_id and suggestion.message_id: + await self.bot.delete_message( + message_id=suggestion.message_id, channel_id=suggestion.channel_id + ) + + suggestion.channel_id = None + suggestion.message_id = None + await suggestion.resolve( + state=self.state, resolved_by=interaction.user.id, was_approved=False ) - await suggestion.mark_cleared_by(self.state, interaction.user.id, response) await interaction.send( self.bot.get_locale("CLEAR_INNER_MESSAGE", interaction.locale).format( suggestion_id @@ -456,7 +476,8 @@ async def clear( extra_metadata={ "author_id": interaction.author.id, "guild_id": interaction.guild_id, - "suggestion_id": suggestion.suggestion_id, + "suggestion_id": suggestion_id, + "suggestion_type": suggestion_type, }, ) await self.stats.log_stats( diff --git a/suggestions/objects/queued_suggestion.py b/suggestions/objects/queued_suggestion.py index 4c6737e..1a3d873 100644 --- a/suggestions/objects/queued_suggestion.py +++ b/suggestions/objects/queued_suggestion.py @@ -221,6 +221,8 @@ def as_dict(self) -> dict: "is_anonymous": self.is_anonymous, "still_in_queue": self.still_in_queue, "suggestion_author_id": self.suggestion_author_id, + "message_id": self.message_id, + "channel_id": self.channel_id, } if self._id: @@ -239,10 +241,6 @@ def as_dict(self) -> dict: if self.related_suggestion_id: data["related_suggestion_id"] = self.related_suggestion_id - if self.message_id is not None: - data["message_id"] = self.message_id - data["channel_id"] = self.channel_id - return data async def as_embed(self, bot: SuggestionsBot) -> Embed: