From 46ec27c3592e551c3248883fda8d7239be563e88 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sun, 27 Apr 2025 03:15:15 -0400 Subject: [PATCH] Update bot manager to respond to start bot when bot is ready --- app/internal/bot_manager.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/app/internal/bot_manager.py b/app/internal/bot_manager.py index 60fbe2e..5b598ce 100644 --- a/app/internal/bot_manager.py +++ b/app/internal/bot_manager.py @@ -36,6 +36,8 @@ class DiscordBotManager: @self.bot.event async def on_ready(): LOGGER.info(f'Logged in as {self.bot.user}') + # Set the event when on_ready is called + self._ready_event.set() @self.bot.event async def on_voice_state_update(member, before, after): @@ -49,13 +51,30 @@ class DiscordBotManager: LOGGER.warning(f"Error leaving voice channel: '{e}'") # Attempt to reconnect to the channel after a brief pause await asyncio.sleep(2) + # You might need to store the previous channel ID more robustly + # for reconnection attempts in a real application await self.join_voice_channel(guild_id, before.channel.id) - # Load Opus for the current CPU + + # Load Opus for the current CPU await self.load_opus() + # Create the task to run the bot in the background self.bot_task = self.loop.create_task(self.bot.start(token)) + # Wait for the on_ready event to be set by the bot task + LOGGER.info("Waiting for bot to become ready...") + try: + # Add a timeout for robustness in case on_ready never fires + await asyncio.wait_for(self._ready_event.wait(), timeout=60.0) # Adjust timeout as needed + LOGGER.info("Bot is ready, start_bot returning.") + except asyncio.TimeoutError: + LOGGER.error("Timeout waiting for bot to become ready. Bot might have failed to start.") + # Optionally cancel the bot task if it didn't become ready + if self.bot_task and not self.bot_task.done(): + self.bot_task.cancel() + raise RuntimeError("Bot failed to become ready within timeout.") + async def stop_bot(self): async with self.lock: if self.bot: