Search in sources :

Example 1 with ServerVoiceChannelImpl

use of org.javacord.core.entity.channel.ServerVoiceChannelImpl in project Javacord by BtoBastian.

the class ChannelUpdateHandler method handleServerVoiceChannel.

/**
 * Handles a server voice channel update.
 *
 * @param jsonChannel The channel data.
 */
private void handleServerVoiceChannel(JsonNode jsonChannel) {
    long channelId = jsonChannel.get("id").asLong();
    Optional<ServerVoiceChannel> optionalChannel = api.getServerVoiceChannelById(channelId);
    if (!optionalChannel.isPresent()) {
        LoggerUtil.logMissingChannel(logger, channelId);
        return;
    }
    ServerVoiceChannelImpl channel = (ServerVoiceChannelImpl) optionalChannel.get();
    int oldBitrate = channel.getBitrate();
    int newBitrate = jsonChannel.get("bitrate").asInt();
    if (oldBitrate != newBitrate) {
        channel.setBitrate(newBitrate);
        ServerVoiceChannelChangeBitrateEvent event = new ServerVoiceChannelChangeBitrateEventImpl(channel, newBitrate, oldBitrate);
        api.getEventDispatcher().dispatchServerVoiceChannelChangeBitrateEvent((DispatchQueueSelector) channel.getServer(), channel.getServer(), channel, event);
    }
    int oldUserLimit = channel.getUserLimit().orElse(0);
    int newUserLimit = jsonChannel.get("user_limit").asInt();
    if (oldUserLimit != newUserLimit) {
        channel.setUserLimit(newUserLimit);
        ServerVoiceChannelChangeUserLimitEvent event = new ServerVoiceChannelChangeUserLimitEventImpl(channel, newUserLimit, oldUserLimit);
        api.getEventDispatcher().dispatchServerVoiceChannelChangeUserLimitEvent((DispatchQueueSelector) channel.getServer(), channel.getServer(), channel, event);
    }
}
Also used : ServerVoiceChannelChangeBitrateEvent(org.javacord.api.event.channel.server.voice.ServerVoiceChannelChangeBitrateEvent) ServerVoiceChannelChangeUserLimitEvent(org.javacord.api.event.channel.server.voice.ServerVoiceChannelChangeUserLimitEvent) ServerVoiceChannelChangeBitrateEventImpl(org.javacord.core.event.channel.server.voice.ServerVoiceChannelChangeBitrateEventImpl) ServerVoiceChannelChangeUserLimitEventImpl(org.javacord.core.event.channel.server.voice.ServerVoiceChannelChangeUserLimitEventImpl) ServerVoiceChannel(org.javacord.api.entity.channel.ServerVoiceChannel) ServerVoiceChannelImpl(org.javacord.core.entity.channel.ServerVoiceChannelImpl)

Example 2 with ServerVoiceChannelImpl

use of org.javacord.core.entity.channel.ServerVoiceChannelImpl in project Javacord by BtoBastian.

the class ChannelUpdateHandler method handleRegularServerChannel.

