diff options
| author | Samlon <stankiewiczgracjan2019@gmail.com> | 2025-08-21 12:21:53 +0200 |
|---|---|---|
| committer | Samlon <stankiewiczgracjan2019@gmail.com> | 2025-08-21 12:21:53 +0200 |
| commit | 47180d6ffe94ca97576476d7b635f393de0293e0 (patch) | |
| tree | e2a835b806c5a46034464974c647391ea02d4f51 /cogs/music/play.py | |
| parent | play.py fixing (diff) | |
| download | senator-sperma-47180d6ffe94ca97576476d7b635f393de0293e0.tar.gz senator-sperma-47180d6ffe94ca97576476d7b635f393de0293e0.zip | |
play.py rework
Diffstat (limited to '')
| -rw-r--r-- | cogs/music/play.py | 89 |
1 files changed, 18 insertions, 71 deletions
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 @@ | |||
| 1 | import logging | ||
| 2 | import discord | 1 | import discord |
| 3 | import yt_dlp | 2 | from . import play_utils |
| 4 | from discord.ext import commands | 3 | from discord.ext import commands |
| 5 | from youtubesearchpython import VideosSearch | 4 | import logging |
| 6 | 5 | ||
| 7 | logger = logging.getLogger(__name__) | 6 | logger = logging.getLogger(__name__) |
| 8 | 7 | ||
| 9 | # Suppress noise about console usage from yt-dlp | ||
| 10 | yt_dlp.utils.bug_reports_message = lambda: '' | ||
| 11 | |||
| 12 | |||
| 13 | class Play(commands.Cog): | 8 | class Play(commands.Cog): |
| 14 | def __init__(self, bot): | 9 | def __init__(self, bot): |
| 15 | self.bot = bot | 10 | self.bot = bot |
| 16 | self.ydl_opts = { | ||
| 17 | 'extract_audio': True, | ||
| 18 | 'noplaylist': True, | ||
| 19 | 'quiet': True, | ||
| 20 | 'default_search': 'auto', | ||
| 21 | } | ||
| 22 | |||
| 23 | async def play_audio(self, ctx, vc, url, title): | ||
| 24 | try: | ||
| 25 | with yt_dlp.YoutubeDL(self.ydl_opts) as ydl: | ||
| 26 | info = await self.bot.loop.run_in_executor( | ||
| 27 | None, | ||
| 28 | lambda: ydl.extract_info(url, download=False) | ||
| 29 | ) | ||
| 30 | if 'entries' in info: | ||
| 31 | info = info['entries'][0] | ||
| 32 | 11 | ||
| 33 | url2 = info['url'] | 12 | @discord.slash_command(name="grajcuj", description="grajcujmy wszyscy!") |
| 34 | 13 | async def play(self, ctx: discord.ApplicationContext, query: str = discord.Option(str, "co zagrać??", required=True)): | |
| 35 | ffmpeg_options = { | ||
| 36 | 'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', | ||
| 37 | 'options': '-vn' | ||
| 38 | } | ||
| 39 | |||
| 40 | source = discord.FFmpegPCMAudio(url2, **ffmpeg_options) | ||
| 41 | vc.play(source) | ||
| 42 | await ctx.followup.send(f"gra: {title}") | ||
| 43 | logger.info(f"Playing: {title}") | ||
| 44 | except Exception as e: | ||
| 45 | await ctx.followup.send(f"Wystąpił błąd podczas odtwarzania: {str(e)}") | ||
| 46 | logger.error(f"Error playing audio: {e}", exc_info=True) | ||
| 47 | |||
| 48 | @commands.slash_command(name="grajcuj", description="grajcuj melodyje") | ||
| 49 | async def play(self, ctx, *, search: str): | ||
| 50 | await ctx.defer() | 14 | await ctx.defer() |
| 51 | 15 | url, title = await play_utils.PlayUtils(self.bot).get_url(ctx, query) | |
| 52 | if not (ctx.author.voice and ctx.author.voice.channel): | 16 | logger.info(f"Found video: {title}") |
| 53 | await ctx.followup.send("nie jesteś w kanale głosowym") | 17 | filename = await play_utils.PlayUtils(self.bot).download(ctx, url) |
| 54 | logger.info("User is not in a voice channel") | 18 | logger.info(f"Downloaded file: {filename}") |
| 55 | return | 19 | await play_utils.PlayUtils(self.bot).play(ctx, filename, title) |
| 56 | 20 | ||
| 57 | channel = ctx.author.voice.channel | 21 | @discord.slash_command(name="grajcuj_zkompresowane", description="grajcujmy wszyscy w wysokiej jakosci!") |
| 58 | vc = ctx.voice_client or await channel.connect() | 22 | async def play_compressed(self, ctx: discord.ApplicationContext, url: str = discord.Option(str, "co zagrać??", required=True)): |
| 59 | 23 | await ctx.defer() | |
| 60 | if vc.channel != channel: | 24 | url, title = await play_utils.PlayUtils(self.bot).get_url(ctx, url) |
| 61 | await vc.move_to(channel) | 25 | filename = await play_utils.PlayUtils(self.bot).download(ctx, url) |
| 62 | 26 | filename = await play_utils.PlayUtils(self.bot).compress(ctx, filename) | |
| 63 | # Search for the video | 27 | await play_utils.PlayUtils(self.bot).play(ctx, filename, title) |
| 64 | videos_search = VideosSearch(search, limit=1) | ||
| 65 | result = videos_search.result() | ||
| 66 | |||
| 67 | if not result['result']: | ||
| 68 | await ctx.followup.send("nie znaleziono wideo dla twojego wyszukiwania.", ephemeral=True) | ||
| 69 | logger.info(f"Could not find a video for search: {search}") | ||
| 70 | return | ||
| 71 | |||
| 72 | video = result['result'][0] | ||
| 73 | title = video['title'] | ||
| 74 | url = video['link'] | ||
| 75 | |||
| 76 | # Play the audio directly | ||
| 77 | if vc.is_playing() or vc.is_paused(): | ||
| 78 | vc.stop() | ||
| 79 | await self.play_audio(ctx, vc, url, title) | ||
| 80 | |||
| 81 | 28 | ||
| 82 | def setup(bot): | 29 | def setup(bot): |
| 83 | bot.add_cog(Play(bot)) | 30 | bot.add_cog(Play(bot)) \ No newline at end of file |
