- Add error handling for Forbidden exceptions when sending alerts

- Improve alert message formatting
This commit is contained in:
Emily Doherty 2025-11-04 20:30:21 -08:00
parent 2164e98730
commit c5bb53c0e7
2 changed files with 20 additions and 20 deletions

View File

@ -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(

View File

@ -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