Compare commits

36 Commits

Author SHA1 Message Date
Lukas Blacha
c3eb9a9d73 Fixed Typing... 2023-02-08 19:01:07 +01:00
Lukas Blacha
67cb769111 Added auto-disconnect feature 2023-02-08 18:57:57 +01:00
Lukas Blacha
cde8430d53 Added auto-disconnect feature 2023-02-08 18:55:59 +01:00
Lukas Blacha
07ca78e1bc Added auto-disconnect feature 2023-02-08 18:54:39 +01:00
Lukas Blacha
fb0a12c213 Added auto-disconnect feature 2023-02-08 18:53:20 +01:00
Lukas Blacha
daaa120594 Added auto-disconnect feature 2023-02-08 18:52:58 +01:00
Lukas Blacha
f09d8e3e9c Added auto-disconnect feature 2023-02-08 18:50:58 +01:00
Lukas Blacha
0f16af383b Added auto-disconnect feature 2023-02-08 18:47:13 +01:00
Lukas Blacha
607752fd4f Added auto-disconnect feature 2023-02-08 18:44:45 +01:00
Lukas Blacha
b2d843324d Added auto-disconnect feature 2023-02-08 18:43:11 +01:00
Lukas Blacha
7ce9e51155 Added auto-disconnect feature 2023-02-08 18:40:54 +01:00
Lukas Blacha
8ff36532cc Added auto-disconnect feature 2023-02-08 18:38:21 +01:00
Lukas Blacha
26a2d07714 Added auto-disconnect feature 2023-02-08 18:31:04 +01:00
Lukas Blacha
4a0c0161bd Added auto-disconnect feature 2023-02-08 18:30:03 +01:00
Lukas Blacha
a16a1e3ad2 Added auto-disconnect feature 2023-02-08 18:19:55 +01:00
Lukas Blacha
a194d4a4ef Added auto-disconnect feature 2023-02-08 18:17:31 +01:00
Lukas Blacha
f9e5ade444 Added prefix flag for Startup (Standard "<") 2023-02-08 18:15:48 +01:00
Lukas Blacha
40671616f3 Added prefix flag for Startup (Standard "<") 2023-02-08 17:59:01 +01:00
Lukas Blacha
116e7e6d15 Added prefix flag for Startup (Standard "<") 2023-02-08 17:57:28 +01:00
Lukas Blacha
443eec8320 Added cores flag for Startup 2023-02-08 17:42:53 +01:00
Lukas Blacha
71e76d754d Changed Version and Branch (0.2, main) 2023-02-08 13:19:49 +01:00
Lukas Blacha
be24384610 Added Youtube Search-Command 2023-02-08 13:14:23 +01:00
Lukas Blacha
e1f1fedfdf Added youtube_search method 2023-02-08 12:54:36 +01:00
Lukas Blacha
4aabde261b Added youtube_search method 2023-02-08 12:48:01 +01:00
Lukas Blacha
53f175379f Added youtube_search method 2023-02-08 12:47:08 +01:00
Lukas Blacha
6993fde7d8 Changed Google-Search to Youtube (fix) 2023-02-08 12:42:49 +01:00
Lukas Blacha
bc8b212504 Changed Google-Search to Youtube (fix) 2023-02-08 12:42:14 +01:00
Lukas Blacha
e51f75c57c Changed Google-Search to Youtube (fix) 2023-02-08 12:39:56 +01:00
Lukas Blacha
08934559b8 Changed Google-Search to Youtube (fix) 2023-02-08 12:16:41 +01:00
Lukas Blacha
93fdfb1f77 Changed Google-Search to Youtube (fix) 2023-02-08 12:15:27 +01:00
Lukas Blacha
9b1e473ec1 Changed Google-Search to Youtube (fix) 2023-02-08 12:15:03 +01:00
Lukas Blacha
7ffc356b1c Changed Google-Search to Youtube (fix) 2023-02-08 12:14:00 +01:00
Lukas Blacha
71bd1f6ae5 Changed Google-Search to Youtube (fix) 2023-02-08 12:11:04 +01:00
Lukas Blacha
5d58d05668 Changed Google-Search to Youtube 2023-02-08 12:10:18 +01:00
Lukas Blacha
c1e731aa4d Added branch variable 2023-02-08 11:45:52 +01:00
Lukas Blacha
c08bad409f Added Google-Command 2023-02-08 11:42:24 +01:00

View File

@@ -1,5 +1,9 @@
import asyncio import asyncio
import re
from datetime import timedelta from datetime import timedelta
from urllib.parse import quote_plus
import aiohttp
import yt_dlp import yt_dlp
import discord import discord
import logging import logging
@@ -14,7 +18,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 +28,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 == None:
threads = 2
else:
threads = int(args.cores)
if args.prefix == 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,14 +100,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()
found = re.findall(r"watch\?v=(\S{11})", dom)[0]
url = f"https://www.youtube.com/watch?v={found}"
return url
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="Relatively simple music bot example",
intents=intents, help_command=None intents=intents, help_command=None
) )
@@ -95,8 +128,10 @@ 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}&permissions=968552344896&scope=bot%20applications.commands")
print("------") print("------")
@@ -159,6 +194,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()
@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 +207,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 +240,29 @@ 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):
url = await youtube_search(search=search)
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 +273,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}")