diff options
| author | salmonik <raisersoft@proton.me> | 2025-09-26 09:39:11 +0200 |
|---|---|---|
| committer | salmonik <raisersoft@proton.me> | 2025-09-26 09:39:11 +0200 |
| commit | 87a55bf43e61af57c6c88f1be57e2e0f2036d869 (patch) | |
| tree | a8eee32d1f94ca1c5c40bcd4b9b570eeb4bb0f79 /cogs/music/play.py | |
| parent | add requirements.txt (diff) | |
| download | senator-sperma-87a55bf43e61af57c6c88f1be57e2e0f2036d869.tar.gz senator-sperma-87a55bf43e61af57c6c88f1be57e2e0f2036d869.zip | |
add local music playing
Diffstat (limited to '')
| -rw-r--r-- | cogs/music/play.py | 44 |
1 files changed, 37 insertions, 7 deletions
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 @@ | |||
| 1 | import os | ||
| 2 | |||
| 1 | import discord | 3 | import discord |
| 4 | import mutagen | ||
| 5 | |||
| 2 | from . import play_utils | 6 | from . import play_utils |
| 3 | from discord.ext import commands | 7 | from discord.ext import commands |
| 4 | import logging | 8 | import logging |
| @@ -10,13 +14,39 @@ class Play(commands.Cog): | |||
| 10 | self.bot = bot | 14 | self.bot = bot |
| 11 | 15 | ||
| 12 | @discord.slash_command(name="grajcuj", description="grajcujmy wszyscy!") | 16 | @discord.slash_command(name="grajcuj", description="grajcujmy wszyscy!") |
| 13 | async def play(self, ctx: discord.ApplicationContext, query: str = discord.Option(str, "co zagrać??", required=True)): | 17 | async def play( |
| 14 | await ctx.defer() | 18 | self, |
| 15 | url, title = await play_utils.PlayUtils(self.bot).get_url(ctx, query) | 19 | ctx: discord.ApplicationContext, |
| 16 | logger.info(f"Found video: {title}") | 20 | query: str = discord.Option(str, "co zagrać??", required=True), |
| 17 | filename = await play_utils.PlayUtils(self.bot).download(ctx, url) | 21 | source: str = discord.Option(str, "co zagrać??", required=True, choices=["local", "soundcloud", "youtube"]) |
| 18 | logger.info(f"Downloaded file: {filename}") | 22 | |
| 19 | await play_utils.PlayUtils(self.bot).play(ctx, filename, title) | 23 | ): |
| 24 | await ctx.defer(ephemeral=True) | ||
| 25 | if source == "local": | ||
| 26 | candidates = await play_utils.PlayUtils(self.bot).search_local(query) | ||
| 27 | if candidates: | ||
| 28 | embed = discord.Embed(title="Wyniki wyszukiwania", description="Wybierz plik do odtworzenia:", color=discord.Color.blue()) | ||
| 29 | view = discord.ui.View(disable_on_timeout=True) | ||
| 30 | for i, candidate in enumerate(candidates, start=1): | ||
| 31 | filename = os.path.basename(candidate) | ||
| 32 | metadata = None | ||
| 33 | if os.path.splitext(filename)[1].lower() in ['.mp3', '.wav', '.opus']: | ||
| 34 | metadata = mutagen.File(candidate) | ||
| 35 | title = metadata.get('title', None) | ||
| 36 | if metadata and title: | ||
| 37 | embed.add_field(name=f"{i}. {title}", value="\u200b", inline=False) | ||
| 38 | else: | ||
| 39 | embed.add_field(name=f"{i}. {filename}", value="\u200b", inline=False) | ||
| 40 | button = discord.ui.Button(label=f"Play {i}") | ||
| 41 | async def button_callback(interaction: discord.Interaction, candidate=candidate): | ||
| 42 | await play_utils.PlayUtils(self.bot).play(ctx, candidate) | ||
| 43 | button.callback = button_callback | ||
| 44 | view.add_item(button) | ||
| 45 | await ctx.respond(embed=embed, view=view, ephemeral=True) | ||
| 46 | else: | ||
| 47 | await ctx.respond("Nie znaleziono pliku do odtworzenia", ephemeral=True) | ||
| 48 | |||
| 49 | |||
| 20 | 50 | ||
| 21 | @discord.slash_command(name="grajcuj_zkompresowane", description="grajcujmy wszyscy w wysokiej jakosci!") | 51 | @discord.slash_command(name="grajcuj_zkompresowane", description="grajcujmy wszyscy w wysokiej jakosci!") |
| 22 | async def play_compressed(self, ctx: discord.ApplicationContext, url: str = discord.Option(str, "co zagrać??", required=True)): | 52 | async def play_compressed(self, ctx: discord.ApplicationContext, url: str = discord.Option(str, "co zagrać??", required=True)): |
