如何构建自动播放音乐机器人 Discord.py

时间:2021-06-19 16:47:40

标签: python discord.py

我想构建一个自动播放音乐机器人。我有一首歌曲,我希望机器人在当前歌曲结束后自动播放下一首歌曲。我该怎么做?

    async def play(self, ctx, input: str):
        YDL_OPTIONS = {'format': 'bestaudio', 'noplaylist': 'True'}
        voice = discord.utils.get(self.client.voice_clients, guild=ctx.guild)
        print(type(voice))
        # input = input.strip("<>")
        with YoutubeDL(YDL_OPTIONS) as ydl:
            info = ydl.extract_info(input, download=False)
        description = ""
        count = 0
        if "entries" in info.keys():
            for id, ent in enumerate(info["entries"]):
                count += 1
                self.queue.add(ent)
                description += f"{id}.  {str(ent['title'])[:70].rjust(80)}" + \
                               ("..." if len(ent['title'][70:]) else "") + \
                               f"`{ent['duration'] // 60}:{ent['duration'] % 60}`\n" if count < 4 else ""
        else:
            URL = info['formats'][0]['url']
            self.queue.add(info)
            description += f"1. {str(info['title'])[:70].rjust(80)}" + \
                           ("..." if len(info['title'][70:]) else "") + \
                           f"`{info['duration'] // 60}:{info['duration'] % 60}`\n"

        if not voice.is_playing() and re.match(URL_REGEX, input):
            voice.play(FFmpegPCMAudio(executable="ffmpeg.exe", source=self.queue._queue[self.queue.position]['url'],
                                      **Music.FFMPEG_OPTIONS))
            await ctx.send(embed=self.get_embed(ctx, self.queue._queue, [self.queue.position + 1, self.queue.length]))
        elif voice.is_playing():
            pass # some staff here

0 个答案:

没有答案