cronokirby / alchemy

A discord library for Elixir
MIT License
152 stars 34 forks source link

If the voice gateway connection is terminated unintentionally, alchemy goes into a sort of "limbo" where the bot is connected to a voice channel but not connected to the voice gateway. #85

Open dongmaster opened 4 years ago

dongmaster commented 4 years ago

Sorry for possibly confusing title.

If you let a bot be connected to a voice channel without doing anything in there for quite a few hours (+12 hours possibly), then eventually, the connection to the voice gateway will be terminated for some reason or the other, such as the connection being closed, timing out, or receiving a 503 error code. Example: 07:24:13.970 [debug] Voice Gateway for guild_id disconnected, reason: {:remote, :closed}

https://github.com/cronokirby/alchemy/blob/master/lib/Voice/gateway.ex#L78-L85 This ondisconnect handler doesn't seem to properly handle this scenario, and causes the bot to fall into a sort of "limbo" where you can make the bot join different voice channels, but there's no real connection to the voice gateway anymore, thus resulting in not being able to play any audio in the voice channel. In this limbo state, the bot is visibly connected to a voice channel in the discord client.

How to reproduce the problem:

  1. Start up a discord bot in a virtual machine (or your real machine if you don't mind unplugging your ethernet cable)
  2. Make the bot connect to a voice channel.
  3. Unplug the virtual network adapter for the virtual machine so that the machine loses internet connection.
  4. After 40 seconds after killing the internet connection for the virtual machine, turn it on again by plugging in the virtual network adapter.
  5. You should now see something like this message in your terminal from the bot: 07:24:13.970 [debug] Voice Gateway for guild_id disconnected, reason: {:remote, :closed}
  6. Try to make the bot disconnect from the voice channel. It doesn't work. Error message: {:error, "You're not joined to voice in this guild"}
  7. Try to make the bot join another voice channel. It visibly switches the bot to the other channel in the discord client, but no connection to the voice gateway is made. Error message: {:error, "Timed out"}
  8. Try to play audio: Alchemy.Voice.play_url("guild_id", "https://www.youtube.com/watch?v=5fi3VpFrJP0"). It doesn't work. Error message: {:error, "You're not joined to voice in this guild"}
cronokirby commented 4 years ago

Let's see if we can reproduce this after #87 is fixed...

dongmaster commented 4 years ago

I've done some changes to my own fork of Alchemy to see if this issue has been fixed. See this commit for what I changed (it's not much): https://github.com/dongmaster/alchemy/commit/e5fc09541d085fbb03cdd060db2adcbdf5db986c

I'll make a pull request in a few days when I see that the change has fixed it. You could also implement the changes yourself if you want to fix it earlier.