1
votes

So I wrote a Discord bot in Python and added voice support to it a few months ago. I am currently hosting on Heroku and things worked when I originally wrote the bot using an Opus buildpack for Heroku. However currently when running the bot on Heroku the bot will connect to the voice channel and then the bot will think it still is not connected even though it is clearly visibly connected in the Discord UI.

I have already tried using different Opus buildpacks to no avail. The one I'm currently using is this one: https://elements.heroku.com/buildpacks/xrisk/heroku-opus

I am also using the latest version of the following libraries

discord.py[voice]
beautifulsoup4
bs4
aiohttp
Pillow
aiofiles
imageio
youtube_dl

This is the command for only connecting the bot, and there is a duplicate of this in the code used to play the audio itself. The same behavior is observed in both instances.

@commands.command(pass_context=True, aliases=["disconnect"])
    async def join(self, ctx):
        can_send = await check_can_use(ctx, "join")
        if not can_send:
            return

        global voice
        try:
            channel = ctx.message.author.voice.channel
            voice = get(self.bot.voice_clients, guild=ctx.guild)

            # Connect bot to voice channel
            if voice and voice.is_connected():
                await voice.move_to(channel)
            else:
                voice = await channel.connect()
                print(f"Connected to {channel}\n")
            await ctx.send(f"**Connected to {channel}**")
        except Exception as e:
            print(e)
            print("Must be in voice channel")
            await ctx.send("**Must be in voice channel**")

When running on my computer locally the bot connects properly and sends the message that it has connected to the server. When running on Heroku the bot just stops when connecting to the voice chat.

The line:

    print(f"Connected to {channel}\n")  

is never run. The bot hangs after running

    voice = await channel.connect()

I am able to print the voice object so I do know that

    voice = get(self.bot.voice_clients, guild=ctx.guild)

is working as intended.

The exception also does not run until the bot is terminated. The output of which can be seen here https://pastebin.com/y97Ggvr0.

1
Ignore the alias, it appears I added something by mistake. Unrelated to the error at handCybrNight

1 Answers

0
votes

So it appears that the hanging issue was something noticed on discord.py 1.2.3 and a few versions between when I last updated and 1.2.3. Heroku was also not installing 1.2.4, but instead installing 1.2.3 causing the issue. Forcing 1.2.4 in my requirements.txt has solved the problem.

If anyone else encounters this make sure you are using the 1.2.4 version of discord.py[voice]