Some changes
This commit is contained in:
75
Music-Bot.py
75
Music-Bot.py
@@ -1,4 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
import datetime
|
||||||
import re
|
import re
|
||||||
from pytube import YouTube
|
from pytube import YouTube
|
||||||
import aiohttp
|
import aiohttp
|
||||||
@@ -31,13 +32,13 @@ parser.add_argument("-p", "--prefix", help="Provides the Prefix for the bot (Sta
|
|||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
if args.cores == None:
|
if args.cores is None:
|
||||||
threads = 2
|
threads = 2
|
||||||
else:
|
else:
|
||||||
threads = int(args.cores)
|
threads = int(args.cores)
|
||||||
|
|
||||||
|
|
||||||
if args.prefix == None:
|
if args.prefix is None:
|
||||||
b_prefix = "<"
|
b_prefix = "<"
|
||||||
else:
|
else:
|
||||||
b_prefix = str(args.prefix)
|
b_prefix = str(args.prefix)
|
||||||
@@ -108,9 +109,7 @@ else:
|
|||||||
async with aiohttp.ClientSession() as client:
|
async with aiohttp.ClientSession() as client:
|
||||||
async with client.get("https://www.youtube.com/results", params=p, headers=h) as resp:
|
async with client.get("https://www.youtube.com/results", params=p, headers=h) as resp:
|
||||||
dom = await resp.text()
|
dom = await resp.text()
|
||||||
result = re.findall(r"watch\?v=(\S{11})", dom)[0:5]
|
result = list(set(re.findall(r"watch\?v=(\S{11})", dom)))[0:5]
|
||||||
print(result)
|
|
||||||
print("----")
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@@ -119,7 +118,7 @@ else:
|
|||||||
|
|
||||||
bot = commands.Bot(
|
bot = commands.Bot(
|
||||||
command_prefix=commands.when_mentioned_or(b_prefix),
|
command_prefix=commands.when_mentioned_or(b_prefix),
|
||||||
description="Relatively simple music bot example",
|
description="BCA Music-Bot",
|
||||||
intents=intents, help_command=None
|
intents=intents, help_command=None
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -132,7 +131,8 @@ else:
|
|||||||
print(f"Threads for ffmpeg: {threads}")
|
print(f"Threads for ffmpeg: {threads}")
|
||||||
print(f"Prefix: {b_prefix}")
|
print(f"Prefix: {b_prefix}")
|
||||||
print(
|
print(
|
||||||
f"Invitation LINK: https://discord.com/api/oauth2/authorize?client_id={bot.user.id}&permissions=968552344896&scope=bot%20applications.commands")
|
f"Invitation LINK: https://discord.com/api/oauth2/authorize?client_id={bot.user.id}"
|
||||||
|
f"&permissions=968552344896&scope=bot%20applications.commands")
|
||||||
print("------")
|
print("------")
|
||||||
|
|
||||||
|
|
||||||
@@ -196,7 +196,7 @@ else:
|
|||||||
while ctx.voice_client.is_playing():
|
while ctx.voice_client.is_playing():
|
||||||
await asyncio.sleep(3)
|
await asyncio.sleep(3)
|
||||||
else:
|
else:
|
||||||
await ctx.voice_client.disconnect()
|
await ctx.voice_client.disconnect(force=True)
|
||||||
|
|
||||||
|
|
||||||
@bot.slash_command(name="stream", description="Streams a song from YouTube [Without preloading]")
|
@bot.slash_command(name="stream", description="Streams a song from YouTube [Without preloading]")
|
||||||
@@ -242,23 +242,58 @@ else:
|
|||||||
@bot.slash_command(name="youtube", description="Searches YouTube for a given string and plays the first result")
|
@bot.slash_command(name="youtube", description="Searches YouTube for a given string and plays the first result")
|
||||||
async def youtube(ctx, *, search: str):
|
async def youtube(ctx, *, search: str):
|
||||||
results = await youtube_search(search=search)
|
results = await youtube_search(search=search)
|
||||||
embed = Embed(title=f"Search results for `{search}`", color=discord.Color.green())
|
embed = Embed(title=f"", color=discord.Color.green())
|
||||||
|
embed.add_field(name="", value="", inline=False)
|
||||||
counter = 1
|
counter = 1
|
||||||
for result in results:
|
for result in results:
|
||||||
url = f"https://youtube.com/watch?v={result}"
|
url = f"https://youtube.com/watch?v={result}"
|
||||||
title = YouTube(url)
|
video = YouTube(url)
|
||||||
embed.add_field(name=f"{counter}", value=title.title, inline=False)
|
embed.add_field(name="", value=f"{counter} | `[{str(datetime.timedelta(seconds=video.length))}]` "
|
||||||
|
f"[{video.title}]({url}{result[counter-1]}) {video.author}", inline=False)
|
||||||
counter += 1
|
counter += 1
|
||||||
print(url)
|
await ctx.respond(f"{success}**🎶 Search results for `{search}`**")
|
||||||
await ctx.respond(f"{success}**A short selection of videos will be displayed shortly...**")
|
|
||||||
msg = await ctx.send(embed=embed)
|
msg = await ctx.send(embed=embed)
|
||||||
await msg.add_reaction('1️')
|
await msg.add_reaction("1️⃣")
|
||||||
#await msg.add_reaction('')
|
await msg.add_reaction("2️⃣")
|
||||||
#await msg.add_reaction('')
|
await msg.add_reaction("3️⃣")
|
||||||
#await msg.add_reaction('')
|
await msg.add_reaction("4️⃣")
|
||||||
#await msg.add_reaction('')
|
await msg.add_reaction("5️⃣")
|
||||||
#await msg.add_reaction('')
|
await msg.add_reaction("❌")
|
||||||
|
try:
|
||||||
|
reaction = await bot.wait_for(event='reaction_add', timeout=60.0)
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
await ctx.send('👎')
|
||||||
|
else:
|
||||||
|
vid_id = "https://youtube.com/watch?v="
|
||||||
|
if str(reaction[0]) == "1️⃣":
|
||||||
|
vid_id += results[0]
|
||||||
|
if str(reaction[0]) == "2️⃣":
|
||||||
|
vid_id += results[1]
|
||||||
|
if str(reaction[0]) == "3️⃣":
|
||||||
|
vid_id += results[2]
|
||||||
|
if str(reaction[0]) == "5️⃣":
|
||||||
|
vid_id += results[3]
|
||||||
|
if str(reaction[0]) == "5️⃣":
|
||||||
|
vid_id += results[4]
|
||||||
|
else:
|
||||||
|
vid_id = None
|
||||||
|
print(vid_id)
|
||||||
|
if vid_id is None:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
player = await YTDLSource.from_url(vid_id, loop=bot.loop)
|
||||||
|
ctx.voice_client.play(
|
||||||
|
player, after=lambda e: print(f"Player error: {e}") if e else None
|
||||||
|
)
|
||||||
|
except discord.HTTPException as err:
|
||||||
|
raise commands.CommandError(err)
|
||||||
|
finally:
|
||||||
|
await ctx.send(f"{success}Now playing: `{player.title}`\nRequested by {ctx.author.mention}")
|
||||||
|
while ctx.voice_client.is_playing():
|
||||||
|
await asyncio.sleep(3)
|
||||||
|
else:
|
||||||
|
await ctx.voice_client.disconnect(force=True)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
async with ctx.typing():
|
async with ctx.typing():
|
||||||
|
|||||||
Reference in New Issue
Block a user