Search in sources :

Example 1 with ConnectionStage

use of net.dv8tion.jda.internal.audio.ConnectionStage in project JDA by DV8FromTheWorld.

the class WebSocketClient method updateAudioConnection0.

public ConnectionRequest updateAudioConnection0(long guildId, AudioChannel connectedChannel) {
    // Called by VoiceStateUpdateHandler when we receive a response from discord
    // about our request to CONNECT or DISCONNECT.
    // "stage" should never be RECONNECT here thus we don't check for that case
    ConnectionRequest request = queuedAudioConnections.get(guildId);
    if (request == null)
        return null;
    ConnectionStage requestStage = request.getStage();
    if (connectedChannel == null) {
        // -> Otherwise we ignore it
        switch(requestStage) {
            case DISCONNECT:
                return queuedAudioConnections.remove(guildId);
            case RECONNECT:
                request.setStage(ConnectionStage.CONNECT);
                request.setNextAttemptEpoch(System.currentTimeMillis());
            default:
                return null;
        }
    } else if (requestStage == ConnectionStage.CONNECT) {
        // request, then don't remove it.
        if (request.getChannelId() == connectedChannel.getIdLong())
            return queuedAudioConnections.remove(guildId);
    }
    // If the channel is not the one we are looking for!
    return null;
}
Also used : ConnectionRequest(net.dv8tion.jda.internal.audio.ConnectionRequest) ConnectionStage(net.dv8tion.jda.internal.audio.ConnectionStage)

Example 2 with ConnectionStage

use of net.dv8tion.jda.internal.audio.ConnectionStage in project JDA by DV8FromTheWorld.

the class WebSocketSendingThread method handleAudioRequest.

private void handleAudioRequest(ConnectionRequest audioRequest) {
    long channelId = audioRequest.getChannelId();
    long guildId = audioRequest.getGuildIdLong();
    Guild guild = api.getGuildById(guildId);
    if (guild == null) {
        LOG.debug("Discarding voice request due to null guild {}", guildId);
        // race condition on guild delete, avoid NPE on DISCONNECT requests
        queuedAudioConnections.remove(guildId);
        return;
    }
    ConnectionStage stage = audioRequest.getStage();
    AudioManager audioManager = guild.getAudioManager();
    DataObject packet;
    switch(stage) {
        case RECONNECT:
        case DISCONNECT:
            packet = newVoiceClose(guildId);
            break;
        default:
        case CONNECT:
            packet = newVoiceOpen(audioManager, channelId, guild.getIdLong());
    }
    LOG.debug("Sending voice request {}", packet);
    if (send(packet)) {
        // If we didn't get RateLimited, Next request attempt will be 10 seconds from now
        // we remove it in VoiceStateUpdateHandler once we hear that it has updated our status
        // in 10 seconds we will attempt again in case we did not receive an update
        audioRequest.setNextAttemptEpoch(System.currentTimeMillis() + 10000);
        // If we are already in the correct state according to voice state
        // we will not receive a VOICE_STATE_UPDATE that would remove it
        // thus we update it here
        final GuildVoiceState voiceState = guild.getSelfMember().getVoiceState();
        client.updateAudioConnection0(guild.getIdLong(), voiceState.getChannel());
    }
}
Also used : AudioManager(net.dv8tion.jda.api.managers.AudioManager) DataObject(net.dv8tion.jda.api.utils.data.DataObject) ConnectionStage(net.dv8tion.jda.internal.audio.ConnectionStage) GuildVoiceState(net.dv8tion.jda.api.entities.GuildVoiceState) Guild(net.dv8tion.jda.api.entities.Guild)

Aggregations

ConnectionStage (net.dv8tion.jda.internal.audio.ConnectionStage)2 Guild (net.dv8tion.jda.api.entities.Guild)1 GuildVoiceState (net.dv8tion.jda.api.entities.GuildVoiceState)1 AudioManager (net.dv8tion.jda.api.managers.AudioManager)1 DataObject (net.dv8tion.jda.api.utils.data.DataObject)1 ConnectionRequest (net.dv8tion.jda.internal.audio.ConnectionRequest)1