Discord.py重写音乐机器人排队

时间:2019-11-26 11:32:13

标签: python python-3.x python-asyncio youtube-dl discord.py-rewrite

我正在使用discord.py rewrite在音乐机器人上解决不和谐问题,并创建了一个词典队列,该队列添加了带有服务器ID的新条目,但是我不确定如何在当前歌曲播放时播放下一个词典条目完成播放。

我还使play命令调用queueNext()函数从队列中删除当前正在播放的歌曲,因此下一首歌曲是第一首条目,但是现在该代码没有方法播放队列中的下一首歌曲,下面是代码:

# Music Bot
ytdl_options = {
    'format': 'bestaudio/best',
    'restrictfilenames': True,
    'noplaylist': True,
    'nocheckcertificate': True,
    'quiet': True,
    'ignoreerrors': False,
    'logtostderr': False,
    'no_warnings': True,
    'default_search': 'auto',
    'source_address': '0.0.0.0'
    }
bot.queue = {}
bot.vc = None
async def queueNext(srv):
    if bot.queue[srv]:
        bot.queue[srv].pop(0)
    else:
        bot.vc.disconnect()
@bot.command()
async def play(ctx, *, url: str = None):
    if not url:
        await ctx.send('Please specify a title or URL for me to play.')
        return
    voiceChannel = ctx.author.voice.channel
    if ctx.voice_client is not None:
        await ctx.voice_client.move_to(voiceChannel)
    elif ctx.author.voice and ctx.author.voice.channel:
        await voiceChannel.connect()
    bot.vc = ctx.voice_client
    ytdl = youtube_dl.YoutubeDL(ytdl_options)
    loop = asyncio.get_event_loop()
    data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url))
    if 'entries' in data:
        data = data['entries'][0]
    server = ctx.guild.id
    if server in bot.queue:
        bot.queue[server].append(data)
    else:
        bot.queue[server] = [data]
    await ctx.send(data.get('title') + ' added to the queue.')
    if not bot.vc.is_playing() if bot.vc else True:
        source = ytdl.prepare_filename(bot.queue[server][0])
        bot.vc.play(discord.FFmpegPCMAudio(source, options='-vn'), after=await queueNext(server))
        await ctx.send('Now playing: ' + data.get('title'))

任何帮助将不胜感激,即使只是帮助逻辑地解释使它按预期工作也必须做些什么。

0 个答案:

没有答案