Discord Music Bot 队列功能(它跳过中间曲目并跳转到最后一首曲目)

问题描述 投票:0回答:0

我是新来的。请在这个著名的平台上指导我 XD

我正在使用 Python (discord.py) 创建一个 Discord 音乐机器人,我想添加一个队列功能。

请注意,斜线命令使用“interaction”而不是“ctx”。

首先,我决定修改 /play 命令,添加 global list queue = [],然后当用户 /play 查询时,查询将被追加到队列中。然后,我想让它播放。

代码如下所示:

await interaction.response.defer()
    source = await YTDLSource.from_url(query, loop=client.loop)
    queue.append(source)
    print(queue)
    description = "**{}**\n{}\nRequested by: {}".format(queue[-1], queue[-1].data['webpage_url'], interaction.user.mention)
    embed = discord.Embed(title="Added to queue", description=description, color=0x23EBE3)
    embed.set_thumbnail(url="THUMBNAIL_URL")
    await interaction.followup.send(embed=embed)

    async def play_next():
        if len(queue) > 1:
            queue.pop(0)
            print(queue)
            voice_client.play(queue[0])
            description = "**{}**\n{}\nRequested by: {}".format(queue[0], queue[0].data['webpage_url'], interaction.user.mention)
            embed = discord.Embed(title="Now playing", description=description, color=0x23EBE3)
            embed.set_thumbnail(url="THUMBNAIL_URL")
            await interaction.followup.send(embed=embed)
    
    if not voice_client.is_playing():
        voice_client.play(queue[0])
        description = "**{}**\n{}\nRequested by: {}".format(queue[0], queue[0].data['webpage_url'], interaction.user.mention)
        embed = discord.Embed(title="Now playing", description=description, color=0x23EBE3)
        embed.set_thumbnail(url="THUMBNAIL_URL")
        await interaction.followup.send(embed=embed)
    elif voice_client.is_playing():
        while voice_client.is_playing():
            await asyncio.sleep(1)
        await play_next()

我的问题是最后四行:

elif voice_client.is_playing():
        while voice_client.is_playing():
            await asyncio.sleep(1)
        await play_next()

将添加并播放第一首曲目。在第一首曲目之后,将播放第二首曲目。这很容易编写代码。但是,当添加第三个、第四个等等时,这就成了问题。 最后四行表示他们将等待第一首歌曲结束,然后他们将同时尝试播放而不是随后播放。

我的逻辑不好,我知道。如果你不能帮助编码,至少指导我一个工作流程。非常感谢您!

我试过改变

voice_client.play(queue[0])

进入

voice_client.play(queue[0], after=lambda e: play_next())

例如,但我最终把自己弄糊涂了。也许我需要一个更好的工作流程来可视化它。

python discord discord.py logic audio-streaming
© www.soinside.com 2019 - 2024. All rights reserved.