Code: Alles auswählen
import discord
import os
import asyncio
from discord.ext import commands
intents = discord.Intents.default()
intents.voice_states = True
intents.messages = True
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_ready():
print(f'Bot ist eingeloggt als {bot.user}')
async def play_audio_file(voice_client, file_path, ctx):
try:
audio_source = discord.FFmpegPCMAudio(file_path)
voice_client.play(audio_source)
# Den aktuellen Titel im Chat anzeigen
file_name = os.path.basename(file_path)
await ctx.send(f"Jetzt läuft: {file_name}")
while voice_client.is_playing():
await asyncio.sleep(1)
except Exception as e:
print(f'Fehler beim Abspielen der Datei {file_path}: {str(e)}')
async def play_folder_recursive(voice_client, folder_path, depth, ctx):
if depth == 0:
for file_name in os.listdir(folder_path):
if file_name.endswith('.mp3'):
file_path = os.path.join(folder_path, file_name)
await play_audio_file(voice_client, file_path, ctx)
return
subfolders = [f for f in os.listdir(folder_path) if os.path.isdir(os.path.join(folder_path, f))]
for subfolder in subfolders:
subfolder_path = os.path.join(folder_path, subfolder)
await play_folder_recursive(voice_client, subfolder_path, depth - 1, ctx)
@bot.command()
async def join(ctx):
if ctx.author.voice is None:
await ctx.send('Du bist in keinem Sprachkanal!')
return
channel = ctx.author.voice.channel
await channel.connect()
@bot.command()
async def play_folder(ctx, folder_name=None, depth=1):
if ctx.author.voice is None:
await ctx.send('Du bist in keinem Sprachkanal!')
return
channel = ctx.author.voice.channel
voice_client = await channel.connect()
base_folder_path = 'C:\Musik'
folders = [f for f in os.listdir(base_folder_path) if os.path.isdir(os.path.join(base_folder_path, f))]
if len(folders) == 0:
await ctx.send('Keine Unterordner gefunden!')
await voice_client.disconnect()
return
if folder_name is None:
await ctx.send('Bitte gib den Namen des Ordners an, den du abspielen möchtest.')
await voice_client.disconnect()
return
folder_path = os.path.join(base_folder_path, folder_name)
if folder_name not in folders or not os.path.isdir(folder_path):
await ctx.send('Ungültiger Ordnername!')
await voice_client.disconnect()
return
if depth > 10:
await ctx.send('Die maximale Verschachtelungstiefe beträgt 10.')
await voice_client.disconnect()
return
await play_folder_recursive(voice_client, folder_path, depth, ctx)
await voice_client.disconnect()
@bot.command()
async def stop(ctx):
if ctx.voice_client is not None:
ctx.voice_client.stop()
await ctx.voice_client.disconnect()
else:
await ctx.send('Ich bin in keinem Sprachkanal!')
@bot.command()
async def test(ctx):
await ctx.send("Ich habe Zugriff auf den Bot!")
bot.run('Your_Bot_Token')