Skip to content

Commit 8efc22d

Browse files
author
hedy
committed
Reminders: Simplify helper function to get button embed
This removes the need of any form of error handling from `embeds` list indexing, and makes the code more concise and readable. The drawback is that a whole new embed object have to be allocated each time, rather than editing the description like before.
1 parent e753586 commit 8efc22d

1 file changed

Lines changed: 11 additions & 25 deletions

File tree

bot/exts/utils/reminders.py

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,22 @@ def __init__(self, cog: "Reminders", reminder: dict, expiration: Duration):
9393
REMINDER_MENTION_BUTTON_TIMEOUT
9494
)
9595

96-
async def get_embed_description(
96+
async def get_embed(
9797
self,
9898
message: str = "Click on the button to add yourself to the list of mentions."
99-
) -> str:
100-
"""Return a string for use in the embed that shows the button."""
99+
) -> discord.Embed:
100+
"""Return an embed to show the button together with."""
101101
description = "The following user(s) will be notified when the reminder arrives:\n"
102102
description += " ".join([
103103
mentionable.mention async for mentionable in self.cog.get_mentionables(
104104
[self.reminder["author"]] + self.reminder["mentions"]
105105
)
106106
])
107+
107108
if message:
108109
description += f"\n\n{message}"
109110

110-
return description
111+
return discord.Embed(description=description)
111112

112113
@discord.ui.button(emoji="🔔", label="Notify me", style=discord.ButtonStyle.green)
113114
async def button_callback(self, interaction: Interaction, button: discord.ui.Button) -> None:
@@ -164,12 +165,7 @@ async def button_callback(self, interaction: Interaction, button: discord.ui.But
164165
)
165166

166167
# Update the embed to show the new list of mentions.
167-
try:
168-
embed = interaction.message.embeds[0]
169-
embed.description = await self.get_embed_description()
170-
await interaction.message.edit(embed=embed)
171-
except Exception:
172-
log.trace(f"Unable to edit the interaction message for reminder #{self.reminder['id']}.")
168+
await interaction.message.edit(embed=await self.get_embed())
173169

174170
async def handle_api_error(
175171
self,
@@ -208,16 +204,10 @@ async def handle_api_error(
208204
async def disable(self, interaction: Interaction, button: discord.ui.Button, reason: str = "") -> None:
209205
"""Disable the button and add an optional reason to the original interaction message."""
210206
button.disabled = True
211-
212-
embeds = interaction.message.embeds
213-
214-
try:
215-
embed = embeds[0]
216-
embed.description = await self.get_embed_description(reason)
217-
await interaction.message.edit(embed=embed, view=self)
218-
except Exception:
219-
log.trace("Unable to disable the reminder notification button.")
220-
await interaction.message.edit(embeds=[], view=None)
207+
await interaction.message.edit(
208+
embed=await self.get_embed(reason),
209+
view=self,
210+
)
221211

222212

223213
class Reminders(Cog):
@@ -535,11 +525,7 @@ async def new_reminder(
535525

536526
# Add a button for others to also get notified.
537527
view = OptInReminderMentionView(self, reminder, expiration)
538-
await ctx.send(
539-
view=view,
540-
delete_after=view.timeout,
541-
embed=discord.Embed(description=await view.get_embed_description())
542-
)
528+
await ctx.send(embed=await view.get_embed(), view=view, delete_after=view.timeout)
543529

544530
self.schedule_reminder(reminder)
545531

0 commit comments

Comments
 (0)