Discord Py - heroku 托管 Bot 上的音乐命令

Discord Py - Music commands on a heroku hosted Bot

我一直在尝试永久在线托管我的 Discord Bot。它没有太大变化,尽管所有语音通道(音乐命令)都不再起作用。

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

在构建日志上,它确实安装了它们,没有任何问题,并且 Bot 很快就会在日志上联机。 为了您的兴趣:我使用 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 文件夹中。但是我怎样才能 realized/transfer 将其交给 Heroku 使其可执行呢?

在 Heroku 的设置选项卡下,添加以下构建包

  • 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

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

Re-deploy 你的机器人,看看它是否有效

构建包

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

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

  • 点子
  • youtube_dl
  • discord.py[语音]
  • ffmpeg
  • 氯化钠

你的代码

将此添加到您的代码中。

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