Compare commits
45 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a3c818a963 | ||
|
|
be24384610 | ||
|
|
e1f1fedfdf | ||
|
|
4aabde261b | ||
|
|
53f175379f | ||
|
|
6993fde7d8 | ||
|
|
bc8b212504 | ||
|
|
e51f75c57c | ||
|
|
08934559b8 | ||
|
|
93fdfb1f77 | ||
|
|
9b1e473ec1 | ||
|
|
7ffc356b1c | ||
|
|
71bd1f6ae5 | ||
|
|
5d58d05668 | ||
|
|
c1e731aa4d | ||
|
|
c08bad409f | ||
|
|
fadb42bbf6 | ||
|
|
5fddad561d | ||
|
|
651cf70885 | ||
|
|
02609a65e5 | ||
|
|
837686c71d | ||
|
|
6b8a8a0dfc | ||
|
|
9540f37d06 | ||
|
|
f87f878ca4 | ||
|
|
52cf259c20 | ||
|
|
3893e6ddc0 | ||
|
|
de2779b61f | ||
|
|
6070780b0a | ||
|
|
c2e3081995 | ||
|
|
7e0a927c2b | ||
|
|
ba14c23ed9 | ||
|
|
e770272bc4 | ||
|
|
f6439245c6 | ||
|
|
3496e0e5a7 | ||
|
|
22e74ff829 | ||
|
|
467f2dd00b | ||
|
|
dec4be3d1b | ||
|
|
7a09c752e9 | ||
|
|
93ec9ca2ec | ||
|
|
9f61462777 | ||
|
|
1d5e32eec0 | ||
|
|
b286d8338e | ||
|
|
9735a408c9 | ||
|
|
e9bcaa210c | ||
|
|
ede2a02bb7 |
81
Music-Bot.py
81
Music-Bot.py
@@ -1,9 +1,13 @@
|
||||
import asyncio
|
||||
import re
|
||||
from datetime import timedelta
|
||||
|
||||
import aiohttp
|
||||
import yt_dlp
|
||||
import discord
|
||||
import logging
|
||||
import argparse
|
||||
import os
|
||||
from discord.ext import commands
|
||||
from discord import Embed
|
||||
|
||||
@@ -13,10 +17,12 @@ banner = """
|
||||
| | | | \__ \ | ( _____| | | ( | |
|
||||
_| _| \__,_| ____/ _| \___| ____/ \___/ \__|
|
||||
"""
|
||||
version = "0.1"
|
||||
version = "0.2"
|
||||
branch = "main"
|
||||
success = "**Success ✅**\n"
|
||||
warning = "**Warning ℹ️**\n"
|
||||
error = "**Error ❗️**\n"
|
||||
cache_dir = "cache"
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("-t", "--token", help="Bot TOKEN")
|
||||
@@ -33,7 +39,7 @@ else:
|
||||
|
||||
ytdl_format_options = {
|
||||
"format": "bestaudio/mp3",
|
||||
"outtmpl": "cache/%(extractor)s-%(id)s-%(title)s.mp3",
|
||||
"outtmpl": f"{cache_dir}/%(extractor)s-%(id)s-%(title)s.mp3",
|
||||
"restrictfilenames": True,
|
||||
"noplaylist": True,
|
||||
"nocheckcertificate": True,
|
||||
@@ -76,7 +82,20 @@ else:
|
||||
data = data["entries"][0]
|
||||
|
||||
filename = data["url"] if stream else ytdl.prepare_filename(data)
|
||||
return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)
|
||||
return cls(discord.FFmpegPCMAudio(source=filename, executable="ffmpeg", pipe=False, stderr=False,
|
||||
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()
|
||||
@@ -85,33 +104,44 @@ else:
|
||||
bot = commands.Bot(
|
||||
command_prefix=commands.when_mentioned_or("!"),
|
||||
description="Relatively simple music bot example",
|
||||
intents=intents,
|
||||
intents=intents, help_command=None
|
||||
)
|
||||
|
||||
|
||||
@bot.event
|
||||
async def on_ready():
|
||||
print(banner)
|
||||
print(f"Bot Version: {version}")
|
||||
print(f"Bot Version: {version} ({branch})")
|
||||
print(f"Bot: {bot.user} (ID: {bot.user.id})")
|
||||
print(
|
||||
f"Invitation LINK: https://discord.com/api/oauth2/authorize?client_id={bot.user.id}&permissions=968552344896&scope=bot%20applications.commands")
|
||||
print("------")
|
||||
|
||||
|
||||
@bot.command(name="shutdown")
|
||||
@commands.is_owner()
|
||||
async def _shutdown(ctx):
|
||||
files = os.listdir(cache_dir)
|
||||
async with ctx.channel.typing():
|
||||
for file in files:
|
||||
os.remove(f"{cache_dir}/{file}")
|
||||
await ctx.send(f"{warning}Cleaned up {len(files)} Files...\n\n{success}Shutting down...")
|
||||
await bot.close()
|
||||
|
||||
|
||||
@bot.slash_command(name="join", description="Summon the bot into your channel")
|
||||
async def join(ctx: commands.Context):
|
||||
try:
|
||||
channel = ctx.author.voice.channel
|
||||
if channel is None:
|
||||
await ctx.send(f"{error}You are not in a voice-channel! Could not join...")
|
||||
if ctx.voice_client is not None:
|
||||
return await ctx.voice_client.move_to(channel)
|
||||
|
||||
await ctx.respond(f"{success}Connected to `{channel.name}`")
|
||||
await channel.connect()
|
||||
except Exception as err:
|
||||
print(err)
|
||||
if channel is None:
|
||||
await ctx.send(f"{error}You are not in a voice-channel! Could not join...")
|
||||
if ctx.voice_client is not None:
|
||||
return await ctx.voice_client.move_to(channel)
|
||||
|
||||
await ctx.respond(f"{success}Connected to `{channel.name}`")
|
||||
await channel.connect()
|
||||
|
||||
|
||||
@bot.slash_command(name="nowplaying", descriprion="Show details what the bot is currently playing")
|
||||
@@ -184,8 +214,25 @@ else:
|
||||
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
|
||||
@stream.before_invoke
|
||||
@youtube.before_invoke
|
||||
async def ensure_voice(ctx: commands.Context):
|
||||
if ctx.voice_client is None:
|
||||
if ctx.author.voice:
|
||||
@@ -200,15 +247,16 @@ else:
|
||||
async def on_application_command_error(ctx, error):
|
||||
print(f"[on_application_command_error]\n{ctx.author}\n{error}")
|
||||
if isinstance(error, discord.ext.commands.CommandError):
|
||||
cool_down_time = int(error.cooldown.get_retry_after())
|
||||
td = timedelta(seconds=cool_down_time)
|
||||
embed = Embed(title=f"{warning}Dieser Command befindet sich im Cool Down!\n Versuche es in `{td}` nochmal!",
|
||||
embed = Embed(title=f"{warning}{error}",
|
||||
color=15158332)
|
||||
elif isinstance(error, discord.ext.commands.CommandOnCooldown):
|
||||
cool_down_time = int(error.cooldown.get_retry_after())
|
||||
td = timedelta(seconds=cool_down_time)
|
||||
embed = Embed(title=f"{warning}Dieser Command befindet sich im Cool Down!\n Versuche es in `{td}` nochmal!",
|
||||
color=15158332)
|
||||
elif isinstance(error, discord.ext.commands.is_owner()):
|
||||
embed = Embed(title=f"{warning}{error}",
|
||||
color=15158332)
|
||||
else:
|
||||
embed = Embed(title=f"{error}", color=15158332)
|
||||
await ctx.respond(embed=embed)
|
||||
@@ -220,5 +268,4 @@ else:
|
||||
embed = Embed(title=f"{error}", color=15158332)
|
||||
await ctx.send(embed=embed)
|
||||
|
||||
|
||||
bot.run(args.token)
|
||||
bot.run(token=args.token, reconnect=True)
|
||||
|
||||
BIN
Music-Bot.pyc
BIN
Music-Bot.pyc
Binary file not shown.
@@ -10,7 +10,7 @@ Simple Music-Bot for Discord written in Python with <3
|
||||
## How to start
|
||||
1. Clone the repository
|
||||
2. cd into it
|
||||
3. Run the **compiled** version: python3 Music-Bot.pyc -t TOKEN
|
||||
3. Run : python3 Music-Bot.pyc -t TOKEN
|
||||
4. Click on the invitation-Link in the terminal
|
||||
5. DONE <3
|
||||
|
||||
|
||||
Reference in New Issue
Block a user