Update bot manager to respond to start bot when bot is ready
This commit is contained in:
@@ -36,6 +36,8 @@ class DiscordBotManager:
|
|||||||
@self.bot.event
|
@self.bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
LOGGER.info(f'Logged in as {self.bot.user}')
|
LOGGER.info(f'Logged in as {self.bot.user}')
|
||||||
|
# Set the event when on_ready is called
|
||||||
|
self._ready_event.set()
|
||||||
|
|
||||||
@self.bot.event
|
@self.bot.event
|
||||||
async def on_voice_state_update(member, before, after):
|
async def on_voice_state_update(member, before, after):
|
||||||
@@ -49,13 +51,30 @@ class DiscordBotManager:
|
|||||||
LOGGER.warning(f"Error leaving voice channel: '{e}'")
|
LOGGER.warning(f"Error leaving voice channel: '{e}'")
|
||||||
# Attempt to reconnect to the channel after a brief pause
|
# Attempt to reconnect to the channel after a brief pause
|
||||||
await asyncio.sleep(2)
|
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)
|
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()
|
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))
|
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 def stop_bot(self):
|
||||||
async with self.lock:
|
async with self.lock:
|
||||||
if self.bot:
|
if self.bot:
|
||||||
|
|||||||
Reference in New Issue
Block a user