From 87a55bf43e61af57c6c88f1be57e2e0f2036d869 Mon Sep 17 00:00:00 2001 From: salmonik Date: Fri, 26 Sep 2025 09:39:11 +0200 Subject: add local music playing --- cogs/music/play.py | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) (limited to 'cogs/music/play.py') diff --git a/cogs/music/play.py b/cogs/music/play.py index 7066ae8..b7d409d 100644 --- a/cogs/music/play.py +++ b/cogs/music/play.py @@ -1,4 +1,8 @@ +import os + import discord +import mutagen + from . import play_utils from discord.ext import commands import logging @@ -10,13 +14,39 @@ class Play(commands.Cog): self.bot = bot @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() - 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) + async def play( + self, + ctx: discord.ApplicationContext, + query: str = discord.Option(str, "co zagrać??", required=True), + source: str = discord.Option(str, "co zagrać??", required=True, choices=["local", "soundcloud", "youtube"]) + + ): + await ctx.defer(ephemeral=True) + if source == "local": + candidates = await play_utils.PlayUtils(self.bot).search_local(query) + if candidates: + embed = discord.Embed(title="Wyniki wyszukiwania", description="Wybierz plik do odtworzenia:", color=discord.Color.blue()) + view = discord.ui.View(disable_on_timeout=True) + for i, candidate in enumerate(candidates, start=1): + filename = os.path.basename(candidate) + metadata = None + if os.path.splitext(filename)[1].lower() in ['.mp3', '.wav', '.opus']: + metadata = mutagen.File(candidate) + title = metadata.get('title', None) + if metadata and title: + embed.add_field(name=f"{i}. {title}", value="\u200b", inline=False) + else: + embed.add_field(name=f"{i}. {filename}", value="\u200b", inline=False) + button = discord.ui.Button(label=f"Play {i}") + async def button_callback(interaction: discord.Interaction, candidate=candidate): + await play_utils.PlayUtils(self.bot).play(ctx, candidate) + button.callback = button_callback + view.add_item(button) + await ctx.respond(embed=embed, view=view, ephemeral=True) + else: + await ctx.respond("Nie znaleziono pliku do odtworzenia", ephemeral=True) + + @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)): -- cgit v1.2.3