Search in sources :

Example 1 with IPermissionContainer

use of net.dv8tion.jda.api.entities.IPermissionContainer 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)

Example 2 with IPermissionContainer

use of net.dv8tion.jda.api.entities.IPermissionContainer in project JDA by DV8FromTheWorld.

the class PermOverrideManagerImpl method checkPermissions.

@Override
protected boolean checkPermissions() {
    Member selfMember = getGuild().getSelfMember();
    IPermissionContainer channel = getChannel();
    if (!selfMember.hasPermission(channel, Permission.VIEW_CHANNEL))
        throw new MissingAccessException(channel, Permission.VIEW_CHANNEL);
    if (!selfMember.hasAccess(channel))
        throw new MissingAccessException(channel, Permission.VOICE_CONNECT);
    if (!selfMember.hasPermission(channel, Permission.MANAGE_PERMISSIONS))
        throw new InsufficientPermissionException(channel, Permission.MANAGE_PERMISSIONS);
    return super.checkPermissions();
}
Also used : InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) MissingAccessException(net.dv8tion.jda.api.exceptions.MissingAccessException) Member(net.dv8tion.jda.api.entities.Member) IPermissionContainer(net.dv8tion.jda.api.entities.IPermissionContainer) PermissionOverride(net.dv8tion.jda.api.entities.PermissionOverride)

Aggregations

IPermissionContainer (net.dv8tion.jda.api.entities.IPermissionContainer)2 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 Member (net.dv8tion.jda.api.entities.Member)1 PermissionOverride (net.dv8tion.jda.api.entities.PermissionOverride)1 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)1 MissingAccessException (net.dv8tion.jda.api.exceptions.MissingAccessException)1 ConnectionRequest (net.dv8tion.jda.internal.audio.ConnectionRequest)1