提问者:小点点

当第一首歌完成不和谐机器人时,如何在队列中播放下一首歌


我想让我的不和谐音乐机器人能够在第一首歌结束后立即播放下一首排队的歌曲。有什么方法可以做到这一点吗?

这是我的播放功能

queue = []

@client.command(name='play',help ='Play a song',aliases=['plays', 'p'])
async def play(ctx, url):
    global queue
    server = ctx.message.guild
    voice_channel = server.voice_client
    queue.append(url)
    async with ctx.typing():
        player = await YTDLSource.from_url(queue[0], loop=client.loop)
        voice_channel.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
        del(queue[0])
        await ctx.send(f'**Now playing:** {player.title}')

共1个答案

匿名用户

这不是最好的解决办法,但应该行得通。

queues = {}  # Dictionary with queues for each server


def queue(ctx, id):
    if len(queues) > 0 and queues[id] != []:
        voice = ctx.guild.voice_client
        audio = queues[id].pop(0)
        voice.play(audio, after=lambda x=None: queue(ctx, ctx.message.guild.id))


@client.command(name='play',help ='Play a song',aliases=['plays', 'p'])
async def play(ctx, url):
    server = ctx.message.guild
    guild_id = ctx.message.guild.id
    voice = get(bot.voice_clients, guild=ctx.guild)
    audio = await YTDLSource.from_url(url, loop=client.loop)
    if not voice.is_playing():
        async with ctx.typing():
            voice.play(audio, after=lambda x=None: queue(ctx, guild_id))
            voice.is_playing()
            await ctx.send(f'**Now playing:** {audio.title}')
    else:
        if guild_id in queues:
            queues[guild_id].append(audio)
        else:
            queues[guild_id] = [audio]

        await ctx.send("Added to queue.")

或以队列为列表:

queue = []


def queued(ctx):
    if len(queue) > 0:
        voice = ctx.guild.voice_client
        audio = queue.pop(0)
        voice.play(audio, after=lambda x=None: queued(ctx, ctx.message.guild.id))


@client.command(name='play',help ='Play a song',aliases=['plays', 'p'])
async def play(ctx, url):
    server = ctx.message.guild
    voice = get(bot.voice_clients, guild=ctx.guild)
    if not voice.is_playing():
        async with ctx.typing():
            audio = await YTDLSource.from_url(url, loop=client.loop)
            voice.play(audio, after=lambda x=None: queued(ctx))
            voice.is_playing()
            await ctx.send(f'**Now playing:** {audio.title}')
    else:
        queue.append(audio)