private void handleRegularServerChannel(JsonNode jsonChannel) {
    long channelId = jsonChannel.get("id").asLong();
    Optional<RegularServerChannel> optionalChannel = api.getRegularServerChannelById(channelId);
    if (!optionalChannel.isPresent()) {
        LoggerUtil.logMissingChannel(logger, channelId);
        return;
    }
    RegularServerChannelImpl channel = (RegularServerChannelImpl) optionalChannel.get();
    ServerImpl server = (ServerImpl) channel.getServer();
    final AtomicBoolean areYouAffected = new AtomicBoolean(false);
    ChannelCategory oldCategory = channel.asCategorizable().flatMap(Categorizable::getCategory).orElse(null);
    ChannelCategory newCategory = jsonChannel.hasNonNull("parent_id") ? channel.getServer().getChannelCategoryById(jsonChannel.get("parent_id").asLong(-1)).orElse(null) : null;
    final RegularServerChannelImpl regularServerChannel = (RegularServerChannelImpl) channel;
    final int oldRawPosition = regularServerChannel.getRawPosition();
    final int newRawPosition = jsonChannel.get("position").asInt();
    if (oldRawPosition != newRawPosition || !Objects.deepEquals(oldCategory, newCategory)) {
        final int oldPosition = regularServerChannel.getPosition();
        if (regularServerChannel instanceof ServerTextChannelImpl) {
            ((ServerTextChannelImpl) regularServerChannel).setParentId(newCategory == null ? -1 : newCategory.getId());
        } else if (regularServerChannel instanceof ServerVoiceChannelImpl) {
            ((ServerVoiceChannelImpl) regularServerChannel).setParentId(newCategory == null ? -1 : newCategory.getId());
        }
        regularServerChannel.setRawPosition(newRawPosition);
        final int newPosition = regularServerChannel.getPosition();
        final ServerChannelChangePositionEvent event = new ServerChannelChangePositionEventImpl(regularServerChannel, newPosition, oldPosition, newRawPosition, oldRawPosition, newCategory, oldCategory);
        if (server.isReady()) {
            api.getEventDispatcher().dispatchServerChannelChangePositionEvent((DispatchQueueSelector) regularServerChannel.getServer(), regularServerChannel.getServer(), regularServerChannel, event);
        }
    }
    Collection<Long> rolesWithOverwrittenPermissions = new HashSet<>();
    Collection<Long> usersWithOverwrittenPermissions = new HashSet<>();
    if (jsonChannel.has("permission_overwrites") && !jsonChannel.get("permission_overwrites").isNull()) {
        for (JsonNode permissionOverwriteJson : jsonChannel.get("permission_overwrites")) {
            Permissions oldOverwrittenPermissions;
            ConcurrentHashMap<Long, Permissions> overwrittenPermissions;
            long entityId = permissionOverwriteJson.get("id").asLong();
            Optional<DiscordEntity> entity;
            switch(permissionOverwriteJson.get("type").asInt()) {
                case 0:
                    Role role = server.getRoleById(entityId).orElseThrow(() -> new IllegalStateException("Received channel update event with unknown role!"));
                    entity = Optional.of(role);
                    oldOverwrittenPermissions = regularServerChannel.getOverwrittenPermissions(role);
                    overwrittenPermissions = regularServerChannel.getInternalOverwrittenRolePermissions();
                    rolesWithOverwrittenPermissions.add(entityId);
                    break;
                case 1:
                    oldOverwrittenPermissions = regularServerChannel.getOverwrittenUserPermissions().getOrDefault(entityId, PermissionsImpl.EMPTY_PERMISSIONS);
                    entity = api.getCachedUserById(entityId).map(DiscordEntity.class::cast);
                    overwrittenPermissions = regularServerChannel.getInternalOverwrittenUserPermissions();
                    usersWithOverwrittenPermissions.add(entityId);
                    break;
                default:
                    throw new IllegalStateException("Permission overwrite object with unknown type: " + permissionOverwriteJson);
            }
            long allow = permissionOverwriteJson.get("allow").asLong(0);
            long deny = permissionOverwriteJson.get("deny").asLong(0);
            Permissions newOverwrittenPermissions = new PermissionsImpl(allow, deny);
            if (!newOverwrittenPermissions.equals(oldOverwrittenPermissions)) {
                overwrittenPermissions.put(entityId, newOverwrittenPermissions);
                if (server.isReady()) {
                    dispatchServerChannelChangeOverwrittenPermissionsEvent(channel, newOverwrittenPermissions, oldOverwrittenPermissions, entityId, entity.orElse(null));
                    areYouAffected.compareAndSet(false, entityId == api.getYourself().getId());
                    entity.filter(e -> e instanceof Role).map(Role.class::cast).ifPresent(role -> areYouAffected.compareAndSet(false, role.getUsers().stream().anyMatch(User::isYourself)));
                }
            }
        }
    }
    ConcurrentHashMap<Long, Permissions> overwrittenRolePermissions;
    ConcurrentHashMap<Long, Permissions> overwrittenUserPermissions;
    overwrittenRolePermissions = regularServerChannel.getInternalOverwrittenRolePermissions();
    overwrittenUserPermissions = regularServerChannel.getInternalOverwrittenUserPermissions();
    Iterator<Map.Entry<Long, Permissions>> userIt = overwrittenUserPermissions.entrySet().iterator();
    while (userIt.hasNext()) {
        Map.Entry<Long, Permissions> entry = userIt.next();
        if (usersWithOverwrittenPermissions.contains(entry.getKey())) {
            continue;
        }
        Permissions oldPermissions = entry.getValue();
        userIt.remove();
        if (server.isReady()) {
            dispatchServerChannelChangeOverwrittenPermissionsEvent(channel, PermissionsImpl.EMPTY_PERMISSIONS, oldPermissions, entry.getKey(), api.getCachedUserById(entry.getKey()).orElse(null));
            areYouAffected.compareAndSet(false, entry.getKey() == api.getYourself().getId());
        }
    }
    Iterator<Map.Entry<Long, Permissions>> roleIt = overwrittenRolePermissions.entrySet().iterator();
    while (roleIt.hasNext()) {
        Map.Entry<Long, Permissions> entry = roleIt.next();
        if (rolesWithOverwrittenPermissions.contains(entry.getKey())) {
            continue;
        }
        api.getRoleById(entry.getKey()).ifPresent(role -> {
            Permissions oldPermissions = entry.getValue();
            roleIt.remove();
            if (server.isReady()) {
                dispatchServerChannelChangeOverwrittenPermissionsEvent(channel, PermissionsImpl.EMPTY_PERMISSIONS, oldPermissions, role.getId(), role);
                areYouAffected.compareAndSet(false, role.getUsers().stream().anyMatch(User::isYourself));
            }
        });
    }
    if (areYouAffected.get() && !channel.canYouSee()) {
        api.forEachCachedMessageWhere(msg -> msg.getChannel().getId() == channelId, msg -> {
            api.removeMessageFromCache(msg.getId());
            ((MessageCacheImpl) ((TextChannel) channel).getMessageCache()).removeMessage(msg);
        });
    }
}
Also used : User(org.javacord.api.entity.user.User) RegularServerChannel(org.javacord.api.entity.channel.RegularServerChannel) RegularServerChannelImpl(org.javacord.core.entity.channel.RegularServerChannelImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) ServerChannelChangePositionEvent(org.javacord.api.event.channel.server.ServerChannelChangePositionEvent) ServerImpl(org.javacord.core.entity.server.ServerImpl) PermissionsImpl(org.javacord.core.entity.permission.PermissionsImpl) Permissions(org.javacord.api.entity.permission.Permissions) ServerTextChannelImpl(org.javacord.core.entity.channel.ServerTextChannelImpl) HashSet(java.util.HashSet) ServerVoiceChannelImpl(org.javacord.core.entity.channel.ServerVoiceChannelImpl) Role(org.javacord.api.entity.permission.Role) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageCacheImpl(org.javacord.core.util.cache.MessageCacheImpl) ServerChannelChangePositionEventImpl(org.javacord.core.event.channel.server.ServerChannelChangePositionEventImpl) ChannelCategory(org.javacord.api.entity.channel.ChannelCategory) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DiscordEntity(org.javacord.api.entity.DiscordEntity)

