0

I created a simple Music Bot, but there is one problem. When I run the "?play" Command, the Youtube Link is actually playing. Also, if I run the "?play" Command a second time, the link has been safed in the array. So when I run the "?skip" Command, the next YouTube Link has been shown, but if I do the same thing, just without the "?skip" Command, the Bot won't play the new Link automatically, after the first Link ended. Can someone help me please?

import discord
import asyncio
from discord.ext import commands
import youtube_dl

client = commands.Bot(intents=discord.Intents.all(), command_prefix="?", help_command=None)

voice_clients = {}

yt_dl_opts = {'format': 'bestaudio/best'}
ytdl = youtube_dl.YoutubeDL(yt_dl_opts)

ffmpeg_options = {'options': "-vn"}


queue = []


async def play_next(guild_id):
    try:
        url = queue[0]
        loop = asyncio.get_event_loop()
        data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=False))
        song = data['url']
        player = discord.FFmpegPCMAudio(song, **ffmpeg_options)
        voice_clients[guild_id].play(player)
        asyncio.ensure_future(play_next(guild_id))
    except Exception as err:
        print(err)

@client.command()
async def play(ctx, url: str = None):
    print(queue)
    if url == None:
        await ctx.send("Ich brauche schon irgend ein Link")
        return
    try:
        voice_client = await ctx.author.voice.channel.connect() 
        voice_clients[voice_client.guild.id] = voice_client
    except:
        print("Bot ist schon im Voice")
    url = url
    queue.append(url)
    if len(queue) == 1:
        await play_next(ctx.guild.id)
    else:
        await ctx.send("Dein Link wurde erfolgreich in die Warteschlange hinzugefügt!")
        print(queue)
    
@client.command()
async def skip(ctx):
    try:
        voice_clients[ctx.guild.id].stop()
    except Exception as err:
        print(err)
    queue.pop(0)
    if len(queue) >= 1:
        await play_next(ctx.guild.id)
    else:
        await ctx.reply("Es gibt nix abzuspielen")

0 Answers0