Skip to content

Commit

Permalink
Reminders: Refactor all opt-in button related logic into View class
Browse files Browse the repository at this point in the history
- More resilient handling of API errors.
- Don't rely on string manipulation to disable the button.
  • Loading branch information
hedyhli committed Mar 26, 2024
1 parent cdaa59f commit fd40f30
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions bot/exts/utils/reminders.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,21 @@ async def cancel(self, interaction: Interaction, button: discord.ui.Button) -> N
class OptInReminderMentionView(discord.ui.View):
"""A button to opt-in to get notified of someone else's reminder."""

def __init__(self, cog: "Reminders", reminder: dict):
def __init__(self, cog: "Reminders", reminder: dict, expiration: Duration):
super().__init__()

self.cog = cog
self.reminder = reminder

async def get_embed_description(self) -> str:
self.timeout = min(
(expiration - datetime.now(UTC)).total_seconds(),
REMINDER_MENTION_BUTTON_TIMEOUT
)

async def get_embed_description(
self,
message: str = "Click on the button to add yourself to the list of mentions."
) -> str:
"""Return a string for use in the embed that shows the button."""

Check failure on line 100 in bot/exts/utils/reminders.py

View workflow job for this annotation

GitHub Actions / lint-test / lint-test

Ruff (D202)

bot/exts/utils/reminders.py:100:9: D202 No blank lines allowed after function docstring (found 1)

description = "The following user(s) will be notified when the reminder arrives:\n"
Expand All @@ -96,7 +105,8 @@ async def get_embed_description(self) -> str:
[self.reminder["author"]] + self.reminder["mentions"]
)
])
description += "\n\nClick on the button to add yourself to the list of mentions."
if message:
description += f"\n\n{message}"

return description

Expand All @@ -107,11 +117,13 @@ async def button_callback(self, interaction: discord.Interaction, button: discor
# This is required in case the reminder was edited/deleted between
# creation and the opt-in button click.
try:
self.reminder = await self.cog.bot.api_client.get(f"bot/reminders/{self.reminder['id']}")
api_response = await self.cog.bot.api_client.get(f"bot/reminders/{self.reminder['id']}")
except ResponseCodeError as e:
await self.handle_api_error(interaction, button, e)
return

self.reminder = api_response

# Check whether the user should be added.
if interaction.user.id == self.reminder['author']:

Check failure on line 128 in bot/exts/utils/reminders.py

View workflow job for this annotation

GitHub Actions / lint-test / lint-test

Ruff (Q000)

bot/exts/utils/reminders.py:128:49: Q000 Single quotes found but double quotes preferred
await interaction.response.send_message(
Expand Down Expand Up @@ -139,11 +151,13 @@ async def button_callback(self, interaction: discord.Interaction, button: discor

# Add the user to the list of mentions.
try:
self.reminder = await self.cog.add_mention_opt_in(self.reminder, interaction.user.id)
api_response = await self.cog.add_mention_opt_in(self.reminder, interaction.user.id)
except ResponseCodeError as e:
await self.handle_api_error(interaction, button, e)
return

self.reminder = api_response

# Confirm that it was successful.
await interaction.response.send_message(
"You were successfully added to the list of mentions for that reminder.",
Expand Down Expand Up @@ -198,7 +212,7 @@ async def disable(
self,
interaction: discord.Interaction,
button: discord.ui.Button,
reason: str | None = None,
reason: str = "",
) -> None:
"""Disable the button and add an optional reason to the original interaction message."""

Check failure on line 217 in bot/exts/utils/reminders.py

View workflow job for this annotation

GitHub Actions / lint-test / lint-test

Ruff (D202)

bot/exts/utils/reminders.py:217:9: D202 No blank lines allowed after function docstring (found 1)

Expand All @@ -208,15 +222,11 @@ async def disable(

try:
embed = embeds[0]
embed.description = embed.description.split("\n\n", maxsplit=2)[0]
if reason:
embed.description += "\n\n" + reason

embed.description = await self.get_embed_description(reason)
await interaction.message.edit(embed=embed, view=self)

except:

Check failure on line 227 in bot/exts/utils/reminders.py

View workflow job for this annotation

GitHub Actions / lint-test / lint-test

Ruff (E722)

bot/exts/utils/reminders.py:227:9: E722 Do not use bare `except`
log.trace("Unable to disable the reminder notification button.")
await interaction.message.edit(embeds=embeds, view=None)
await interaction.message.edit(embeds=[], view=None)


class Reminders(Cog):
Expand Down Expand Up @@ -534,16 +544,12 @@ async def new_reminder(
)

# Add a button for others to also get notified.
view = OptInReminderMentionView(self, reminder)

button_embed = discord.Embed(
description=await view.get_embed_description(),
)
button_timeout = min(
(expiration - datetime.now(UTC)).total_seconds(),
REMINDER_MENTION_BUTTON_TIMEOUT
view = OptInReminderMentionView(self, reminder, expiration)
await ctx.send(
view=view,
delete_after=view.timeout,
embed=discord.Embed(description=await view.get_embed_description())
)
await ctx.send(embed=button_embed, view=view, delete_after=button_timeout)

self.schedule_reminder(reminder)

Expand Down

0 comments on commit fd40f30

Please sign in to comment.