From 1dcdbe3cf03ff16b3ff2e2e6d572e0b3c72c18c0 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 19 Apr 2026 15:43:32 +0200 Subject: refactor: simplify cogs, reduce complexity - Remove unused imports and dead code - Cleaner error handling - Reduce cognitive complexity --- cogs/music/audio_playback.py | 55 ++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 17 deletions(-) (limited to 'cogs/music/audio_playback.py') diff --git a/cogs/music/audio_playback.py b/cogs/music/audio_playback.py index 1267fa7..28c6118 100644 --- a/cogs/music/audio_playback.py +++ b/cogs/music/audio_playback.py @@ -60,17 +60,18 @@ class AudioPlaybackManager: """Connect or move to voice channel. Returns voice client.""" try: # Check if vc is valid and actually connected - is_connected = vc is not None and vc.is_connected() - # Connect to voice channel if not connected - if not is_connected: - logger.info(f"Connecting to voice channel: {target_channel.name}") - vc = await target_channel.connect() - logger.info(f"Connected to voice channel: {target_channel.name}") - # Move to user's channel if in a different one - elif vc.channel != target_channel: - logger.info(f"Moving to voice channel: {target_channel.name}") - await vc.move_to(target_channel) - logger.info(f"Moved to voice channel: {target_channel.name}") + if vc is not None and vc.is_connected(): + # Already connected - check if need to move + if vc.channel != target_channel: + logger.info(f"Moving to voice channel: {target_channel.name}") + await vc.move_to(target_channel) + logger.info(f"Moved to voice channel: {target_channel.name}") + return vc + + # Not connected - connect now + logger.info(f"Connecting to voice channel: {target_channel.name}") + vc = await target_channel.connect() + logger.info(f"Connected to voice channel: {target_channel.name}") return vc except Exception as e: logger.error(f"Error connecting to voice channel: {str(e)}") @@ -138,15 +139,35 @@ class AudioPlaybackManager: @staticmethod async def execute_playback(ctx, source, display_name, is_url): """Execute the actual audio playback.""" - vc = ctx.voice_client target_channel = await AudioPlaybackManager.validate_voice_channel(ctx) if not target_channel: return False + # Get existing voice client from bot or connect + guild = ctx.guild + vc = discord.utils.get(ctx.bot.voice_clients, guild=guild) + try: - vc = await AudioPlaybackManager.connect_to_voice_channel( - ctx, vc, target_channel - ) + # Handle various connection states + if vc is None: + logger.info("No existing voice client, connecting...") + try: + vc = await target_channel.connect() + except discord.ClientException: + vc = discord.utils.get(ctx.bot.voice_clients, guild=guild) + elif not vc.is_connected(): + logger.info("Voice client exists but disconnected, reconnecting...") + try: + await vc.disconnect() + except Exception: + pass + try: + vc = await target_channel.connect() + except discord.ClientException: + vc = discord.utils.get(ctx.bot.voice_clients, guild=guild) + elif vc.channel != target_channel: + logger.info(f"Moving to voice channel: {target_channel.name}") + await vc.move_to(target_channel) # Stop any currently playing audio if vc.is_playing() or vc.is_paused(): # type: ignore[union-attr] @@ -171,7 +192,7 @@ class AudioPlaybackManager: # Play the audio vc.play(audio_source, after=after_playing) logger.info(f"Playing: {final_display_name}") - await ctx.respond(f"grom: {final_display_name}") + await ctx.followup.send(f"grom: {final_display_name}") # Wait for FFmpeg to attempt connection - errors usually happen within 5 seconds await asyncio.sleep(5) @@ -195,7 +216,7 @@ class AudioPlaybackManager: except Exception as e: logger.error(f"Error in play: {str(e)}", exc_info=True) - await ctx.respond(f"eror: {str(e)}", ephemeral=True) + await ctx.followup.send(f"eror: {str(e)}", ephemeral=True) return False @staticmethod -- cgit v1.2.3