paper-dynasty-discord/helpers/refractor_notifs.py
Cal Corum 25b63b407f
All checks were successful
Ruff Lint / lint (pull_request) Successful in 21s
feat: include refractor card image in tier-up notification embed (#144)
Closes #144

- build_tier_up_embed() accepts optional image_url and calls set_image() when provided
- notify_tier_completion() accepts optional image_url and passes it through
- _trigger_variant_renders() now captures the render response image_url per player and returns a player_id->image_url dict
- _run_post_game_refractor_hook() triggers renders first (to obtain image URLs), then sends notifications with card art included
- Updated test_post_game_refractor_hook.py to reflect new render-before-notify ordering and image_url kwarg in notify calls

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-08 09:38:43 -05:00

97 lines
3.0 KiB
Python

"""
Refractor Tier Completion Notifications
Builds and sends Discord embeds when a player completes a refractor tier
during post-game evaluation. Each tier-up event gets its own embed.
Notification failures are non-fatal: the send is wrapped in try/except so
a Discord API hiccup never disrupts game flow.
"""
import logging
import discord
from helpers.refractor_constants import TIER_NAMES, NOTIF_TIER_COLORS as TIER_COLORS
logger = logging.getLogger("discord_app")
FOOTER_TEXT = "Paper Dynasty Refractor"
def build_tier_up_embed(tier_up: dict, image_url: str | None = None) -> discord.Embed:
"""Build a Discord embed for a tier-up event.
Parameters
----------
tier_up:
Dict with keys: player_name, old_tier, new_tier, current_value, track_name.
image_url:
Optional S3 URL for the newly rendered refractor card image. When
provided, the card art is shown as the embed image.
Returns
-------
discord.Embed
A fully configured embed ready to send to a channel.
"""
player_name: str = tier_up["player_name"]
new_tier: int = tier_up["new_tier"]
track_name: str = tier_up["track_name"]
tier_name = TIER_NAMES.get(new_tier, f"Tier {new_tier}")
color = TIER_COLORS.get(new_tier, 0x2ECC71)
if new_tier >= 4:
# Superfractor — special title and description.
embed = discord.Embed(
title="SUPERFRACTOR!",
description=(
f"**{player_name}** has reached maximum refractor tier on the **{track_name}** track"
),
color=color,
)
else:
embed = discord.Embed(
title="Refractor Tier Up!",
description=(
f"**{player_name}** reached **Tier {new_tier} ({tier_name})** on the **{track_name}** track"
),
color=color,
)
if image_url:
embed.set_image(url=image_url)
embed.set_footer(text=FOOTER_TEXT)
return embed
async def notify_tier_completion(
channel: discord.abc.Messageable, tier_up: dict, image_url: str | None = None
) -> None:
"""Send a tier-up notification embed to the given channel.
Non-fatal: any exception during send is caught and logged so that a
Discord API failure never interrupts game evaluation.
Parameters
----------
channel:
A discord.abc.Messageable (e.g. discord.TextChannel).
tier_up:
Dict with keys: player_name, old_tier, new_tier, current_value, track_name.
image_url:
Optional S3 URL for the refractor card image. Passed through to
build_tier_up_embed so the card art appears in the notification.
"""
try:
embed = build_tier_up_embed(tier_up, image_url=image_url)
await channel.send(embed=embed)
except Exception as exc:
logger.error(
"Failed to send tier-up notification for %s (tier %s): %s",
tier_up.get("player_name", "unknown"),
tier_up.get("new_tier"),
exc,
)