Skip to content

Commit

Permalink
refactor: clean up suggestion message finalization
Browse files Browse the repository at this point in the history
  • Loading branch information
Skelmis committed Nov 23, 2022
1 parent 192f0d6 commit d1cd3c9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 99 deletions.
111 changes: 12 additions & 99 deletions suggestions/cogs/suggestion_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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",
Expand Down
61 changes: 61 additions & 0 deletions suggestions/objects/suggestion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit d1cd3c9

Please sign in to comment.