Skip to content

Commit

Permalink
feat: allow /clear'ing queued suggestions
Browse files Browse the repository at this point in the history
Resolves BT-56
  • Loading branch information
Skelmis committed Sep 1, 2024
1 parent 878d59b commit ac09b3d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion suggestions/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
37 changes: 29 additions & 8 deletions suggestions/cogs/suggestion_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
ErrorHandled,
MissingPermissionsToAccessQueueChannel,
MissingQueueLogsChannel,
SuggestionNotFound,
)
from suggestions.interaction_handler import InteractionHandler
from suggestions.objects import Suggestion, GuildConfig, QueuedSuggestion
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down
6 changes: 2 additions & 4 deletions suggestions/objects/queued_suggestion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit ac09b3d

Please sign in to comment.