diff options
| author | SamlonDev <stankiewiczgracjan2019@gmail.com> | 2025-05-20 05:45:23 +0200 |
|---|---|---|
| committer | SamlonDev <stankiewiczgracjan2019@gmail.com> | 2025-05-20 05:45:23 +0200 |
| commit | fcc3635b9f24051ae07ed597632597e20372c889 (patch) | |
| tree | 20a29a1a784f218b3623ff03ceb8084839ff606a /cogs/music/play.py | |
| download | senator-sperma-fcc3635b9f24051ae07ed597632597e20372c889.tar.gz senator-sperma-fcc3635b9f24051ae07ed597632597e20372c889.zip | |
initial push
Diffstat (limited to 'cogs/music/play.py')
| -rw-r--r-- | cogs/music/play.py | 53 |
1 files changed, 53 insertions, 0 deletions
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 @@ | |||
| 1 | import discord | ||
| 2 | from discord.ext import commands | ||
| 3 | import asyncio | ||
| 4 | from youtubesearchpython import VideosSearch | ||
| 5 | |||
| 6 | class Play(commands.Cog): | ||
| 7 | def __init__(self, bot): | ||
| 8 | self.bot = bot | ||
| 9 | |||
| 10 | @commands.slash_command(name="grajcuj", description="grajcuj melodyje") | ||
| 11 | async def play(self, ctx, *, search: str): | ||
| 12 | await ctx.defer() | ||
| 13 | if not (ctx.author.voice and ctx.author.voice.channel): | ||
| 14 | await ctx.followup.send("nie jesteś w kanale głosowym") | ||
| 15 | return | ||
| 16 | channel = ctx.author.voice.channel | ||
| 17 | if ctx.voice_client is not None: | ||
| 18 | vc = ctx.voice_client | ||
| 19 | await vc.move_to(channel) | ||
| 20 | else: | ||
| 21 | vc = await channel.connect() | ||
| 22 | videos_search = VideosSearch(search, limit=1) | ||
| 23 | result = videos_search.result() | ||
| 24 | if not result['result']: | ||
| 25 | await ctx.followup.send("nie znaleziono wideo dla twojego wyszukiwania.", ephemeral=True) | ||
| 26 | return | ||
| 27 | video = result['result'][0] | ||
| 28 | video_id = video['id'] | ||
| 29 | title = video['title'] | ||
| 30 | url = video['link'] | ||
| 31 | proc = await asyncio.create_subprocess_exec( | ||
| 32 | "yt-dlp", "-f", "bestaudio", "-g", "--no-cache-dir", url, | ||
| 33 | stdout=asyncio.subprocess.PIPE | ||
| 34 | ) | ||
| 35 | stdout, _ = await proc.communicate() | ||
| 36 | stream_url = stdout.decode().strip() | ||
| 37 | FFMPEG_OPTIONS = { | ||
| 38 | 'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', | ||
| 39 | 'options': '-vn' | ||
| 40 | } | ||
| 41 | audio = discord.FFmpegPCMAudio( | ||
| 42 | stream_url, | ||
| 43 | before_options=FFMPEG_OPTIONS['before_options'], | ||
| 44 | options=FFMPEG_OPTIONS['options'] | ||
| 45 | ) | ||
| 46 | if not vc.is_playing(): | ||
| 47 | vc.play(audio) | ||
| 48 | await ctx.followup.send(f"gra: {title}") | ||
| 49 | else: | ||
| 50 | await ctx.followup.send(f"dodano do koleyjki: {title}") | ||
| 51 | |||
| 52 | def setup(bot): | ||
| 53 | bot.add_cog(Play(bot)) \ No newline at end of file | ||
