Add optional startup announcement feature

Bot can now send a one-time announcement to all configured channels when it starts.

Configuration:
- Set STARTUP_ANNOUNCEMENT in .env with your message
- Leave empty to disable announcements
- Prevents duplicate announcements on reconnects

Usage example:
STARTUP_ANNOUNCEMENT=Bot updated! New features: colored prices and charts

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Michael Simard
2025-12-03 22:32:32 -06:00
parent 76516a4e73
commit 388430780d
4 changed files with 31 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
# Discord Configuration
DISCORD_TOKEN=your_discord_bot_token_here
CHANNEL_ID=your_channel_id_here,optional_second_channel_id
STARTUP_ANNOUNCEMENT=
# Bot Configuration
COMMAND_PREFIX=!

View File

@@ -160,6 +160,7 @@ Environment variables in `.env`:
|----------|-------------|---------|
| `DISCORD_TOKEN` | Your Discord bot token | Required |
| `CHANNEL_ID` | Discord channel ID(s) for updates (comma-separated for multiple) | Required |
| `STARTUP_ANNOUNCEMENT` | Optional message to send when bot starts (empty = no announcement) | `""` |
| `COMMAND_PREFIX` | Command prefix for bot | `!` |
| `PRIMARY_TICKER` | Stock ticker for hourly updates | `PYPL` |
| `UPDATE_INTERVAL_HOURS` | Hours between updates | `1` |
@@ -169,6 +170,11 @@ Environment variables in `.env`:
CHANNEL_ID=1442203998932304035,9876543210123456789
```
**Startup Announcements:** Send a one-time message when the bot starts (useful for announcing updates):
```
STARTUP_ANNOUNCEMENT=Bot updated with new features: colored price changes and FinViz charts!
```
## Switching Stock API Providers
The bot is designed with an abstract API layer. To switch providers:

23
bot.py
View File

@@ -38,6 +38,7 @@ class StockBot(commands.Bot):
self.scheduler = AsyncIOScheduler(timezone=pytz.timezone('America/New_York'))
self.target_channel_ids = [int(id) for id in Config.CHANNEL_IDS]
self.primary_ticker = Config.PRIMARY_TICKER
self.startup_announced = False
async def setup_hook(self):
"""Initialize the bot and set up scheduled tasks."""
@@ -77,6 +78,11 @@ class StockBot(commands.Bot):
else:
logger.error(f'Could not find channel with ID {channel_id}')
# Send startup announcement if configured and not already sent
if Config.STARTUP_ANNOUNCEMENT and not self.startup_announced:
await self.send_startup_announcement()
self.startup_announced = True
async def send_hourly_update(self):
"""Send hourly stock price update if market is open."""
if not MarketHours.is_market_open():
@@ -97,6 +103,23 @@ class StockBot(commands.Bot):
for channel_id in self.target_channel_ids:
await self.send_stock_update(self.primary_ticker, channel_id)
async def send_startup_announcement(self):
"""Send startup announcement to configured channels."""
embed = discord.Embed(
title="🤖 Bot Update",
description=Config.STARTUP_ANNOUNCEMENT,
color=discord.Color.blue(),
timestamp=datetime.now(pytz.timezone('America/New_York'))
)
for channel_id in self.target_channel_ids:
channel = self.get_channel(channel_id)
if channel:
await channel.send(embed=embed)
logger.info(f"Sent startup announcement to channel {channel_id}")
else:
logger.error(f"Could not send announcement to channel {channel_id}")
async def send_stock_update(self, ticker: str, channel_id: int):
"""
Fetch stock data and send formatted embed to specified channel.

View File

@@ -12,6 +12,7 @@ class Config:
CHANNEL_ID = os.getenv('CHANNEL_ID')
CHANNEL_IDS = [id.strip() for id in os.getenv('CHANNEL_ID', '').split(',') if id.strip()]
COMMAND_PREFIX = os.getenv('COMMAND_PREFIX', '!')
STARTUP_ANNOUNCEMENT = os.getenv('STARTUP_ANNOUNCEMENT', '')
# Stock configuration
PRIMARY_TICKER = os.getenv('PRIMARY_TICKER', 'PYPL')