Это моя команда play и функции play_next.
Команда play добавляет ссылку в очередь, а play_next должен ждать, пока текущая песня не завершит воспроизведение, а затем воспроизвести следующую песню и повторить.
/>Если у вас есть предложения, оставьте комментарий ниже
async def play_next(self, ctx):
if len(self.queues) > 0:
voice_client = self.voice_clients.get(ctx.guild.id)
voice_client.is_playing = True
#get the first url
m_url = self.queues[0]
#remove the first element as you are currently playing it
self.queues.pop(0)
loop = asyncio.get_event_loop()
data = await loop.run_in_executor(None, lambda: self.ytdl.extract_info(m_url, download=False))
song = data['url']
voice_client.play(nextcord.FFmpegOpusAudio(song, executable= "ffmpeg.exe", **self.ffmpeg_options), after=lambda e: asyncio.run_coroutine_threadsafe(self.play_next(), self.client.loop))
else:
self.voice_client.is_playing = False
@commands.command(aliases=['p'])
async def play(self, ctx, *, link):
if ctx.author.voice is None:
return await ctx.send("You need to be in a voice channel to play music!")
if ctx.guild.voice_client is None:
voice_client = await ctx.author.voice.channel.connect()
self.voice_clients[ctx.guild.id] = voice_client
else:
voice_client = self.voice_clients.get(ctx.guild.id)
if "www.youtube.com" not in link:
query_string = urllib.parse.urlencode({'search_query': link})
content = urllib.request.urlopen(self.youtube_results_url + query_string)
search_results = re.findall(r'/watch\?v=(.{11})', content.read().decode())
if not search_results:
return await ctx.send(f"`Unable to find {link}`")
link = self.youtube_watch_url + search_results[0]
ME = await ctx.send(f'`Initializing....`')
data = await self.client.loop.run_in_executor(None, lambda: self.ytdl.extract_info(link, download=False))
if data:
song = data['url']
songtitle = data['title']
#check = GetQueue(ctx.guild.id)
check2 = self.queues
# if check: #queue not empty
# UpdateQueue(ctx.guild.id, link)
# await ME.edit(f'`{songtitle}, added to queue by: {ctx.author.name}`')
if not check2:
#queue is empty
player = nextcord.FFmpegOpusAudio(song, **self.ffmpeg_options)
voice_client.play(player, after=lambda e: asyncio.run_coroutine_threadsafe(self.play_next(ctx), self.client.loop))
#UpdateQueue(ctx.guild.id, link)
self.queues.append(link)
# print(self.queues)
await ME.edit(f'`Now playing {songtitle}, requested by: {ctx.author.name}`')
return
return await ME.edit(f'`Only one song can be played at a time`')
Подробнее здесь: https://stackoverflow.com/questions/784 ... play-queue
Музыкальный бот nextcord.py, как играть в очередь ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Музыкальный бот Discord, когда я пропускаю трек, бот пропускает сразу 2 трека
Anonymous » » в форуме Python - 0 Ответы
- 38 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Как текущая очередь, очередь отправки и целевая очередь взаимодействуют друг с другом в GCD?
Anonymous » » в форуме IOS - 0 Ответы
- 102 Просмотры
-
Последнее сообщение Anonymous
-