- 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.bot.logger.log(logging.INFO, f"This is bot version {VERSION}")
self._tdb = tdb.Database(aiohttp.ClientSession()) 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() @slash_command()
async def help(self, ctx): async def help(self, ctx):
await ctx.send( await ctx.send(

View File

@ -23,6 +23,7 @@ from interactions import Task, IntervalTrigger
from interactions import slash_command, listen from interactions import slash_command, listen
from interactions.api.events import Component from interactions.api.events import Component
from interactions.api.events import Startup from interactions.api.events import Startup
from interactions.client.errors import Forbidden
from token_bot.controller.alerts import AlertsController from token_bot.controller.alerts import AlertsController
from token_bot.controller.users import UsersController from token_bot.controller.users import UsersController
@ -84,15 +85,21 @@ class Tracker(Extension):
) )
for user in users_alerts: for user in users_alerts:
discord_user = await self.bot.fetch_user(user.user_id) 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 = [ embeds = [
Embed( Embed(
title="GoblinBot Tracker Alert Triggered", title="GoblinBot Tracker Alert Triggered",
color=0xB10000, color=0xB10000,
description=f"Hello, you requested to be sent an alert when the price of the World of Warcraft " description=f"You requested to be alerted on the WoW token price. You have {alert_tally} {alert_word}\n\n",
f"token reaches a certain value.\n\n",
) )
] ]
alerts_by_flavor = await gather_alerts_by_flavor(users_alerts[user])
for flavor in alerts_by_flavor: for flavor in alerts_by_flavor:
embeds.append( embeds.append(
await self._render_alert_flavor( await self._render_alert_flavor(
@ -103,11 +110,16 @@ class Tracker(Extension):
Embed( Embed(
title="", title="",
color=0xB10000, color=0xB10000,
description=f"As a reminder, you can remove an alert via ```/remove-alert```\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", f"or you can remove all alerts and user data via ```/remove-registration```\n",
) )
) )
try:
await discord_user.send(embeds=embeds) 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( self.bot.logger.log(
logging.INFO, "TokenBot Tracker: Done Processing User Alerts" 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) 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") @slash_command(description="The current retail token cost")
async def current(self, ctx: SlashContext): async def current(self, ctx: SlashContext):
current_str = await self.get_current_token(ctx, tdb.Flavor.RETAIL) current_str = await self.get_current_token(ctx, tdb.Flavor.RETAIL)