From 507f03e716478fce3ee9b9c3f3f5964c34b5b2dd Mon Sep 17 00:00:00 2001 From: Samlon Date: Fri, 25 Jul 2025 19:36:56 +0200 Subject: play.py fix --- cogs/music/play.py | 84 +++++++++++------------------------------------------- 1 file changed, 16 insertions(+), 68 deletions(-) (limited to 'cogs/music/play.py') diff --git a/cogs/music/play.py b/cogs/music/play.py index b47de03..5d82127 100644 --- a/cogs/music/play.py +++ b/cogs/music/play.py @@ -12,7 +12,6 @@ yt_dlp.utils.bug_reports_message = lambda: '' class Play(commands.Cog): def __init__(self, bot): self.bot = bot - self.queues = {} self.ydl_opts = { 'format': 'bestaudio/best', 'postprocessors': [{ @@ -27,34 +26,7 @@ class Play(commands.Cog): 'source_address': '0.0.0.0', } - def get_queue(self, guild_id): - return self.queues.setdefault(guild_id, []) - - async def play_next(self, ctx, vc, guild_id): - queue = self.get_queue(guild_id) - if queue: - title, url = queue[0] - - def after_playing(): - if queue: - queue.pop(0) - if queue: - next_title, next_url = queue[0] - if not vc.is_playing() and not vc.is_paused(): - asyncio.run_coroutine_threadsafe( - self.play_audio(ctx, vc, next_url, next_title), - self.bot.loop - ) - else: - asyncio.run_coroutine_threadsafe( - vc.disconnect(), - self.bot.loop - ) - - if not vc.is_playing() and not vc.is_paused(): - await self.play_audio(ctx, vc, url, title, after=after_playing) - - async def play_audio(self, ctx, vc, url, title, after=None): + async def play_audio(self, ctx, vc, url, title): try: with yt_dlp.YoutubeDL(self.ydl_opts) as ydl: info = await self.bot.loop.run_in_executor( @@ -71,14 +43,21 @@ class Play(commands.Cog): 'options': '-vn' } + def after_playing(error): + if error: + print(f'Player error: {error}') + coro = vc.disconnect() + fut = asyncio.run_coroutine_threadsafe(coro, self.bot.loop) + try: + fut.result() + except Exception as e: + print(f'Error disconnecting: {e}') + source = discord.FFmpegPCMAudio(url2, **ffmpeg_options) - vc.play(source, after=after) + vc.play(source, after=after_playing) await ctx.followup.send(f"gra: {title}") except Exception as e: await ctx.followup.send(f"Wystąpił błąd podczas odtwarzania: {str(e)}") - # Try to play next in the queue if there's an error - if after: - after(None) @commands.slash_command(name="grajcuj", description="grajcuj melodyje") async def play(self, ctx, *, search: str): @@ -106,41 +85,10 @@ class Play(commands.Cog): title = video['title'] url = video['link'] - # Add to queue - queue = self.get_queue(ctx.guild.id) - queue.append((title, url)) - - # If nothing is playing, start playing - if not vc.is_playing() and not vc.is_paused(): - await self.play_next(ctx, vc, ctx.guild.id) - else: - await ctx.followup.send(f"dodano do kolejki: {title}") - - @commands.slash_command(name="stop", description="zatrzymaj odtwarzanie i wyczyść kolejkę") - async def stop(self, ctx): - if ctx.voice_client: - queue = self.get_queue(ctx.guild.id) - queue.clear() - await ctx.voice_client.disconnect() - await ctx.respond("Zatrzymano odtwarzanie i wyczyszczono kolejkę") - else: - await ctx.respond("Nie jestem połączony z kanałem głosowym") - - @commands.slash_command(name="pauza", description="wstrzymaj odtwarzanie") - async def pause(self, ctx): - if ctx.voice_client and ctx.voice_client.is_playing(): - ctx.voice_client.pause() - await ctx.respond("Wstrzymano odtwarzanie") - else: - await ctx.respond("Nic nie jest teraz odtwarzane") - - @commands.slash_command(name="wznow", description="wznów odtwarzanie") - async def resume(self, ctx): - if ctx.voice_client and ctx.voice_client.is_paused(): - ctx.voice_client.resume() - await ctx.respond("Wznowiono odtwarzanie") - else: - await ctx.respond("Nic nie jest teraz wstrzymane") + # Play the audio directly + if vc.is_playing() or vc.is_paused(): + vc.stop() + await self.play_audio(ctx, vc, url, title) def setup(bot): -- cgit v1.2.3