Делаю музыкального бота, столкнулся с проблемой добавления музыки в список и последующем воспроизведении. Чтобы можно было во время проигрывания видео в боте ещё раз прописать команду play и поставить в очередь видео, которое воспроизведётся после окончания предыдущего. Мой код:
YDL_OPTIONS = {'format': 'worstaudio/best', 'noplaylist': 'False', 'simulate': 'True',
'preferredquality': '192', 'preferredcodec': 'mp3', 'key': 'FFmpegExtractAudio'}
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
client = commands.Bot( command_prefix ='!', intents = intents)
#Музыка
@client.command()
async def join(ctx):
await ctx.message.author.voice.channel.connect()
@client.command()
async def leave(ctx):
await ctx.guild.voice_client.disconnect()
@client.command()
async def play(ctx, *, arg):
if not ctx.message.author.voice:
await ctx.send('Ты не в голосовом канале')
return
try:
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
with YoutubeDL(YDL_OPTIONS) as ydl:
if 'https://' in arg:
info = ydl.extract_info(arg, download=False)
else:
info = ydl.extract_info(f"ytsearch:{arg}", download=False)['entries'][0]
url = info['formats'][0]['url']
voice.play(discord.FFmpegPCMAudio(executable ="ffmpeg\\ffmpeg.exe", source=url, **FFMPEG_OPTIONS))
info = str(info)
await ctx.send('https://www.youtube.com/watch?v='+info[8:19])
except:
await ctx.message.author.voice.channel.connect()
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
with YoutubeDL(YDL_OPTIONS) as ydl:
if 'https://' in arg:
info = ydl.extract_info(arg, download=False)
else:
info = ydl.extract_info(f"ytsearch:{arg}", download=False)['entries'][0]
url = info['formats'][0]['url']
voice.play(discord.FFmpegPCMAudio(executable="ffmpeg\\ffmpeg.exe", source=url, **FFMPEG_OPTIONS))
info = str(info)
await ctx.send('https://www.youtube.com/watch?v=' + info[8:19])