Compare commits
16 Commits
fadb42bbf6
...
A-0.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
71e76d754d | ||
|
|
be24384610 | ||
|
|
e1f1fedfdf | ||
|
|
4aabde261b | ||
|
|
53f175379f | ||
|
|
6993fde7d8 | ||
|
|
bc8b212504 | ||
|
|
e51f75c57c | ||
|
|
08934559b8 | ||
|
|
93fdfb1f77 | ||
|
|
9b1e473ec1 | ||
|
|
7ffc356b1c | ||
|
|
71bd1f6ae5 | ||
|
|
5d58d05668 | ||
|
|
c1e731aa4d | ||
|
|
c08bad409f |
37
Music-Bot.py
37
Music-Bot.py
@@ -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"
|
||||||
@@ -82,6 +88,18 @@ else:
|
|||||||
before_options="-threads 2", options=ffmpeg_options), data=data)
|
before_options="-threads 2", 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
|
||||||
|
|
||||||
@@ -95,7 +113,7 @@ 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(
|
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")
|
||||||
@@ -198,8 +216,25 @@ 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}")
|
||||||
|
|
||||||
|
|
||||||
@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:
|
||||||
|
|||||||
Reference in New Issue
Block a user