Compare commits
43 Commits
fadb42bbf6
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 17cf18160c | |||
|
|
ab03ebb4da | ||
|
|
d38d93e8cb | ||
|
|
c940d735d2 | ||
|
|
53109d68f9 | ||
|
|
f55d45232c | ||
|
|
be7002bba5 | ||
|
|
c3eb9a9d73 | ||
|
|
67cb769111 | ||
|
|
cde8430d53 | ||
|
|
07ca78e1bc | ||
|
|
fb0a12c213 | ||
|
|
daaa120594 | ||
|
|
f09d8e3e9c | ||
|
|
0f16af383b | ||
|
|
607752fd4f | ||
|
|
b2d843324d | ||
|
|
7ce9e51155 | ||
|
|
8ff36532cc | ||
|
|
26a2d07714 | ||
|
|
4a0c0161bd | ||
|
|
a16a1e3ad2 | ||
|
|
a194d4a4ef | ||
|
|
f9e5ade444 | ||
|
|
40671616f3 | ||
|
|
116e7e6d15 | ||
|
|
443eec8320 | ||
|
|
71e76d754d | ||
|
|
be24384610 | ||
|
|
e1f1fedfdf | ||
|
|
4aabde261b | ||
|
|
53f175379f | ||
|
|
6993fde7d8 | ||
|
|
bc8b212504 | ||
|
|
e51f75c57c | ||
|
|
08934559b8 | ||
|
|
93fdfb1f77 | ||
|
|
9b1e473ec1 | ||
|
|
7ffc356b1c | ||
|
|
71bd1f6ae5 | ||
|
|
5d58d05668 | ||
|
|
c1e731aa4d | ||
|
|
c08bad409f |
136
Music-Bot.py
136
Music-Bot.py
@@ -1,5 +1,8 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
from datetime import timedelta
|
import datetime
|
||||||
|
import re
|
||||||
|
from pytube import YouTube
|
||||||
|
import aiohttp
|
||||||
import yt_dlp
|
import yt_dlp
|
||||||
import discord
|
import discord
|
||||||
import logging
|
import logging
|
||||||
@@ -14,7 +17,9 @@ banner = """
|
|||||||
| | | | \__ \ | ( _____| | | ( | |
|
| | | | \__ \ | ( _____| | | ( | |
|
||||||
_| _| \__,_| ____/ _| \___| ____/ \___/ \__|
|
_| _| \__,_| ____/ _| \___| ____/ \___/ \__|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
version = "0.1"
|
version = "0.1"
|
||||||
|
branch = "dev"
|
||||||
success = "**Success ✅**\n"
|
success = "**Success ✅**\n"
|
||||||
warning = "**Warning ℹ️**\n"
|
warning = "**Warning ℹ️**\n"
|
||||||
error = "**Error ❗️**\n"
|
error = "**Error ❗️**\n"
|
||||||
@@ -22,9 +27,24 @@ cache_dir = "cache"
|
|||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("-t", "--token", help="Bot TOKEN")
|
parser.add_argument("-t", "--token", help="Bot TOKEN")
|
||||||
|
parser.add_argument("-c", "--cores", help="Number of Cores for ffmpeg (Standard 2)")
|
||||||
|
parser.add_argument("-p", "--prefix", help="Provides the Prefix for the bot (Standard <)")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if args.token == None:
|
|
||||||
|
if args.cores is None:
|
||||||
|
threads = 2
|
||||||
|
else:
|
||||||
|
threads = int(args.cores)
|
||||||
|
|
||||||
|
|
||||||
|
if args.prefix is None:
|
||||||
|
b_prefix = "<"
|
||||||
|
else:
|
||||||
|
b_prefix = str(args.prefix)
|
||||||
|
|
||||||
|
|
||||||
|
if args.token is None:
|
||||||
print(f"{banner}\n\nPLEASE PROVIDE BOT A TOKEN BY RUNNING LIKE THE FOLLOWING:")
|
print(f"{banner}\n\nPLEASE PROVIDE BOT A TOKEN BY RUNNING LIKE THE FOLLOWING:")
|
||||||
print("\n")
|
print("\n")
|
||||||
print(">>> python3 Music-Bot.py -t TOKEN <<<")
|
print(">>> python3 Music-Bot.py -t TOKEN <<<")
|
||||||
@@ -79,15 +99,26 @@ else:
|
|||||||
|
|
||||||
filename = data["url"] if stream else ytdl.prepare_filename(data)
|
filename = data["url"] if stream else ytdl.prepare_filename(data)
|
||||||
return cls(discord.FFmpegPCMAudio(source=filename, executable="ffmpeg", pipe=False, stderr=False,
|
return cls(discord.FFmpegPCMAudio(source=filename, executable="ffmpeg", pipe=False, stderr=False,
|
||||||
before_options="-threads 2", options=ffmpeg_options), data=data)
|
before_options=f"-threads {threads}", options=ffmpeg_options), data=data)
|
||||||
|
|
||||||
|
|
||||||
|
async def youtube_search(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()
|
||||||
|
result = list(set(re.findall(r"watch\?v=(\S{11})", dom)))[0:5]
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
intents = discord.Intents.default()
|
intents = discord.Intents.default()
|
||||||
intents.message_content = True
|
intents.message_content = True
|
||||||
|
|
||||||
bot = commands.Bot(
|
bot = commands.Bot(
|
||||||
command_prefix=commands.when_mentioned_or("!"),
|
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
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -95,10 +126,13 @@ else:
|
|||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
print(banner)
|
print(banner)
|
||||||
print(f"Bot Version: {version}")
|
print(f"Bot Version: {version} ({branch})")
|
||||||
print(f"Bot: {bot.user} (ID: {bot.user.id})")
|
print(f"Bot: {bot.user} (ID: {bot.user.id})")
|
||||||
|
print(f"Threads for ffmpeg: {threads}")
|
||||||
|
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("------")
|
||||||
|
|
||||||
|
|
||||||
@@ -159,6 +193,10 @@ else:
|
|||||||
raise commands.CommandError(err)
|
raise commands.CommandError(err)
|
||||||
finally:
|
finally:
|
||||||
await ctx.send(f"{success}Now playing: `{player.title}`\nRequested by {ctx.author.mention}")
|
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)
|
||||||
|
|
||||||
|
|
||||||
@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]")
|
||||||
@@ -168,8 +206,11 @@ else:
|
|||||||
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
|
||||||
)
|
)
|
||||||
|
|
||||||
await ctx.respond(f"{success}Now playing: `{player.title}`\nRequested by {ctx.author.mention}")
|
await ctx.respond(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()
|
||||||
|
|
||||||
|
|
||||||
@bot.slash_command(name="pause", description="Pauses the playback")
|
@bot.slash_command(name="pause", description="Pauses the playback")
|
||||||
@@ -198,8 +239,84 @@ else:
|
|||||||
await ctx.voice_client.disconnect(force=True)
|
await ctx.voice_client.disconnect(force=True)
|
||||||
|
|
||||||
|
|
||||||
|
@bot.slash_command(name="youtube", description="Searches YouTube for a given string and plays the first result")
|
||||||
|
async def youtube(ctx, *, search: str):
|
||||||
|
results = await youtube_search(search=search)
|
||||||
|
embed = Embed(title=f"", color=discord.Color.green())
|
||||||
|
embed.add_field(name="", value="", inline=False)
|
||||||
|
counter = 1
|
||||||
|
for result in results:
|
||||||
|
url = f"https://youtube.com/watch?v={result}"
|
||||||
|
video = YouTube(url)
|
||||||
|
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
|
||||||
|
await ctx.respond(f"{success}**🎶 Search results for `{search}`**")
|
||||||
|
msg = await ctx.send(embed=embed)
|
||||||
|
await msg.add_reaction("1️⃣")
|
||||||
|
await msg.add_reaction("2️⃣")
|
||||||
|
await msg.add_reaction("3️⃣")
|
||||||
|
await msg.add_reaction("4️⃣")
|
||||||
|
await msg.add_reaction("5️⃣")
|
||||||
|
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():
|
||||||
|
await ctx.respond("🤖 Your song is queued for download... please wait ", ephemeral=True)
|
||||||
|
try:
|
||||||
|
player = await YTDLSource.from_url(url, 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()
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
@play.before_invoke
|
@play.before_invoke
|
||||||
@stream.before_invoke
|
@stream.before_invoke
|
||||||
|
#@youtube.before_invoke
|
||||||
async def ensure_voice(ctx: commands.Context):
|
async def ensure_voice(ctx: commands.Context):
|
||||||
if ctx.voice_client is None:
|
if ctx.voice_client is None:
|
||||||
if ctx.author.voice:
|
if ctx.author.voice:
|
||||||
@@ -210,6 +327,8 @@ else:
|
|||||||
ctx.voice_client.stop()
|
ctx.voice_client.stop()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_application_command_error(ctx, error):
|
async def on_application_command_error(ctx, error):
|
||||||
print(f"[on_application_command_error]\n{ctx.author}\n{error}")
|
print(f"[on_application_command_error]\n{ctx.author}\n{error}")
|
||||||
@@ -234,5 +353,6 @@ else:
|
|||||||
print(f"[on_command_error]\n{ctx.author}\n{error}")
|
print(f"[on_command_error]\n{ctx.author}\n{error}")
|
||||||
embed = Embed(title=f"{error}", color=15158332)
|
embed = Embed(title=f"{error}", color=15158332)
|
||||||
await ctx.send(embed=embed)
|
await ctx.send(embed=embed)
|
||||||
|
"""
|
||||||
|
|
||||||
bot.run(token=args.token, reconnect=True)
|
bot.run(token=args.token, reconnect=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user