General tracker.py improvements

Mostly speed-ups by awaiting multiple awaits where there's not a dependency on each other's results, and bounds checks
This commit is contained in:
Emily Doherty 2024-12-08 16:48:05 -08:00
parent 62c205aa35
commit 3a06464c29

View File

@ -157,7 +157,7 @@ class Tracker(Extension):
@slash_command(
name="add-alert",
description="List all alerts you have signed up for"
description="Add an alert listener"
)
async def add_alert(self, ctx: SlashContext):
if not await self._users.exists(ctx.user.id):
@ -184,8 +184,10 @@ class Tracker(Extension):
else:
alert = Alert(alert_type, flavor, user.region)
if not await self._users.is_subscribed(user, alert):
await self._users.add_alert(user, alert)
await self._alerts.add_user(alert, user)
await asyncio.gather(
self._users.add_alert(user, alert),
self._alerts.add_user(alert, user)
)
await ctx.send("Successfully added alert", ephemeral=True)
else:
@ -197,14 +199,26 @@ class Tracker(Extension):
description="Remove an alert you have signed up for"
)
async def remove_alert(self, ctx: SlashContext):
if not await self._users.exists(ctx.user.id):
await ctx.send("You are not registered with any region\n"
"Please register with /register before adding alerts",
ephemeral=True)
return
user = await self._users.get(ctx.user.id)
alerts = await self._users.list_alerts(user)
if len(alerts) == 0:
await ctx.send("You do not have any alerts registered", ephemeral=True)
return
try:
alert = await self.remove_alert_select_menu(ctx, user)
except TimeoutError:
return
else:
await self._users.remove_alert(user, alert)
await self._alerts.remove_user(alert, user)
await asyncio.gather(
self._users.remove_alert(user, alert),
self._alerts.remove_user(alert, user)
)
await ctx.send("Successfully removed alert", ephemeral=True)