From 2339a21639e79ba7f6ecb334d65d1857b2490733 Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 8 Apr 2026 16:21:49 +0200 Subject: "fix" voice management --- cogs/vclogs.py | 26 +++++++++++++++++++++----- cogs/voice.py | 15 +++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) (limited to 'cogs') diff --git a/cogs/vclogs.py b/cogs/vclogs.py index 0d80ae7..477aa1c 100644 --- a/cogs/vclogs.py +++ b/cogs/vclogs.py @@ -78,15 +78,31 @@ class VcLogs(commands.Cog): logger.info("saving file") tts.save(tts_file) - voice_client = await voice_mgr.get_voice_client(channel.guild) - volume = get_volume() - - if voice_client is None: + # Get voice status using our new method + voice_status = await voice_mgr.get_voice_status(channel.guild) + voice_client = voice_status[0] if voice_status else None + is_connected = voice_status[1] if voice_status else False + is_playing = voice_status[2] if voice_status else False + + # Ensure we have a connected voice client + if not is_connected: voice_client = await voice_mgr.join_channel(channel) + # Update status after joining + voice_status = await voice_mgr.get_voice_status(channel.guild) + voice_client = voice_status[0] if voice_status else None + is_connected = voice_status[1] if voice_status else False + is_playing = voice_status[2] if voice_status else False + + if not is_connected or voice_client is None: + logger.error("Failed to join voice channel") + if os.path.exists(tts_file): + os.remove(tts_file) + return + volume = get_volume() logger.info(f"connected: {voice_client}") - if voice_client and not voice_client.is_playing(): + if voice_client and not is_playing: logger.info("preparing to play audio") audio_source = discord.FFmpegPCMAudio(tts_file, options=f'-filter:a "volume={volume}"') voice_client.play(audio_source) diff --git a/cogs/voice.py b/cogs/voice.py index 8ee3699..316de7b 100644 --- a/cogs/voice.py +++ b/cogs/voice.py @@ -69,6 +69,21 @@ class VoiceManager: """Get existing voice client for a guild.""" return discord.utils.get(self.bot.voice_clients, guild=guild) + async def get_voice_status(self, guild): + """Get voice client status including connection and playback state. + + Returns: + tuple: (voice_client, is_connected, is_playing) or (None, False, False) if no client + """ + voice_client = await self.get_voice_client(guild) + if voice_client is None: + return None, False, False + + is_connected = voice_client.is_connected() + is_playing = voice_client.is_playing() if is_connected else False + + return voice_client, is_connected, is_playing + async def play_audio(self, channel, audio_source, volume=1.0): """Play audio in a voice channel.""" voice_client = await self.join_channel(channel) -- cgit v1.2.3