aboutsummaryrefslogtreecommitdiffstats
path: root/cogs/ping_commands.py
diff options
context:
space:
mode:
Diffstat (limited to 'cogs/ping_commands.py')
-rw-r--r--cogs/ping_commands.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/cogs/ping_commands.py b/cogs/ping_commands.py
new file mode 100644
index 0000000..beb9685
--- /dev/null
+++ b/cogs/ping_commands.py
@@ -0,0 +1,60 @@
1import discord
2from discord.ext import commands
3import asyncio
4from yt_dlp import VideosSearch
5
6class PingCommands(commands.Cog):
7 def __init__(self, bot):
8 self.bot = bot
9
10 @commands.Cog.command()
11 async def vc(self, ctx):
12 # join voice channel
13 pass
14
15 @commands.slash_command(name="grajcuj", description="grajcuj melodyje")
16 async def play(self, ctx, *, search: str):
17 await ctx.defer()
18 if not (ctx.author.voice and ctx.author.voice.channel):
19 await ctx.followup.send("nie jesteś w kanale głosowym")
20 return
21 channel = ctx.author.voice.channel
22 if ctx.voice_client is not None:
23 vc = ctx.voice_client
24 await vc.move_to(channel)
25 else:
26 vc = await channel.connect()
27 videos_search = VideosSearch(search, limit=1)
28 result = videos_search.result()
29 if not result['result']:
30 await ctx.followup.send("nie znaleziono wideo dla twojego wyszukiwania.", ephemeral=True)
31 return
32 video = result['result'][0]
33 title = video['title']
34 url = video['link']
35
36 # Get direct audio stream URL
37 proc = await asyncio.create_subprocess_exec(
38 "yt-dlp", "-f", "bestaudio", "-g", url,
39 stdout=asyncio.subprocess.PIPE
40 )
41 stdout, _ = await proc.communicate()
42 stream_url = stdout.decode().strip()
43
44 ffmpeg_options = {
45 "before_options": "-probesize 32k -analyzeduration 0 -reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5",
46 "options": "-vn"
47 }
48 audio = discord.FFmpegPCMAudio(
49 stream_url,
50 before_options=ffmpeg_options["before_options"],
51 options=ffmpeg_options["options"]
52 )
53 if not vc.is_playing():
54 vc.play(audio)
55 await ctx.followup.send(f"gra: {title}")
56 else:
57 await ctx.followup.send(f"dodano do koleyjki: {title}")
58
59
60