diff --git a/main.py b/main.py index c8b10e6..8699a7b 100644 --- a/main.py +++ b/main.py @@ -2,17 +2,34 @@ import discord import yt_dlp as youtube_dl import time import asyncio +import requests +import json g_client = discord.Client() queue = [] voice_client = None + +async def q_vid(url): + global voice_client + global queue + ydl = youtube_dl.YoutubeDL({"simulate": True}) + res = ydl.extract_info(url, force_generic_extractor=ydl.params.get('force_generic_extractor', False)) + + link = None + for format in res["requested_formats"]: + if not (format["asr"] is None): + link = format["url"] + if not link: return + + queue.append({"link": link, "ctx": res}) + @g_client.event async def on_ready(): print("ohai") while True: await asyncio.sleep(.1) if len(queue) > 0: - if not voice_client.is_playing() and voice_client is not None: + if (voice_client is not None) and (not voice_client.is_playing()): voice_client.play(discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(queue[0]["link"], before_options="-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5"), volume=1.0)) print("playan") sav = queue[0]["link"] @@ -41,16 +58,9 @@ async def on_message(message): if message.content.startswith("*play"): try: voice_client = await message.author.voice.channel.connect() except: pass - ydl = youtube_dl.YoutubeDL({"simulate": True}) - res = ydl.extract_info(message.content.split(" ")[1], force_generic_extractor=ydl.params.get('force_generic_extractor', False)) - link = None - for format in res["requested_formats"]: - if not (format["asr"] is None): - link = format["url"] - if not link: return - - queue.append({"link": link, "ctx": res}) + await q_vid(message.content.split(" ")[1]) + await message.channel.send("k") if message.content.startswith("*np"): @@ -71,6 +81,21 @@ async def on_message(message): if message.content.startswith("*s"): queue.pop(0) voice_client.stop() + + if message.content.startswith("*b"): + try: voice_client = await message.author.voice.channel.connect() + except: pass + res = json.loads(requests.get(f"https://vid.puffyan.us/api/v1/search?q={'%20'.join(message.content.split(' ')[1:])}&pretty=1").text) + res = [item for item in res if item["type"] == "video"] + if message.content.split(' ')[0][-1] in list("012345"): + await q_vid(f"https://www.youtube.com/watch?v={res[int(message.content.split(' ')[0][-1])]['videoId']}") + return + embed = discord.Embed(title="does this look right") + for i, q in enumerate(res): + if i > 5: break + embed.add_field(name=str(i), value=q["title"], inline=False) + await message.channel.send(embed=embed) + conf = open("token", "r") g_client.run(conf.read(), bot=True) conf.close() \ No newline at end of file