Search in sources :

Example 1 with ConnectionRequest

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

the class WebSocketClient method queueAudioDisconnect.

public void queueAudioDisconnect(Guild guild) {
    locked("There was an error queueing the audio disconnect", () -> {
        final long guildId = guild.getIdLong();
        ConnectionRequest request = queuedAudioConnections.get(guildId);
        if (request == null) {
            // If we do not have a request
            queuedAudioConnections.put(guildId, new ConnectionRequest(guild));
        } else {
            // If we have a request, change to DISCONNECT
            request.setStage(ConnectionStage.DISCONNECT);
        }
    });
}
Also used : ConnectionRequest(net.dv8tion.jda.internal.audio.ConnectionRequest)

Example 2 with ConnectionRequest

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

the class WebSocketClient method queueAudioConnect.

public void queueAudioConnect(AudioChannel channel) {
    locked("There was an error queueing the audio connect", () -> {
        final long guildId = channel.getGuild().getIdLong();
        ConnectionRequest request = queuedAudioConnections.get(guildId);
        if (request == null) {
            // starting a whole new connection
            request = new ConnectionRequest(channel, ConnectionStage.CONNECT);
            queuedAudioConnections.put(guildId, request);
        } else if (request.getStage() == ConnectionStage.DISCONNECT) {
            // if planned to disconnect, we want to reconnect
            request.setStage(ConnectionStage.RECONNECT);
        }
        // in all cases, update to this channel
        request.setChannel(channel);
    });
}
Also used : ConnectionRequest(net.dv8tion.jda.internal.audio.ConnectionRequest)

Example 3 with ConnectionRequest

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

the class WebSocketClient method queueAudioReconnect.

public void queueAudioReconnect(AudioChannel channel) {
    locked("There was an error queueing the audio reconnect", () -> {
        final long guildId = channel.getGuild().getIdLong();
        ConnectionRequest request = queuedAudioConnections.get(guildId);
        if (request == null) {
            // If no request, then just reconnect
            request = new ConnectionRequest(channel, ConnectionStage.RECONNECT);
            queuedAudioConnections.put(guildId, request);
        } else {
            // If there is a request we change it to reconnect, no matter what it is
            request.setStage(ConnectionStage.RECONNECT);
        }
        // in all cases, update to this channel
        request.setChannel(channel);
    });
}
Also used : ConnectionRequest(net.dv8tion.jda.internal.audio.ConnectionRequest)

Example 4 with ConnectionRequest

use of net.dv8tion.jda.internal.audio.ConnectionRequest 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 5 with ConnectionRequest

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

the class WebSocketClient method getNextAudioConnectRequest.

protected ConnectionRequest getNextAudioConnectRequest() {
    // Don't try to setup audio connections before JDA has finished loading.
    if (sessionId == null)
        return null;
    long now = System.currentTimeMillis();
    AtomicReference<ConnectionRequest> request = new AtomicReference<>();
    queuedAudioConnections.retainEntries((// we use this because it locks the mutex
    guildId, // we use this because it locks the mutex
    audioRequest) -> {
        if (audioRequest.getNextAttemptEpoch() < now) {
            // Check if the guild is ready
            Guild guild = api.getGuildById(guildId);
            if (guild == null) {
                // Not yet ready, check if the guild is known to this shard
                GuildSetupController controller = api.getGuildSetupController();
                if (!controller.isKnown(guildId)) {
                    // The guild is not tracked anymore -> we can't connect the audio channel
                    LOG.debug("Removing audio connection request because the guild has been removed. {}", audioRequest);
                    return false;
                }
                return true;
            }
            ConnectionListener listener = guild.getAudioManager().getConnectionListener();
            if (audioRequest.getStage() != ConnectionStage.DISCONNECT) {
                // Check if we can connect to the target channel
                AudioChannel channel = (AudioChannel) guild.getGuildChannelById(audioRequest.getChannelId());
                if (channel == null) {
                    if (listener != null)
                        listener.onStatusChange(ConnectionStatus.DISCONNECTED_CHANNEL_DELETED);
                    return false;
                }
                IPermissionContainer permChannel = (IPermissionContainer) channel;
                if (!guild.getSelfMember().hasPermission(permChannel, Permission.VOICE_CONNECT)) {
                    if (listener != null)
                        listener.onStatusChange(ConnectionStatus.DISCONNECTED_LOST_PERMISSION);
                    return false;
                }
            }
            // This will take the first result
            request.compareAndSet(null, audioRequest);
        }
        return true;
    });
    return request.get();
}
Also used : ConnectionRequest(net.dv8tion.jda.internal.audio.ConnectionRequest) AudioChannel(net.dv8tion.jda.api.entities.AudioChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConnectionListener(net.dv8tion.jda.api.audio.hooks.ConnectionListener) Guild(net.dv8tion.jda.api.entities.Guild) IPermissionContainer(net.dv8tion.jda.api.entities.IPermissionContainer)

Aggregations

ConnectionRequest (net.dv8tion.jda.internal.audio.ConnectionRequest)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ConnectionListener (net.dv8tion.jda.api.audio.hooks.ConnectionListener)1 AudioChannel (net.dv8tion.jda.api.entities.AudioChannel)1 Guild (net.dv8tion.jda.api.entities.Guild)1 IPermissionContainer (net.dv8tion.jda.api.entities.IPermissionContainer)1 DataObject (net.dv8tion.jda.api.utils.data.DataObject)1 ConnectionStage (net.dv8tion.jda.internal.audio.ConnectionStage)1