Attempt to fix voice_client

This commit is contained in:
Logan Cusano
2025-08-02 00:05:53 -04:00
parent 03eaf6887e
commit 554df45826

View File

@@ -69,26 +69,34 @@ class VoiceCog(commands.Cog):
return False
# Get voice client for this guild
voice_client = discord.utils.get(self.bot.voice_clients, guild=guild)
current_voice_client = discord.utils.get(self.bot.voice_clients, guild=guild)
if voice_client:
if voice_client.channel == channel:
if current_voice_client:
if current_voice_client.channel == channel:
print(f"Already in channel: {channel.name} in guild {guild.name}.")
voice_client_to_use = current_voice_client # Use existing
else:
await voice_client.move_to(channel)
await current_voice_client.move_to(channel)
print(f"Moved to channel: {channel.name} in guild {guild.name}.")
voice_client_to_use = current_voice_client # Use moved existing
else:
try:
voice_client = await channel.connect(timeout=60.0, reconnect=True)
voice_client_to_use = await channel.connect(timeout=60.0, reconnect=True)
print(f"Connected to channel: {channel.name} in guild {guild.name}.")
except Exception as e:
print(f"Failed to connect to {channel.name} in guild {guild.name}. Error: {e}")
return False
# --- IMPORTANT: Validate voice_client_to_use BEFORE passing to NoiseGate ---
if not voice_client_to_use:
print(f"Failed to obtain a valid VoiceClient object for guild {guild.name}.")
return False
if discord.opus.is_loaded():
# Create and start the NoiseGate audio stream for this server
# Ensure the correct voice client is passed
stream_handler = NoiseGate(
_voice_connection=voice_client,
_voice_connection=voice_client_to_use, # Corrected: use the unified variable
_noise_gate_threshold=self.ng_threshold,
_input_device_index=self.device_id
)
@@ -98,7 +106,9 @@ class VoiceCog(commands.Cog):
return True
else:
print("Opus library not loaded. Cannot start audio stream.")
await voice_client.disconnect() # Disconnect if opus isn't loaded
# Disconnect if opus isn't loaded but a connection was made
if voice_client_to_use:
await voice_client_to_use.disconnect()
return False
# Internal API method to leave a voice channel
@@ -155,7 +165,6 @@ class VoiceCog(commands.Cog):
if not discord.opus.is_loaded():
await ctx.send("Opus audio library is not loaded. I cannot join a voice channel.")
raise commands.CommandError("Opus not loaded.")
# --- Discord Bot Manager Class ---
class DiscordBotManager:
def __init__(self, device_id: int = 0, ng_threshold: int = 50):