Example 3 with ServerVoiceChannelImpl

use of org.javacord.core.entity.channel.ServerVoiceChannelImpl in project Javacord by BtoBastian.

the class VoiceStateUpdateHandler method handleServerVoiceChannel.

private void handleServerVoiceChannel(JsonNode packet, long userId) {
    api.getPossiblyUnreadyServerById(packet.get("guild_id").asLong()).map(ServerImpl.class::cast).ifPresent(server -> {
        Member member = new MemberImpl(api, server, packet.get("member"), null);
        Optional<ServerVoiceChannelImpl> oldChannel = server.getConnectedVoiceChannel(userId).map(ServerVoiceChannelImpl.class::cast);
        Optional<ServerVoiceChannelImpl> newChannel;
        if (packet.hasNonNull("channel_id")) {
            newChannel = server.getVoiceChannelById(packet.get("channel_id").asLong()).map(ServerVoiceChannelImpl.class::cast);
        } else {
            newChannel = Optional.empty();
        }
        if (!newChannel.equals(oldChannel)) {
            oldChannel.ifPresent(channel -> {
                channel.removeConnectedUser(userId);
                dispatchServerVoiceChannelMemberLeaveEvent(member, newChannel.orElse(null), channel, server);
            });
            newChannel.ifPresent(channel -> {
                channel.addConnectedUser(userId);
                dispatchServerVoiceChannelMemberJoinEvent(member, channel, oldChannel.orElse(null), server);
            });
        }
        if (!packet.hasNonNull("member")) {
            logger.warn("Received VOICE_STATE_UPDATE packet without non-null member field: {}", packet);
            return;
        }
        MemberImpl newMember = new MemberImpl(api, server, packet.get("member"), null);
        Member oldMember = server.getRealMemberById(packet.get("user_id").asLong()).orElse(null);
        boolean newSelfMuted = packet.get("self_mute").asBoolean();
        boolean oldSelfMuted = server.isSelfMuted(userId);
        if (newSelfMuted != oldSelfMuted) {
            UserChangeSelfMutedEventImpl event = new UserChangeSelfMutedEventImpl(newMember, oldMember);
            api.getEventDispatcher().dispatchUserChangeSelfMutedEvent(server, server, newMember.getUser(), event);
        }
        boolean newSelfDeafened = packet.get("self_deaf").asBoolean();
        boolean oldSelfDeafened = server.isSelfDeafened(userId);
        if (newSelfDeafened != oldSelfDeafened) {
            UserChangeSelfDeafenedEventImpl event = new UserChangeSelfDeafenedEventImpl(newMember, oldMember);
            api.getEventDispatcher().dispatchUserChangeSelfDeafenedEvent(server, server, newMember.getUser(), event);
        }
        boolean newMuted = packet.get("mute").asBoolean();
        boolean oldMuted = server.isMuted(userId);
        if (newMuted != oldMuted) {
            server.setMuted(userId, newMuted);
            UserChangeMutedEventImpl event = new UserChangeMutedEventImpl(newMember, oldMember);
            api.getEventDispatcher().dispatchUserChangeMutedEvent(server, server, newMember.getUser(), event);
        }
        boolean newDeafened = packet.get("deaf").asBoolean();
        boolean oldDeafened = server.isDeafened(userId);
        if (newDeafened != oldDeafened) {
            server.setDeafened(userId, newDeafened);
            UserChangeDeafenedEventImpl event = new UserChangeDeafenedEventImpl(newMember, oldMember);
            api.getEventDispatcher().dispatchUserChangeDeafenedEvent(server, server, newMember.getUser(), event);
        }
    });
}
Also used : UserChangeSelfDeafenedEventImpl(org.javacord.core.event.user.UserChangeSelfDeafenedEventImpl) MemberImpl(org.javacord.core.entity.user.MemberImpl) UserChangeSelfMutedEventImpl(org.javacord.core.event.user.UserChangeSelfMutedEventImpl) UserChangeMutedEventImpl(org.javacord.core.event.user.UserChangeMutedEventImpl) Member(org.javacord.core.entity.user.Member) ServerVoiceChannelImpl(org.javacord.core.entity.channel.ServerVoiceChannelImpl) UserChangeDeafenedEventImpl(org.javacord.core.event.user.UserChangeDeafenedEventImpl)

