From 388430780dced5ad446e8baf83040429814e86f5 Mon Sep 17 00:00:00 2001 From: Michael Simard Date: Wed, 3 Dec 2025 22:32:32 -0600 Subject: [PATCH] Add optional startup announcement feature MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .env.example | 1 + README.md | 6 ++++++ bot.py | 23 +++++++++++++++++++++++ config.py | 1 + 4 files changed, 31 insertions(+) diff --git a/.env.example b/.env.example index c0043ca..8e48bea 100644 --- a/.env.example +++ b/.env.example @@ -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=! diff --git a/README.md b/README.md index 2fb9f9f..4d0ab81 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/bot.py b/bot.py index 6b27daf..3854c00 100644 --- a/bot.py +++ b/bot.py @@ -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. diff --git a/config.py b/config.py index 004fe1a..37e7400 100644 --- a/config.py +++ b/config.py @@ -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')