Skip to content

Commit

Permalink
feat(schedule): add retries (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
sloria authored Dec 31, 2021
1 parent 73c5942 commit 598be65
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions bot/exts/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,30 +115,38 @@ async def schedule_command(self, inter: GuildCommandInteraction):
async def schedule_new(self, inter: GuildCommandInteraction):
"""Quickly add a new scheduled event with guided prompts."""
# Step 1: Prompt for the start time
start_time, start_time_message = await self._prompt_for_text_input(
inter, prompt=START_TIME_PROMPT, is_initial_interaction=True
)
logger.info(f"attempting to schedule new practice session: {start_time}")
user_timezone = await store.get_user_timezone(user_id=inter.user.id)
try:
scheduled_start_time, used_timezone = parse_practice_time(
start_time, user_timezone=user_timezone
)
except NoTimeZoneError:
raise commands.errors.BadArgument(
f'⚠️Could not parse time zone from "{start_time}". Make sure to include a time zone, e.g. "{PACIFIC_CURRENT_NAME.lower()}".'
)
except pytz.UnknownTimeZoneError:
raise commands.errors.BadArgument("⚠️Invalid time zone. Please try again.")
if not scheduled_start_time:
raise commands.errors.BadArgument(
f'⚠️Could not parse "{start_time}" into a date or time. Make sure to include "am" or "pm" as well as a timezone, e.g. "{PACIFIC_CURRENT_NAME.lower()}".'
)
assert used_timezone is not None
if scheduled_start_time < utcnow():
raise commands.errors.BadArgument(
"⚠Parsed date or time is in the past. Try again with a future date or time."
tries = 0
max_retries = 3
scheduled_start_time: dt.datetime | None = None
current_prompt = START_TIME_PROMPT
while scheduled_start_time is None:
if tries >= max_retries:
await inter.send(
"⚠️I can't seem to parse your messages. Try running `/schedule new` again and use more specific input.",
ephemeral=True,
)
return
start_time, start_time_message = await self._prompt_for_text_input(
inter,
prompt=current_prompt,
is_initial_interaction=tries == 0,
)
logger.info(f"attempting to schedule new practice session: {start_time}")
user_timezone = await store.get_user_timezone(user_id=inter.user.id)
try:
scheduled_start_time, used_timezone = parse_practice_time(
start_time, user_timezone=user_timezone
)
except NoTimeZoneError:
current_prompt = f'⚠️Could not parse time zone from "{start_time}". Try again. Make sure to include a time zone, e.g. "{PACIFIC_CURRENT_NAME.lower()}".'
except pytz.UnknownTimeZoneError:
current_prompt = "⚠️Invalid time zone. Please try again."
else:
if not scheduled_start_time:
current_prompt = f'⚠️Could not parse "{start_time}" into a date or time. Try again. Make sure to include "am" or "pm" as well as a timezone, e.g. "{PACIFIC_CURRENT_NAME.lower()}".'
elif scheduled_start_time < utcnow():
current_prompt = "⚠Parsed date or time is in the past. Try again with a future date or time."
tries += 1
await start_time_message.edit(
content=(
"☑️ **When will your event start?**\n"
Expand Down Expand Up @@ -217,6 +225,8 @@ async def schedule_new(self, inter: GuildCommandInteraction):
embed=make_event_embed(event),
view=LinkView(label="Event Link", url=event_url),
)

assert used_timezone is not None
if str(user_timezone) != str(used_timezone):
await store.set_user_timezone(user.id, used_timezone)
new_timezone_display = display_timezone(used_timezone, utcnow()).lower()
Expand Down

0 comments on commit 598be65

Please sign in to comment.