Example 4 with ServerVoiceChannelImpl

use of org.javacord.core.entity.channel.ServerVoiceChannelImpl in project Javacord by BtoBastian.

the class ServerImpl method getOrCreateServerVoiceChannel.

/**
 * Gets or creates a server voice channel.
 *
 * @param data The json data of the channel.
 * @return The server voice channel.
 */
public ServerVoiceChannel getOrCreateServerVoiceChannel(JsonNode data) {
    long id = Long.parseLong(data.get("id").asText());
    ChannelType type = ChannelType.fromId(data.get("type").asInt());
    synchronized (this) {
        if (type == ChannelType.SERVER_VOICE_CHANNEL) {
            return getVoiceChannelById(id).orElseGet(() -> new ServerVoiceChannelImpl(api, this, data));
        }
    }
    // Invalid channel type
    return null;
}
Also used : ChannelType(org.javacord.api.entity.channel.ChannelType) ServerVoiceChannelImpl(org.javacord.core.entity.channel.ServerVoiceChannelImpl)

Aggregations

ServerVoiceChannelImpl (org.javacord.core.entity.channel.ServerVoiceChannelImpl)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 DiscordEntity (org.javacord.api.entity.DiscordEntity)1 ChannelCategory (org.javacord.api.entity.channel.ChannelCategory)1 ChannelType (org.javacord.api.entity.channel.ChannelType)1 RegularServerChannel (org.javacord.api.entity.channel.RegularServerChannel)1 ServerVoiceChannel (org.javacord.api.entity.channel.ServerVoiceChannel)1 Permissions (org.javacord.api.entity.permission.Permissions)1 Role (org.javacord.api.entity.permission.Role)1 User (org.javacord.api.entity.user.User)1 ServerChannelChangePositionEvent (org.javacord.api.event.channel.server.ServerChannelChangePositionEvent)1 ServerVoiceChannelChangeBitrateEvent (org.javacord.api.event.channel.server.voice.ServerVoiceChannelChangeBitrateEvent)1 ServerVoiceChannelChangeUserLimitEvent (org.javacord.api.event.channel.server.voice.ServerVoiceChannelChangeUserLimitEvent)1 RegularServerChannelImpl (org.javacord.core.entity.channel.RegularServerChannelImpl)1 ServerTextChannelImpl (org.javacord.core.entity.channel.ServerTextChannelImpl)1 PermissionsImpl (org.javacord.core.entity.permission.PermissionsImpl)1