From fcc3635b9f24051ae07ed597632597e20372c889 Mon Sep 17 00:00:00 2001 From: SamlonDev Date: Tue, 20 May 2025 05:45:23 +0200 Subject: initial push --- cogs/music/play.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 cogs/music/play.py (limited to 'cogs/music/play.py') diff --git a/cogs/music/play.py b/cogs/music/play.py new file mode 100644 index 0000000..1eafbca --- /dev/null +++ b/cogs/music/play.py @@ -0,0 +1,53 @@ +import discord +from discord.ext import commands +import asyncio +from youtubesearchpython import VideosSearch + +class Play(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @commands.slash_command(name="grajcuj", description="grajcuj melodyje") + async def play(self, ctx, *, search: str): + await ctx.defer() + if not (ctx.author.voice and ctx.author.voice.channel): + await ctx.followup.send("nie jesteś w kanale głosowym") + return + channel = ctx.author.voice.channel + if ctx.voice_client is not None: + vc = ctx.voice_client + await vc.move_to(channel) + else: + vc = await channel.connect() + 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) + return + video = result['result'][0] + video_id = video['id'] + title = video['title'] + url = video['link'] + proc = await asyncio.create_subprocess_exec( + "yt-dlp", "-f", "bestaudio", "-g", "--no-cache-dir", url, + stdout=asyncio.subprocess.PIPE + ) + stdout, _ = await proc.communicate() + stream_url = stdout.decode().strip() + FFMPEG_OPTIONS = { + 'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', + 'options': '-vn' + } + audio = discord.FFmpegPCMAudio( + stream_url, + before_options=FFMPEG_OPTIONS['before_options'], + options=FFMPEG_OPTIONS['options'] + ) + if not vc.is_playing(): + vc.play(audio) + await ctx.followup.send(f"gra: {title}") + else: + await ctx.followup.send(f"dodano do koleyjki: {title}") + +def setup(bot): + bot.add_cog(Play(bot)) \ No newline at end of file -- cgit v1.2.3