音乐机器人、!skip 命令和 Heroku 目录的问题

时间:2021-03-02 08:48:34

标签: python heroku discord.py

当我尝试使用 Visual Code Studio 调试我的机器人时,一切正常。但是,当我将所有内容上传到 Heroku 时,!skip 命令就像一个 !stop 命令。我联系了一些人,在查看代码时,他们说这与为队列创建新文件夹有关,我应该将 .mp3 文件下载到主目录中。唯一的问题是我已经尝试在我的代码中多次更改目录,但它根本不起作用,或者当我尝试 !skip 命令时它仍然完全停止音乐。有谁知道如何解决这个问题?

这是我尝试使用 ./Queue 和 Queue 目录更新的 !play 和 !queue 命令。

任何帮助将不胜感激!

async def play(ctx, url: str):

    def check_queue():
        Queue_infile = os.path.isdir("./Queue")
        if Queue_infile is True:
            DIR = os.path.abspath(os.path.realpath("Queue"))
            length = len(os.listdir(DIR))
            try:
                first_file = os.listdir(DIR)[0]
            except:
                queues.clear()
                return
            main_location = os.path.dirname(os.path.realpath(__file__))
            song_path = os.path.abspath(os.path.realpath("Queue") + "\\" + first_file)
            if length != 0:
                song_there = os.path.isfile("song.mp3")
                if song_there:
                    os.remove("song.mp3")
                shutil.move(song_path, main_location)
                for file in os.listdir("./"):
                    if file.endswith("mp3"):
                        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 = 1.00

            else:
                queues.clear()
                return
        else:
            queues.clear()

    song_there = os.path.isfile("song.mp3")
    try:
        if song_there:
            os.remove("song.mp3")
    except PermissionError:
        return

    Queue_infile = os.path.isdir("./Queue")
    try:
        Queue_folder = "./Queue"
        if Queue_infile is True:
            shutil.rmtree(Queue_folder)
    except:
        print("No old Queue folder")

    voice = get(client.voice_clients, guild=ctx.guild)

    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }
    
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        get_info = ydl.extract_info(url, download=False)
        if get_info["duration"] >= 630:
            await ctx.send("Sorry, this video is too long. Please try sending a shorter video.")
            return False

        ydl.download([url])

    for file in os.listdir("./"):
        if file.endswith(".mp3"):
            name = 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 = 1.00

    nname = name.rsplit("-", 2)
    await ctx.send(f"Now playing: {nname[0]}")
@client.command()
async def queue(ctx, url: str):
    Queue_infile = os.path.isdir("./Queue")
    if Queue_infile is False:
        os.mkdir("Queue")
    DIR = os.path.abspath(os.path.realpath("Queue"))
    q_num = len(os.listdir(DIR))
    q_num += 1
    add_queue = True
    while add_queue:
        if q_num in queues:
            q_num +=1
        else:
            add_queue = False
            queues[q_num] = q_num

    queue_path = os.path.abspath(os.path.realpath("Queue") + f"\\song{q_num}.%(ext)s")

    ydl_opts = {
        'format': 'bestaudio/best',
        'outtmpl': queue_path,
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        get_info = ydl.extract_info(url, download=False)
        if get_info["duration"] >= 630:
            await ctx.send("Sorry, this video is too long. Please try sending a shorter video.")
            return False

        ydl.download([url])

1 个答案:

答案 0 :(得分:0)

是的!你想去以下

教程:https://www.youtube.com/watch?v=f3wsxbMbi5M

我用过这个,它对我有用!您仍然需要做一个 Procfile 并更改工作和令牌!部署时它应该会自动检测到它是 python!

本教程还向您展示了如何将 ffmpeg 安装到您的 Heroku 服务器,以便机器人可以运行音乐命令!

用 Heroku 的方式做的另一点是不可靠,而 Github 的方式更可靠并且每次都有效。