fix voice activity
This commit is contained in:
@@ -105,23 +105,22 @@ class DiscordBotManager:
|
||||
if guild_id in self.voice_connections: raise RuntimeError("Already connected to this guild's voice channel.")
|
||||
|
||||
try:
|
||||
# 1. Connect to the channel first
|
||||
self._voice_ready_event.clear()
|
||||
voice_client = await channel.connect(timeout=60.0, reconnect=True)
|
||||
LOGGER.debug("Voice client connecting...")
|
||||
|
||||
# 2. Wait for the on_voice_state_update event to confirm readiness
|
||||
await asyncio.wait_for(self._voice_ready_event.wait(), timeout=15.0)
|
||||
LOGGER.info("Bot voice connection is ready.")
|
||||
|
||||
# 3. NOW, create and start the audio stream handler
|
||||
stream_handler = NoiseGate(
|
||||
_input_device_index=device_id,
|
||||
_voice_connection=voice_client,
|
||||
_noise_gate_threshold=ng_threshold)
|
||||
_noise_gate_threshold=ng_threshold,
|
||||
# **THE FIX**: Pass the event loop to the stream handler.
|
||||
loop=self.loop,
|
||||
_input_device_index=device_id
|
||||
)
|
||||
stream_handler.run()
|
||||
|
||||
# 4. Store both client and stream handler for proper management
|
||||
self.voice_connections[guild_id] = {
|
||||
"client": voice_client,
|
||||
"stream": stream_handler
|
||||
@@ -141,13 +140,11 @@ class DiscordBotManager:
|
||||
connection_info = self.voice_connections.get(guild_id)
|
||||
if not connection_info: raise RuntimeError("Not connected to the specified guild's voice channel.")
|
||||
|
||||
# Cleanly stop the associated audio stream first
|
||||
stream_handler = connection_info.get('stream')
|
||||
if stream_handler:
|
||||
LOGGER.info(f"Stopping audio stream for guild {guild_id}.")
|
||||
await stream_handler.close()
|
||||
|
||||
# Disconnect the voice client
|
||||
voice_client = connection_info.get('client')
|
||||
if voice_client and voice_client.is_connected():
|
||||
await voice_client.disconnect()
|
||||
|
||||
Reference in New Issue
Block a user