<i id='WTxkA'><tr id='WTxkA'><dt id='WTxkA'><q id='WTxkA'><span id='WTxkA'><b id='WTxkA'><form id='WTxkA'><ins id='WTxkA'></ins><ul id='WTxkA'></ul><sub id='WTxkA'></sub></form><legend id='WTxkA'></legend><bdo id='WTxkA'><pre id='WTxkA'><center id='WTxkA'></center></pre></bdo></b><th id='WTxkA'></th></span></q></dt></tr></i><div id='WTxkA'><tfoot id='WTxkA'></tfoot><dl id='WTxkA'><fieldset id='WTxkA'></fieldset></dl></div>
    <tfoot id='WTxkA'></tfoot>
    <legend id='WTxkA'><style id='WTxkA'><dir id='WTxkA'><q id='WTxkA'></q></dir></style></legend>

        <bdo id='WTxkA'></bdo><ul id='WTxkA'></ul>

    1. <small id='WTxkA'></small><noframes id='WTxkA'>

    2. 首次完成 Discord Bot 后如何播放下一首歌曲

      时间:2023-09-09

            <tbody id='X1263'></tbody>

            • <bdo id='X1263'></bdo><ul id='X1263'></ul>
              <tfoot id='X1263'></tfoot>
            • <small id='X1263'></small><noframes id='X1263'>

            • <legend id='X1263'><style id='X1263'><dir id='X1263'><q id='X1263'></q></dir></style></legend>

              <i id='X1263'><tr id='X1263'><dt id='X1263'><q id='X1263'><span id='X1263'><b id='X1263'><form id='X1263'><ins id='X1263'></ins><ul id='X1263'></ul><sub id='X1263'></sub></form><legend id='X1263'></legend><bdo id='X1263'><pre id='X1263'><center id='X1263'></center></pre></bdo></b><th id='X1263'></th></span></q></dt></tr></i><div id='X1263'><tfoot id='X1263'></tfoot><dl id='X1263'><fieldset id='X1263'></fieldset></dl></div>
                本文介绍了首次完成 Discord Bot 后如何播放下一首歌曲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试使用 discord.py 创建一个 Discord 音乐机器人,我是 Python 新手.我不知道如何让 Bot 自动播放下一首歌曲.我尝试了很多不同的东西.这是我当前播放一首歌曲的代码:

                I'm trying to create a Discord music Bot with discord.py, I'm new to Python. I don't know how to let the Bot automatically play the next song. I tried many different things. This is my current code for playing one song:

                vc.play(discord.FFmpegPCMAudio(executable="C:/FFmpeg/bin/ffmpeg.exe", source=sound + ".mp3"))
                await message.channel.send("Spiele nun " + str(sound) +"weiter")
                

                使用上面的代码我没有遇到问题.

                With the above code I didn't get a problem.

                推荐答案

                您应该使用 FFmpegPCMAudio 函数的 after 参数.此参数接受一个可调用对象作为参数,并在当前歌曲结束时执行它.

                You should use the after parameter of the FFmpegPCMAudio function. This parameter takes a callable as an argument and executes it when the current song is over.

                • 首先,您必须创建一个歌曲队列变量:
                song_queue = []
                

                • 然后,您必须更改 play() 函数:
                • source = sound + '.mp3'
                  soung_queue.append(source)
                  if not vc.is_playing():
                      vc.play(discord.FFmpegPCMAudio(source=source, after=lambda e: play_next(ctx))
                      await ctx.send("Now playing...")
                  else:
                      await ctx.send('Song queued')
                  

                  • 最后,你必须创建一个 play_next() 函数:
                  • import asyncio
                    
                    def play_next(ctx, source):
                        if len(self.song_queue) >= 1:
                            del self.song_queue[0]
                            vc = get(self.bot.voice_clients, guild=ctx.guild)
                            vc.play(discord.FFmpegPCMAudio(source=source, after=lambda e: play_next(ctx))
                            asyncio.run_coroutine_threadsafe(ctx.send("No more songs in queue."))
                    

                    您可以随意命名 play_next() 函数.
                    如果您希望您的机器人因为它播放声音的时间太长而断开连接,请将 play_next() 函数更改为:

                    You can name the play_next() function whatever you want.
                    If you want your bot to disconnect because it's been too long since it was playing sound, change the play_next() function to :

                    import asyncio
                    
                    def play_next(ctx, source):
                        vc = get(self.bot.voice_clients, guild=ctx.guild)
                        if len(self.song_queue) >= 1:
                            del self.song_queue[0]
                            vc.play(discord.FFmpegPCMAudio(source=source, after=lambda e: play_next(ctx))
                        else:
                            asyncio.sleep(90) #wait 1 minute and 30 seconds
                            if not vc.is_playing():
                                asyncio.run_coroutine_threadsafe(vc.disconnect(ctx), self.bot.loop)
                                asyncio.run_coroutine_threadsafe(ctx.send("No more songs in queue."))
                                
                    

                    这篇关于首次完成 Discord Bot 后如何播放下一首歌曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何在 discord.py cogs 中创建别名? 下一篇:如何在 on_member_join() discord.py 中向成员发送私人消

                相关文章

                最新文章

                  <bdo id='etdsz'></bdo><ul id='etdsz'></ul>
              • <tfoot id='etdsz'></tfoot>

              • <legend id='etdsz'><style id='etdsz'><dir id='etdsz'><q id='etdsz'></q></dir></style></legend>
                <i id='etdsz'><tr id='etdsz'><dt id='etdsz'><q id='etdsz'><span id='etdsz'><b id='etdsz'><form id='etdsz'><ins id='etdsz'></ins><ul id='etdsz'></ul><sub id='etdsz'></sub></form><legend id='etdsz'></legend><bdo id='etdsz'><pre id='etdsz'><center id='etdsz'></center></pre></bdo></b><th id='etdsz'></th></span></q></dt></tr></i><div id='etdsz'><tfoot id='etdsz'></tfoot><dl id='etdsz'><fieldset id='etdsz'></fieldset></dl></div>

                    <small id='etdsz'></small><noframes id='etdsz'>