From d1cd3c9bf88e2a75ab16810f4aae2820ea23e64d Mon Sep 17 00:00:00 2001 From: skelmis Date: Wed, 23 Nov 2022 14:44:15 +1300 Subject: [PATCH] refactor: clean up suggestion message finalization --- suggestions/cogs/suggestion_cog.py | 111 ++++------------------------- suggestions/objects/suggestion.py | 61 ++++++++++++++++ 2 files changed, 73 insertions(+), 99 deletions(-) diff --git a/suggestions/cogs/suggestion_cog.py b/suggestions/cogs/suggestion_cog.py index 0882e04..3ef838b 100644 --- a/suggestions/cogs/suggestion_cog.py +++ b/suggestions/cogs/suggestion_cog.py @@ -300,56 +300,12 @@ async def approve( suggestion_id, interaction.guild_id, self.state ) await suggestion.mark_approved_by(self.state, interaction.author.id, response) - if guild_config.keep_logs: - await suggestion.save_reaction_results(self.bot, interaction) - # In place suggestion edit - channel: WrappedChannel = await self.bot.get_or_fetch_channel( - suggestion.channel_id - ) - message: disnake.Message = await channel.fetch_message( - suggestion.message_id - ) - - try: - await message.edit( - embed=await suggestion.as_embed(self.bot), components=None - ) - except disnake.Forbidden: - raise commands.MissingPermissions( - missing_permissions=[ - "Missing permissions edit suggestions in your suggestions channel" - ] - ) - - try: - await message.clear_reactions() - except disnake.Forbidden: - raise commands.MissingPermissions( - missing_permissions=[ - "Missing permissions clear reactions in your suggestions channel" - ] - ) - - else: - # Move the suggestion to the logs channel - await suggestion.save_reaction_results(self.bot, interaction) - await suggestion.try_delete(self.bot, interaction) - channel: WrappedChannel = await self.bot.get_or_fetch_channel( - guild_config.log_channel_id - ) - try: - message: disnake.Message = await channel.send( - embed=await suggestion.as_embed(self.bot) - ) - except disnake.Forbidden: - raise commands.MissingPermissions( - missing_permissions=[ - "Missing permissions to send in configured log channel" - ] - ) - suggestion.message_id = message.id - suggestion.channel_id = channel.id - await self.state.suggestions_db.upsert(suggestion, suggestion) + await suggestion.edit_message_after_finalization( + state=self.state, + bot=self.bot, + interaction=interaction, + guild_config=guild_config, + ) await interaction.send(f"You have approved **{suggestion_id}**", ephemeral=True) log.debug( @@ -396,56 +352,13 @@ async def reject( suggestion_id, interaction.guild_id, self.state ) await suggestion.mark_rejected_by(self.state, interaction.author.id, response) - if guild_config.keep_logs: - await suggestion.save_reaction_results(self.bot, interaction) - # In place suggestion edit - channel: WrappedChannel = await self.bot.get_or_fetch_channel( - suggestion.channel_id - ) - message: disnake.Message = await channel.fetch_message( - suggestion.message_id - ) - - try: - await message.edit( - embed=await suggestion.as_embed(self.bot), components=None - ) - except disnake.Forbidden: - raise commands.MissingPermissions( - missing_permissions=[ - "Missing permissions edit suggestions in your suggestions channel" - ] - ) - - try: - await message.clear_reactions() - except disnake.Forbidden: - raise commands.MissingPermissions( - missing_permissions=[ - "Missing permissions clear reactions in your suggestions channel" - ] - ) + await suggestion.edit_message_after_finalization( + state=self.state, + bot=self.bot, + interaction=interaction, + guild_config=guild_config, + ) - else: - # Move the suggestion to the logs channel - await suggestion.save_reaction_results(self.bot, interaction) - await suggestion.try_delete(self.bot, interaction) - channel: WrappedChannel = await self.bot.get_or_fetch_channel( - guild_config.log_channel_id - ) - try: - message: disnake.Message = await channel.send( - embed=await suggestion.as_embed(self.bot) - ) - except disnake.Forbidden: - raise commands.MissingPermissions( - missing_permissions=[ - "Missing permissions to send in configured log channel" - ] - ) - suggestion.message_id = message.id - suggestion.channel_id = channel.id - await self.state.suggestions_db.upsert(suggestion, suggestion) await interaction.send(f"You have rejected **{suggestion_id}**", ephemeral=True) log.debug( "User %s rejected suggestion %s in guild %s", diff --git a/suggestions/objects/suggestion.py b/suggestions/objects/suggestion.py index 19ea095..3b85374 100644 --- a/suggestions/objects/suggestion.py +++ b/suggestions/objects/suggestion.py @@ -11,6 +11,7 @@ from alaric.logical import AND from bot_base.wraps import WrappedChannel from disnake import Embed, Guild +from disnake.ext import commands from suggestions import ErrorCode from suggestions.exceptions import ErrorHandled, SuggestionNotFound @@ -622,3 +623,63 @@ async def update_vote_count( ephemeral=True, ) raise ErrorHandled + + async def edit_message_after_finalization( + self, + *, + guild_config: GuildConfig, + bot: SuggestionsBot, + state: State, + interaction: disnake.GuildCommandInteraction, + ): + """ + Modify the suggestion message inline with the guilds + configuration now that the suggestion has entered one of + the following states: + - Approved + - Rejected + """ + if guild_config.keep_logs: + await self.save_reaction_results(bot, interaction) + # In place suggestion edit + channel: WrappedChannel = await bot.get_or_fetch_channel(self.channel_id) + message: disnake.Message = await channel.fetch_message(self.message_id) + + try: + await message.edit(embed=await self.as_embed(bot), components=None) + except disnake.Forbidden: + raise commands.MissingPermissions( + missing_permissions=[ + "Missing permissions edit suggestions in your suggestions channel" + ] + ) + + try: + await message.clear_reactions() + except disnake.Forbidden: + raise commands.MissingPermissions( + missing_permissions=[ + "Missing permissions clear reactions in your suggestions channel" + ] + ) + + else: + # Move the suggestion to the logs channel + await self.save_reaction_results(bot, interaction) + await self.try_delete(bot, interaction) + channel: WrappedChannel = await bot.get_or_fetch_channel( + guild_config.log_channel_id + ) + try: + message: disnake.Message = await channel.send( + embed=await self.as_embed(bot) + ) + except disnake.Forbidden: + raise commands.MissingPermissions( + missing_permissions=[ + "Missing permissions to send in configured log channel" + ] + ) + self.message_id = message.id + self.channel_id = channel.id + await state.suggestions_db.upsert(self, self)