fix bot manager after revert

This commit is contained in:
Logan Cusano
2025-07-14 22:09:21 -04:00
parent cd2ea546b8
commit abb2d2f042

View File

@@ -4,7 +4,7 @@ import os
from discord import VoiceClient, VoiceChannel, opus, Activity, ActivityType, Intents from discord import VoiceClient, VoiceChannel, opus, Activity, ActivityType, Intents
from discord.ext import commands from discord.ext import commands
from typing import Optional, Dict from typing import Optional, Dict
from internal.NoiseGatev2 import AudioTransmitter from internal.NoiseGatev2 import AudioStreamManager, NoiseGateSource
from internal.logger import create_logger from internal.logger import create_logger
LOGGER = create_logger(__name__) LOGGER = create_logger(__name__)
@@ -105,40 +105,23 @@ class DiscordBotManager:
try: try:
self._voice_ready_event.clear() self._voice_ready_event.clear()
voice_client = await channel.connect(timeout=60.0, reconnect=True) voice_client = await channel.connect(timeout=60.0, reconnect=True)
LOGGER.debug("Voice client connecting...")
await asyncio.wait_for(self._voice_ready_event.wait(), timeout=15.0) await asyncio.wait_for(self._voice_ready_event.wait(), timeout=15.0)
LOGGER.info("Bot voice connection is ready.")
LOGGER.info(f"Checking opus status before transmitter init: {opus.is_loaded()}") # Create a single audio manager for this connection
audio_manager = AudioStreamManager(input_device_index=device_id)
try: # Create the noise-gated audio source
encoder = opus.OpusEncoder() audio_source = NoiseGateSource(audio_manager.get_stream(), threshold=ng_threshold)
LOGGER.info(f"Manually created OpusEncoder successfully: {encoder}")
except Exception as e:
LOGGER.error(f"MANUAL ENCODER CREATION FAILED: {e}", exc_info=True)
# Create and start the new AudioTransmitter # Play the source
transmitter = AudioTransmitter( voice_client.play(audio_source, after=lambda e: print(f'Player error: {e}') if e else None)
voice_client=voice_client,
noise_gate_threshold=ng_threshold,
loop=self.loop,
input_device_index=device_id
)
# Start the transmitter's main loop as a background task
transmitter_task = self.loop.create_task(transmitter.start())
self.voice_connections[guild_id] = { self.voice_connections[guild_id] = {
"client": voice_client, "client": voice_client,
"transmitter": transmitter, "audio_manager": audio_manager
"task": transmitter_task
} }
LOGGER.info(f"Joined guild {guild_id} and audio transmitter is running.") LOGGER.info(f"Joined guild {guild_id} and started audio stream.")
except asyncio.TimeoutError:
LOGGER.error(f"Timeout waiting for bot to join voice channel {channel_id}.")
raise RuntimeError("Bot failed to confirm voice connection within timeout.")
except Exception as e: except Exception as e:
LOGGER.error(f"Failed to connect to voice channel: {e}", exc_info=True) LOGGER.error(f"Failed to connect to voice channel: {e}", exc_info=True)
raise raise
@@ -149,19 +132,16 @@ class DiscordBotManager:
connection_info = self.voice_connections.get(guild_id) connection_info = self.voice_connections.get(guild_id)
if not connection_info: raise RuntimeError("Not connected to the specified guild's voice channel.") if not connection_info: raise RuntimeError("Not connected to the specified guild's voice channel.")
# Stop the transmitter task and clean up its resources
transmitter = connection_info.get("transmitter")
task = connection_info.get("task")
if transmitter:
LOGGER.info(f"Stopping audio transmitter for guild {guild_id}.")
await transmitter.stop()
if task and not task.done():
task.cancel()
voice_client = connection_info.get("client") voice_client = connection_info.get("client")
if voice_client and voice_client.is_connected(): if voice_client and voice_client.is_connected():
voice_client.stop()
await voice_client.disconnect() await voice_client.disconnect()
# Terminate the audio manager to release PyAudio resources
audio_manager = connection_info.get("audio_manager")
if audio_manager:
audio_manager.terminate()
del self.voice_connections[guild_id] del self.voice_connections[guild_id]
LOGGER.info(f"Left guild {guild_id} voice channel.") LOGGER.info(f"Left guild {guild_id} voice channel.")