【问题标题】:Discord Py - Music commands on a heroku hosted BotDiscord Py - Heroku 托管 Bot 上的音乐命令
【发布时间】:2020-12-23 04:19:29
【问题描述】:

我一直在尝试永久在线托管我的 Discord Bot。虽然所有语音频道(音乐命令)都不再工作,但它并没有太大变化。

我的 requirements.txt 文件包括: 不和谐 ,youtube_dl ,ffmpeg

在构建日志上,它会毫无问题地安装它们,并且机器人很快就会在日志上上线。 为了您的兴趣:我使用 Github 将我的代码推送到 Heroku(它已连接到 Github)。

global voice
    channel = ctx.message.author.voice.channel
    voice = get(client.voice_clients, guild=ctx.guild)

    if voice and voice.is_connected():
        await voice.move_to(channel)

    else:
        voice = await channel.connect()


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

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

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])

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

    def my_after(error):
        coro = voice.disconnect()
        for file in os.listdir("./"):
            if file.endswith(".mp3"):
                os.remove("song.mp3")
        fut = asyncio.run_coroutine_threadsafe(coro, client.loop)
        try:
            fut.result()
        except:
            pass

    voice.play(discord.FFmpegPCMAudio('song.mp3'), after=my_after)
    voice.source = discord.PCMVolumeTransformer(voice.source)
    voice.source.volume = 0.10

上面只是一个示例代码,我的一个音乐命令的样子。

这是我的所有导入,使其可以在我的本地 PC 上运行

import discord
import random
import asyncio
import youtube_dl
import os
import shutil
from discord import FFmpegPCMAudio
from discord.ext import commands
from discord.ext.commands import has_permissions, MissingPermissions
from discord.ext.commands import Bot as BotBase
from discord.utils import get

我知道 youtube_dl 和 ffmpegPCMAudio 下载音频文件(如代码中所示)并将其保存在 Queue 文件夹中。但是我怎样才能实现/将其转移到 Heroku 以使其可执行?

【问题讨论】:

    标签: python heroku discord discord.py discord.py-rewrite


    【解决方案1】:

    构建包

    首先,您需要在 Heroku 上拥有 4 个构建包,以确保一切正常 (Your APP > Settings > Buildpacks)。

    并检查您的 requirements.txt 中是否包含所有这些:

    • youtube_dl
    • discord.py[语音]
    • ffmpeg
    • PyNaCl

    您的代码

    将此添加到您的代码中。

    import ctypes
    import ctypes.util
    
    find_opus = ctypes.util.find_library('opus')
    discord.opus.load_opus(find_opus)
    

    如果还是不行...

    如果您尝试了所有方法,可以尝试一下:

    创建一个名为 Aptfile 的新文件(与创建文件 Procfile 的方法相同,因此在 CMD 中:echo>Aptfile。然后将其粘贴到里面:

    libopus0
    git
    libopus-dev
    libssl-dev
    libffi-dev
    libsodium-dev
    :repo:ppa:mc3man/xerus-media -y 
    

    【讨论】:

    • 哇,它真的适用于构建包和要求 - 非常感谢!旁注:github.com/heroku/heroku-buildpack-apt.git buildpack 不是工作所必需的(当我拥有它并删除它时它失败了,然后再次推送它)
    • 虽然我有一个跳过命令,它说:“discord.ext.commands.errors.CommandInvokeError:命令引发异常:ClientException:已经在播放音频”
    【解决方案2】:

    在 Heroku 的设置选项卡下,添加以下 buildpacks

    • https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest.git
    • https://github.com/kitcast/buildpack-ffmpeg.git
    • https://github.com/alevfalse/heroku-buildpack-ffmpeg.git
    • https://github.com/Crazycatz00/heroku-buildpack-libopus.git
    • https://github.com/guilherme-otran/heroku-buildpack-ffprobe.git

    之后,它应该看起来像这样

    重新部署你的机器人,看看它是否有效

    【讨论】:

    • 遗憾的是它没有。它安装了一些组件,但没有改变。我意识到有一个链接粘贴了两次,并且必须从图片中输入它 - 没问题。我应该从 requirements.txt 中删除 ffmpeg 吗?
    • 是的,你在 requirements.txt 中不需要 ffmpeg。我没有意识到我粘贴了两次链接,这两个链接中的一个是不同的。我现在已经对其进行了编辑,并将另一个替换为https://github.com/alevfalse/heroku-buildpack-ffmpeg.git。做同样的事情,然后重新部署,然后看看这次是否有效。
    • 不。 ffmpeg 已从 requirements.txt 中删除,并且包含所有构建包。没用
    猜你喜欢
    • 2020-11-26
    • 2021-02-21
    • 2020-05-13
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 2021-11-18
    • 2021-06-15
    相关资源
    最近更新 更多