From 47180d6ffe94ca97576476d7b635f393de0293e0 Mon Sep 17 00:00:00 2001 From: Samlon Date: Thu, 21 Aug 2025 12:21:53 +0200 Subject: play.py rework --- cogs/music/play.py | 89 +++++++++++------------------------------------------- 1 file changed, 18 insertions(+), 71 deletions(-) (limited to 'cogs/music/play.py') diff --git a/cogs/music/play.py b/cogs/music/play.py index f18afa8..7066ae8 100644 --- a/cogs/music/play.py +++ b/cogs/music/play.py @@ -1,83 +1,30 @@ -import logging import discord -import yt_dlp +from . import play_utils from discord.ext import commands -from youtubesearchpython import VideosSearch +import logging logger = logging.getLogger(__name__) -# Suppress noise about console usage from yt-dlp -yt_dlp.utils.bug_reports_message = lambda: '' - - class Play(commands.Cog): def __init__(self, bot): self.bot = bot - self.ydl_opts = { - 'extract_audio': True, - 'noplaylist': True, - 'quiet': True, - 'default_search': 'auto', - } - - 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( - None, - lambda: ydl.extract_info(url, download=False) - ) - if 'entries' in info: - info = info['entries'][0] - url2 = info['url'] - - ffmpeg_options = { - 'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', - 'options': '-vn' - } - - source = discord.FFmpegPCMAudio(url2, **ffmpeg_options) - vc.play(source) - await ctx.followup.send(f"gra: {title}") - logger.info(f"Playing: {title}") - except Exception as e: - await ctx.followup.send(f"Wystąpił błąd podczas odtwarzania: {str(e)}") - logger.error(f"Error playing audio: {e}", exc_info=True) - - @commands.slash_command(name="grajcuj", description="grajcuj melodyje") - async def play(self, ctx, *, search: str): + @discord.slash_command(name="grajcuj", description="grajcujmy wszyscy!") + async def play(self, ctx: discord.ApplicationContext, query: str = discord.Option(str, "co zagrać??", required=True)): await ctx.defer() - - if not (ctx.author.voice and ctx.author.voice.channel): - await ctx.followup.send("nie jesteś w kanale głosowym") - logger.info("User is not in a voice channel") - return - - channel = ctx.author.voice.channel - vc = ctx.voice_client or await channel.connect() - - if vc.channel != channel: - await vc.move_to(channel) - - # Search for the video - videos_search = VideosSearch(search, limit=1) - result = videos_search.result() - - if not result['result']: - await ctx.followup.send("nie znaleziono wideo dla twojego wyszukiwania.", ephemeral=True) - logger.info(f"Could not find a video for search: {search}") - return - - video = result['result'][0] - title = video['title'] - url = video['link'] - - # Play the audio directly - if vc.is_playing() or vc.is_paused(): - vc.stop() - await self.play_audio(ctx, vc, url, title) - + url, title = await play_utils.PlayUtils(self.bot).get_url(ctx, query) + logger.info(f"Found video: {title}") + filename = await play_utils.PlayUtils(self.bot).download(ctx, url) + logger.info(f"Downloaded file: {filename}") + await play_utils.PlayUtils(self.bot).play(ctx, filename, title) + + @discord.slash_command(name="grajcuj_zkompresowane", description="grajcujmy wszyscy w wysokiej jakosci!") + async def play_compressed(self, ctx: discord.ApplicationContext, url: str = discord.Option(str, "co zagrać??", required=True)): + await ctx.defer() + url, title = await play_utils.PlayUtils(self.bot).get_url(ctx, url) + filename = await play_utils.PlayUtils(self.bot).download(ctx, url) + filename = await play_utils.PlayUtils(self.bot).compress(ctx, filename) + await play_utils.PlayUtils(self.bot).play(ctx, filename, title) def setup(bot): - bot.add_cog(Play(bot)) + bot.add_cog(Play(bot)) \ No newline at end of file -- cgit v1.2.3