как сделать очередь внутри команды play
мне нужно, чтобы бы при повторном написании команды play видео ролик попадал в очередь и воспроизводился следующим, я пробовал несколько своих способов, но пока ни 1 не решил мою проблему.
вот код:
@client.command()
async def join(ctx):
if not ctx.message.author.voice:
await ctx.send(f"{ctx.author.mention}, you not connected to a voice channel")
return
else:
channel = ctx.message.author.voice.channel
await channel.connect()
@client.command()
async def leave(ctx):
voice_client = ctx.message.guild.voice_client
if voice_client.is_connected():
await voice_client.disconnect()
else:
await ctx.send("The bot is not connected to a voice channel.")
@client.command()
async def play(ctx, url):
try:
with youtube_dl.YoutubeDL(ytdl_format_options) as ydl:
info = ydl.extract_info(url, download=False)
URL = info['formats'][0]['url']
server = ctx.message.guild
voice = server.voice_client
voice.play(discord.FFmpegPCMAudio(URL, **ffmpeg_options))
except:
await ctx.send("The bot is not connected to a voice channel.")
Ответы (1 шт):
Автор решения: Universall
→ Ссылка
Примерно накидал. Это основа, на которую потом можно навешать всяких функций и т.д:
import threading
import time
import discord
import config
from discord import FFmpegPCMAudio
class Player(threading.Thread):
def __init__(self, connection: discord.VoiceClient) -> None:
super(Player, self).__init__()
self.connection = connection
self.active: bool = True
def _play(self, audio_source: str, loop: bool = False) -> None:
source = FFmpegPCMAudio(audio_source, executable=config.EXECUTABLE_FFMPEG, **config.FFMPEG_OPTIONS)
while True:
if self.connection.is_connected():
self.connection.play(source)
else:
self.active = False
return
while self.connection.is_playing():
if not self.active:
return
time.sleep(1)
if loop:
continue
break
def run(self) -> None:
while self.active:
with YoutubeDL(config.YDL_OPTIONS) as ydl:
audio_source = ydl.extract_info("(ссылка на видео)", download=False)["formats"][0]["url"]
self._play(audio_source)
def close(self) -> None:
self.active = False
async def main(author: discord.Member):
voice_channel = author.voice.channel
connection = await voice_channel.connect()
player = Player(connection)
player.start()