结合播放和排队?

时间:2019-10-12 14:42:58

标签: discord.py

我正在为不和谐创建音乐机器人,并希望在播放功能中添加队列功能,以便可以使用play命令来预下载歌曲,然后也将歌曲排队。 问题是我不知道如何组合它们。 我同时提供了队列功能和播放功能

    @commands.command(aliases=["p"])
    async def play(self, ctx, url: str):
        """Plays from URL."""

        def check_queue():
            queue_dir = os.path.isdir("./queues")
            if queue_dir is True:
                directory = os.path.abspath(os.path.realpath("queues"))
                length = len(os.listdir(directory))
                inqueue = length - 1
                try:
                    fsong = os.listdir(directory)[0]
                except:
                    queues.clear()
                    return
                direct = os.path.dirname(os.path.realpath(__file__))
                song_path = os.path.abspath(os.path.realpath("queue") + "\\" + fsong)
                if length != 0:
                    song_exist = os.path.isfile("song.mp3")
                    if song_exist:
                        os.remove("song.mp3")
                    shutil.move(song_path, direct)
                    for file in os.listdir("./"):
                        if file.endswith(".mp3"):
                            os.rename(file, 'song.mp3')
                else:
                    queues.clear()
                    return


        song_exist = os.path.isfile("song.mp3")
        try:
            if song_exist:
                os.remove("song.mp3")
         except PermissionError:
             return
        await ctx.send("Downloading...")


        channel = ctx.message.author.voice.channel
        voice = get(self.bot.voice_clients, guild = ctx.guild)
        if voice and voice.is_connected():
            await voice.move_to(channel)
        else:
            voice = await channel.connect()
        ytdl_options = {
            'format': 'bestaudio/best',
            'postprocessors': [{
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'mp3',
                'preferredquality': '192',
            }],
        }

        with youtube_dl.YoutubeDL(ytdl_options) as ytdl:
            ytdl.download([url])

        for file in os.listdir("./"):
            if file.endswith(".mp3"):
                filename = file
                os.rename(file, "song.mp3")

        voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e: check_queue())
        voice.source = discord.PCMVolumeTransformer(voice.source)
        voice.source.volume = 0.07

        video = filename.rsplit("-", 2)
        embed = discord.Embed(color=0x21d3f3)
        embed.description = f":musical_note: Now playing: **{video[0]}**!"
        await ctx.send(embed=embed)

    queues = {}

    @commands.command(aliases=["q"])
    async def queue(self, ctx, url: str):
        """Queues"""
        queue_dir = os.path.isdir("./queues")
        if queue_dir is False:
            os.mkdir("queues")
        directory = os.path.abspath(os.path.realpath("queues"))
        queue_num = len(os.listdir(directory))
        queue_num += 1
        add_queue = True
        while add_queue:
            if queue_num in queues:
                queue_num += 1
            else:
                add_queue = False
                queues[queue_num] = queue_num 
        q_path = os.path.abspath(os.path.realpath("queues") + f"\song{queue_num}.%s(ext)s")

        ytdl_options = {
            'format': 'bestaudio/best',
            'postprocessors': [{
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'mp3',
                'preferredquality': '192',
            }],
        }
        with youtube_dl.YoutubeDL(ytdl_options) as ytdl:
            ytdl.download([url])

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

0 个答案:

没有答案