From 4663f260b55de0975661fda1c00f60d2ca6e4506 Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 20 May 2026 15:40:18 +0200 Subject: Clean up consumers: replace polling with VoiceManager.play_source() - cron.py: replace while-is_playing polling with VoiceManager.play_source() - vclogs.py: replace while-is_playing polling with VoiceManager.play_source() - stop.py: use VoiceManager.disconnect() instead of raw voice_client - join.py: use VoiceManager.connect() instead of raw channel.connect() - voice.py: add back join_by_channel_id() convenience method - Tests: update for new VoiceManager method names and signatures --- cogs/cron.py | 26 ++++++++++---------------- cogs/music/join.py | 4 +++- cogs/music/stop.py | 5 ++++- cogs/vclogs.py | 7 +------ cogs/voice.py | 11 +++++++++++ 5 files changed, 29 insertions(+), 24 deletions(-) (limited to 'cogs') diff --git a/cogs/cron.py b/cogs/cron.py index aad43d2..ee88085 100644 --- a/cogs/cron.py +++ b/cogs/cron.py @@ -145,29 +145,23 @@ class Cron(commands.Cog): if isinstance(channel, discord.VoiceChannel): voice_mgr = get_voice_manager(self.bot) - voice_client = await voice_mgr.join_channel(channel) - if not voice_client: - if channel.guild.system_channel: - await channel.guild.system_channel.send( - "Nie udano się połączyć z kanałem głosowym" - ) - return try: audio_source = discord.FFmpegPCMAudio(audio_url) - voice_client.play(audio_source) + success = await voice_mgr.play_source(channel, audio_source) - while voice_client.is_playing(): - await asyncio.sleep(1) + if success: + logger.info(f"Finished playing audio: {audio_url}") + else: + raise Exception("Playback failed") - logger.info("Audio playback finished normally - disconnecting") - await voice_client.disconnect() - logger.info(f"Finished playing audio: {audio_url}") + await voice_mgr.disconnect(channel.guild) except Exception as e: logger.error(f"Error playing audio: {e}") - if voice_client.is_connected(): - logger.info("Audio playback failed - disconnecting") - await voice_client.disconnect() + try: + await voice_mgr.disconnect(channel.guild) + except Exception: + pass if channel.guild.system_channel: await channel.guild.system_channel.send( f"Błąd podczas odgrywania audio: {audio_url}" diff --git a/cogs/music/join.py b/cogs/music/join.py index c613b10..d68ca79 100644 --- a/cogs/music/join.py +++ b/cogs/music/join.py @@ -6,6 +6,7 @@ import logging from discord.ext import commands +from cogs.voice import get_voice_manager logger = logging.getLogger(__name__) @@ -16,8 +17,9 @@ class Join(commands.Cog): @commands.slash_command(name="join", description="dołącza do kanalu") async def join(self, ctx): + voice_mgr = get_voice_manager(self.bot) channel = ctx.author.voice.channel - await channel.connect() + await voice_mgr.connect(channel) await ctx.respond(content="\u200b", ephemeral=True) await ctx.interaction.delete_original_response() logger.info("Bot connected to voice channel") diff --git a/cogs/music/stop.py b/cogs/music/stop.py index f06e7f5..9c8d5cf 100644 --- a/cogs/music/stop.py +++ b/cogs/music/stop.py @@ -5,6 +5,8 @@ from discord.ext import commands +from cogs.voice import get_voice_manager + import logging logger = logging.getLogger(__name__) @@ -18,7 +20,8 @@ class Stop(commands.Cog): async def stop(self, ctx): await ctx.respond(content="\u200b", ephemeral=True) await ctx.interaction.delete_original_response() - await ctx.voice_client.disconnect() + voice_mgr = get_voice_manager(self.bot) + await voice_mgr.disconnect(ctx.guild) logger.info("Bot disconnected from voice channel") diff --git a/cogs/vclogs.py b/cogs/vclogs.py index b538265..2e853ad 100644 --- a/cogs/vclogs.py +++ b/cogs/vclogs.py @@ -160,12 +160,7 @@ class VcLogs(commands.Cog): logger.info("preparing to play audio") audio_source = discord.FFmpegPCMAudio(tts_file) - if volume != 1.0: - audio_source = discord.PCMVolumeTransformer(audio_source) - audio_source.volume = volume - voice_client.play(audio_source) - while voice_client.is_playing(): - await asyncio.sleep(0.1) + await voice_mgr.play_source(channel, audio_source, volume=volume) except Exception as e: logger.error(f"TTS playback error: {e}") finally: diff --git a/cogs/voice.py b/cogs/voice.py index ba1993e..86416ef 100644 --- a/cogs/voice.py +++ b/cogs/voice.py @@ -151,6 +151,17 @@ class VoiceManager: """Backward-compatible alias for connect().""" return await self.connect(channel) + async def join_by_channel_id(self, channel_id): + """Join a voice channel by channel ID.""" + channel = self.bot.get_channel(channel_id) + if not channel: + logger.error(f"Channel {channel_id} not found") + return None + if not isinstance(channel, discord.VoiceChannel): + logger.error(f"Channel {channel_id} is not a voice channel") + return None + return await self.connect(channel) + async def get_client(self, guild): """Get existing voice client for a guild.""" return discord.utils.get(self.bot.voice_clients, guild=guild) -- cgit v1.2.3