From c5bb53c0e7531c5487ab15bc328ad01a6e6e44c5 Mon Sep 17 00:00:00 2001 From: Emily Doherty Date: Tue, 4 Nov 2025 20:30:21 -0800 Subject: [PATCH] - Add error handling for Forbidden exceptions when sending alerts - Improve alert message formatting --- token_bot/core.py | 5 ----- token_bot/tracker.py | 35 ++++++++++++++++++++--------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/token_bot/core.py b/token_bot/core.py index eb9b3dd..8193f08 100644 --- a/token_bot/core.py +++ b/token_bot/core.py @@ -22,11 +22,6 @@ class Core(Extension): self.bot.logger.log(logging.INFO, f"This is bot version {VERSION}") self._tdb = tdb.Database(aiohttp.ClientSession()) - @slash_command() - @check(is_owner()) - async def version(self, ctx): - await ctx.send(f"This is bot version {VERSION}", ephemeral=True) - @slash_command() async def help(self, ctx): await ctx.send( diff --git a/token_bot/tracker.py b/token_bot/tracker.py index 975a39d..342738a 100644 --- a/token_bot/tracker.py +++ b/token_bot/tracker.py @@ -23,6 +23,7 @@ from interactions import Task, IntervalTrigger from interactions import slash_command, listen from interactions.api.events import Component from interactions.api.events import Startup +from interactions.client.errors import Forbidden from token_bot.controller.alerts import AlertsController from token_bot.controller.users import UsersController @@ -84,15 +85,21 @@ class Tracker(Extension): ) for user in users_alerts: discord_user = await self.bot.fetch_user(user.user_id) + alerts_by_flavor = await gather_alerts_by_flavor(users_alerts[user]) + alert_tally = 0 + alert_word = "alert" + if alert_tally > 2: + alert_word += 's' + for flavor in alerts_by_flavor: + for _ in alerts_by_flavor[flavor]: + alert_tally += 1 embeds = [ Embed( title="GoblinBot Tracker Alert Triggered", color=0xB10000, - description=f"Hello, you requested to be sent an alert when the price of the World of Warcraft " - f"token reaches a certain value.\n\n", + description=f"You requested to be alerted on the WoW token price. You have {alert_tally} {alert_word}\n\n", ) ] - alerts_by_flavor = await gather_alerts_by_flavor(users_alerts[user]) for flavor in alerts_by_flavor: embeds.append( await self._render_alert_flavor( @@ -103,11 +110,16 @@ class Tracker(Extension): Embed( title="", color=0xB10000, - description=f"As a reminder, you can remove an alert via ```/remove-alert```\n" - f"or you can remove all alerts and user data via ```/remove-registration```\n", + description=f"You can remove an alert via ```/remove-alert```\n" + f"or you can remove all alerts and user data via ```/remove-registration```\n", ) ) - await discord_user.send(embeds=embeds) + try: + await discord_user.send(embeds=embeds) + except Forbidden: + self.bot.logger.log( + logging.ERROR, f"User: {discord_user.id} has no permissions to send alerts, skipping") + self.bot.logger.log( logging.INFO, "TokenBot Tracker: Done Processing User Alerts" ) @@ -160,13 +172,6 @@ class Tracker(Extension): await ctx.send("All alert subscriptions and user data deleted", ephemeral=True) - @slash_command( - name="exists", description="Check if you are registered with GoblinBot" "" - ) - @check(is_owner()) - async def exists(self, ctx: SlashContext): - await ctx.send(str(await self._users.exists(ctx.user.id)), ephemeral=True) - @slash_command(description="The current retail token cost") async def current(self, ctx: SlashContext): current_str = await self.get_current_token(ctx, tdb.Flavor.RETAIL) @@ -444,7 +449,7 @@ class Tracker(Extension): return alert_type async def _alert_select_menu_handler( - self, ctx: SlashContext, menu: StringSelectMenu, message: Message + self, ctx: SlashContext, menu: StringSelectMenu, message: Message ) -> AlertType: try: component: Component = await self.bot.wait_for_component( @@ -489,7 +494,7 @@ class Tracker(Extension): return True async def _render_alert_flavor( - self, alerts: List[Alert], user: User | None = None + self, alerts: List[Alert], user: User | None = None ) -> Embed: region = alerts[0].region flavor = alerts[0].flavor