Fix chart display by adding FinViz fallback

- Add has_chart_attachment parameter to create_stock_embed()
- Use attachment://chart.png only when chart file is attached
- Fall back to FinViz chart URL when Yahoo Finance chart fails
- Prevents broken image references in Discord embeds

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Michael Simard
2025-12-25 00:29:10 -06:00
parent 71e70b77b0
commit 6bbf389a60

21
bot.py
View File

@@ -258,8 +258,6 @@ class StockBot(commands.Bot):
await channel.send(f"Unable to retrieve data for ticker: {ticker}")
return
embed = self.create_stock_embed(stock_data)
# Generate chart using Yahoo Finance historical data
chart_file = None
candle_data = self.yahoo_api.get_candles(ticker, days=30)
@@ -272,6 +270,9 @@ class StockBot(commands.Bot):
if chart_buffer:
chart_file = discord.File(chart_buffer, filename="chart.png")
# Create embed with appropriate chart reference
embed = self.create_stock_embed(stock_data, has_chart_attachment=bool(chart_file))
# Send with chart attachment if available
if chart_file:
await channel.send(embed=embed, file=chart_file)
@@ -280,12 +281,13 @@ class StockBot(commands.Bot):
logger.info(f"Sent stock update for {ticker} to channel {channel_id}")
def create_stock_embed(self, stock_data: dict) -> discord.Embed:
def create_stock_embed(self, stock_data: dict, has_chart_attachment: bool = False) -> discord.Embed:
"""
Create a formatted Discord embed for stock price data.
Args:
stock_data: Dictionary containing stock information
has_chart_attachment: Whether a chart file will be attached
Returns:
Discord embed object
@@ -333,8 +335,12 @@ class StockBot(commands.Bot):
embed.add_field(name="Change", value=change_str, inline=True)
embed.add_field(name="Previous Close", value=f"${stock_data['previous_close']}", inline=True)
# Reference attached chart image
embed.set_image(url="attachment://chart.png")
# Set chart image (attached file or FinViz fallback)
if has_chart_attachment:
embed.set_image(url="attachment://chart.png")
else:
chart_url = f"https://finviz.com/chart.ashx?t={ticker}&ty=c&ta=1&p=d&s=l"
embed.set_image(url=chart_url)
market_status = "🟢 Market Open" if stock_data['market_open'] else "🔴 Market Closed"
embed.set_footer(text=f"{market_status}")
@@ -547,8 +553,6 @@ async def get_stock_price(ctx, ticker: str = None):
await ctx.send(f"Unable to retrieve data for ticker: {ticker}")
return
embed = bot.create_stock_embed(stock_data)
# Generate chart using Yahoo Finance historical data
chart_file = None
candle_data = bot.yahoo_api.get_candles(ticker, days=30)
@@ -561,6 +565,9 @@ async def get_stock_price(ctx, ticker: str = None):
if chart_buffer:
chart_file = discord.File(chart_buffer, filename="chart.png")
# Create embed with appropriate chart reference
embed = bot.create_stock_embed(stock_data, has_chart_attachment=bool(chart_file))
# Send with chart attachment if available
if chart_file:
await ctx.send(embed=embed, file=chart_file)