discord-jda / JDA

Java wrapper for the popular chat & VOIP service: Discord https://discord.com
Apache License 2.0
4.26k stars 730 forks source link

Bot is unable to stream audio on stage in certain scenarios #2656

Closed rxsto closed 4 months ago

rxsto commented 4 months ago

General Troubleshooting

Version of JDA

5.0.0-beta.22

Expected Behaviour

When joining a stage, the bot should be able to stream audio in all cases.

The original discussion about this behavior started in a help post on the JDA Discord server: https://discord.com/channels/125227483518861312/1229423364385538150/1229423364385538150

While building the reproduction repo we were able to further define the underlying issue, as this only happens in very specific cases.

(Scenario 1) The bot is unable to send audio to the stage channel, when these steps are followed:

(Scenario 2) This issue can be resolved by using following workaround:

(Scenario 3) The bot is able to steam audio in an active stage, as long as it has started it itself:

When disconnecting and killing the bot application (as in restarting) whilst it was able to properly stream audio to the channel as a stage moderator, and getting it to play audio again via !play, it will fail again. This might as well be related to the same issue explained in the workaround from Scenario 2.

Code Example for Reproduction Steps

https://github.com/rxsto/jda-stage-repro

Code for JDABuilder or DefaultShardManagerBuilder used

https://github.com/rxsto/jda-stage-repro

Exception or Error

N/A
MinnDevelopment commented 4 months ago

Could this be a race condition issue on discord's end? It seems like simply delaying the request to speak a bit solves the problem for me.

rxsto commented 4 months ago

Interesting catch! And yes indeed, delaying the request to speak seems to solve this problem, at least sometimes. The durations I tested delaying the request to speak with seemed quite arbitrary, since sometimes the exact same amount of delay failed to mitigate the issue, and at other times it worked perfectly fine.

Using 500 milliseconds as the delay seems to be quite consistent though.

Regardless, we're pretty unsure as to how to figure out, if this problem could potentially be on Discord's end.

MinnDevelopment commented 4 months ago

It seems like discord has issues if you send it before the audio gateway is fully connected, you can listen for this using ConnectionListener.

audioManager.setConnectionListener(new ConnectionListener() {
  @Override
  public void onStatusChange(ConnectionStatus status) {
    if (status == CONNECTED) {
     channel.requestToSpeak().queue();
    }
  }
});
rxsto commented 4 months ago

That's it! Awaiting the CONNECTED status resolves this issue for us. Again, interesting catch - it doesn't really make sense how a requestToSpeak request can cause the audio streaming to break.

Thanks for figuring this out! ~ Cheers