【问题标题】:How to make Discord.py music bot automatically play next song in queue如何让 Discord.py 音乐机器人自动播放队列中的下一首歌曲
【发布时间】:2021-10-10 17:03:33
【问题描述】:

歌曲结束后,歌曲会从播放列表队列中弹出,但不会继续播放队列中的下一首歌曲。所以队列命令只显示那里的歌曲。我应该如何更改它以运行下一首歌曲?

playlist = []

@client.command(name='play', help='Plays music')
async def play(ctx):
    voice = ctx.message.guild.voice_client

    def is_connected():  # Tests if bot is connected to voice channel
        voice_client = discord.utils.get(ctx.bot.voice_clients, guild=ctx.guild)
        return voice_client and voice_client.is_connected()

    url = ctx.message.content.lstrip('?play')
    playlist.append(url.lstrip(' '))

    if not ctx.message.author.voice:
        await ctx.send("You are not connected to a voice channel")
        playlist.pop(0)
        return
    else:
        channel = ctx.message.author.voice.channel

    if is_connected():
        if voice.is_playing():  # url already added to the playlist, downloads each index
            index = len(playlist)-1
            player = await YTDLSource.from_url(playlist[index], loop=client.loop)
            await ctx.send('**Added:** {} to queue'.format(player.title))
            playlist[index] = player.title
        else:
            server = ctx.message.guild
            voice_channel = server.voice_client
            async with ctx.typing():
                player = await YTDLSource.from_url(playlist[0], loop=client.loop)
                voice_channel.play(player, after=lambda e: print('Player error: %s' % e) if e else ctx.send('hi'))
                print(player.title)
            await ctx.send('**Now playing:** {}'.format(player.title))
            playlist[0] = player.title
            playlist.pop(0)
    else:
        await channel.connect()
        server = ctx.message.guild
        voice_channel = server.voice_client
        async with ctx.typing():
            player = await YTDLSource.from_url(playlist[0], loop=client.loop)
            voice_channel.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
            print(player.title)
        await ctx.send('**Now playing:** {}'.format(player.title))
        playlist[0] = player.title```

【问题讨论】:

    标签: python discord audio-player


    【解决方案1】:
            await channel.connect()
            server = ctx.message.guild
            voice_channel = server.voice_client
            async with ctx.typing():
                player = await YTDLSource.from_url(playlist[0], loop=client.loop)
                voice_channel.play(player, after=lambda e: asyncio.run_coroutine_threadsafe(after_play(ctx), client.loop))
                print(player.title)
            await ctx.send('**Now playing:** {}'.format(player.title))
            playlist[0] = player.title
    
    
    async def after_play(ctx):
        playlist.pop(0)
        server = ctx.message.guild
        voice_channel = server.voice_client
        player = await YTDLSource.from_url(playlist[0], loop=client.loop)
        voice_channel.play(player, after=lambda e: asyncio.run_coroutine_threadsafe(after_play(ctx), client.loop))
        print(player.title)```
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    • 感谢兄弟它为我工作!
    【解决方案2】:

    对于与音乐相关的内容,您必须设置的不仅仅是命令(播放、跳过、断开连接等)。 请查看有关音乐机器人和东西的示例。 我有一个,但它很旧,但逻辑是你需要的。 https://github.com/Gaming-Rowdies/cs-music/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-22
      • 2021-05-05
      • 1970-01-01
      • 2021-12-10
      • 2019-05-05
      • 1970-01-01
      • 2021-05-24
      相关资源
      最近更新 更多