Added youtube_search method
This commit is contained in:
40
Music-Bot.py
40
Music-Bot.py
@@ -87,20 +87,17 @@ else:
|
|||||||
before_options="-threads 2", options=ffmpeg_options), data=data)
|
before_options="-threads 2", options=ffmpeg_options), data=data)
|
||||||
|
|
||||||
|
|
||||||
class Youtube(discord.ui.View):
|
async def youtube_search(search: str):
|
||||||
def __init__(self, query: str):
|
p = {"search_query": search}
|
||||||
super().__init__()
|
# Spoof a user agent header or the request will immediately fail
|
||||||
# We need to quote the query string to make a valid url. Discord will raise an error if it isn't valid.
|
h = {"User-Agent": "Mozilla/5.0"}
|
||||||
query = quote_plus(query)
|
async with aiohttp.ClientSession() as client:
|
||||||
url = f"https://www.youtube.com/results?search_query={query}"
|
async with client.get("https://www.youtube.com/results", params=p, headers=h) as resp:
|
||||||
|
dom = await resp.text()
|
||||||
# Link buttons cannot be made with the
|
open("debug.html", "w").write(dom)
|
||||||
# decorator, so we have to manually create one.
|
found = re.findall(r"watch\?v=(\S{11})", dom)[0]
|
||||||
# We add the quoted url to the button, and add the button to the view.
|
url = f"https://www.youtube.com/watch?v={found}"
|
||||||
self.add_item(discord.ui.Button(label="Click Here", url=url))
|
return url
|
||||||
|
|
||||||
# Initializing the view and adding the button can actually be done in a one-liner at the start if preferred:
|
|
||||||
# super().__init__(discord.ui.Button(label="Click Here", url=url))
|
|
||||||
|
|
||||||
|
|
||||||
intents = discord.Intents.default()
|
intents = discord.Intents.default()
|
||||||
@@ -220,22 +217,11 @@ else:
|
|||||||
|
|
||||||
|
|
||||||
@bot.slash_command()
|
@bot.slash_command()
|
||||||
async def youtube(ctx, *, search):
|
async def youtube(ctx, *, search: str):
|
||||||
p = {"search_query": search}
|
|
||||||
# Spoof a user agent header or the request will immediately fail
|
|
||||||
h = {"User-Agent": "Mozilla/5.0"}
|
|
||||||
async with aiohttp.ClientSession() as client:
|
|
||||||
async with client.get("https://www.youtube.com/results", params=p, headers=h) as resp:
|
|
||||||
dom = await resp.text()
|
|
||||||
open("debug.html", "w").write(dom)
|
|
||||||
found = re.findall(r"watch\?v=(\S{11})", dom)[0]
|
|
||||||
url = f"https://www.youtube.com/watch?v={found}"
|
|
||||||
print(url)
|
|
||||||
|
|
||||||
async with ctx.typing():
|
async with ctx.typing():
|
||||||
await ctx.respond("🤖 Your song is queued for download... please wait ", ephemeral=True)
|
await ctx.respond("🤖 Your song is queued for download... please wait ", ephemeral=True)
|
||||||
try:
|
try:
|
||||||
player = await YTDLSource.from_url(url, loop=bot.loop)
|
player = await YTDLSource.from_url(youtube_search(search=search), loop=bot.loop)
|
||||||
ctx.voice_client.play(
|
ctx.voice_client.play(
|
||||||
player, after=lambda e: print(f"Player error: {e}") if e else None
|
player, after=lambda e: print(f"Player error: {e}") if e else None
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user