Search code examples
pythonherokudiscorddiscord.py

Discord Py - Music commands on a heroku hosted Bot


I've been trying to host my Discord Bot online now permanently. It hasn't changed much, although all the Voice channel (Musik commands) don't work anymore.

My requirements.txt file includes: discord ,youtube_dl ,ffmpeg

On the Build log it does install them without any problems and the Bot soon goes online on the logs. For your interest: I use Github to push my code to Heroku (it's connected to 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

Above is just an example code, how one of my music commands looks like.

Here are all my imports to make it work on my local 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

I am aware that youtube_dl and ffmpegPCMAudio download and save the audio file (as in the code) in a Queue folder. But how can I realized/transfer that over to Heroku to make it executable?


Solution

  • Buildpacks

    First, you need to have 4 buildpacks on Heroku to make sure everything will work (Your APP > Settings > Buildpacks).

    And check if you have all of these in your requirements.txt:

    • pip
    • youtube_dl
    • discord.py[voice]
    • ffmpeg
    • PyNaCl

    Your Code

    Add this to your code.

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

    If it's still not working...

    If you tried everything you can give this a try:

    Make a new file called Aptfile (the same way you made file Procfile, so in CMD: echo>Aptfile. Then paste this inside:

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