Search in sources :

Example 1 with DispatchQueueSelector

use of org.javacord.core.util.event.DispatchQueueSelector in project Javacord by BtoBastian.

the class GuildMemberUpdateHandler method handle.

@Override
public void handle(JsonNode packet) {
    api.getPossiblyUnreadyServerById(packet.get("guild_id").asLong()).map(server -> (ServerImpl) server).ifPresent(server -> {
        MemberImpl newMember = new MemberImpl(api, server, packet, null);
        Member oldMember = server.getRealMemberById(newMember.getId()).orElse(null);
        api.addMemberToCacheOrReplaceExisting(newMember);
        if (oldMember == null) {
            // Should only happen shortly after startup and is unproblematic
            return;
        }
        if (!newMember.getNickname().equals(oldMember.getNickname())) {
            UserChangeNicknameEvent event = new UserChangeNicknameEventImpl(newMember, oldMember);
            api.getEventDispatcher().dispatchUserChangeNicknameEvent(server, server, newMember.getUser(), event);
        }
        if (!newMember.getServerAvatarHash().equals(oldMember.getServerAvatarHash())) {
            UserChangeServerAvatarEvent event = new UserChangeServerAvatarEventImpl(newMember, oldMember);
            api.getEventDispatcher().dispatchUserChangeServerAvatarEvent(server, server, newMember.getUser(), event);
        }
        if (newMember.isPending() != oldMember.isPending()) {
            UserChangePendingEvent event = new UserChangePendingEventImpl(oldMember, newMember);
            api.getEventDispatcher().dispatchUserChangePendingEvent(server, server, newMember.getUser(), event);
        }
        if (packet.has("roles")) {
            JsonNode jsonRoles = packet.get("roles");
            Collection<Role> newRoles = new HashSet<>();
            Collection<Role> oldRoles = oldMember.getRoles();
            Collection<Role> intersection = new HashSet<>();
            for (JsonNode roleIdJson : jsonRoles) {
                api.getRoleById(roleIdJson.asText()).map(role -> {
                    newRoles.add(role);
                    return role;
                }).filter(oldRoles::contains).ifPresent(intersection::add);
            }
            // Added roles
            Collection<Role> addedRoles = new ArrayList<>(newRoles);
            addedRoles.removeAll(intersection);
            for (Role role : addedRoles) {
                if (role.isEveryoneRole()) {
                    continue;
                }
                UserRoleAddEvent event = new UserRoleAddEventImpl(role, newMember);
                api.getEventDispatcher().dispatchUserRoleAddEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), newMember.getId(), event);
            }
            // Removed roles
            Collection<Role> removedRoles = new ArrayList<>(oldRoles);
            removedRoles.removeAll(intersection);
            for (Role role : removedRoles) {
                if (role.isEveryoneRole()) {
                    continue;
                }
                UserRoleRemoveEvent event = new UserRoleRemoveEventImpl(role, newMember);
                api.getEventDispatcher().dispatchUserRoleRemoveEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), newMember.getId(), event);
            }
        }
        if (newMember.getUser().isYourself()) {
            Set<Long> unreadableChannels = server.getTextChannels().stream().filter(((Predicate<ServerTextChannel>) ServerTextChannel::canYouSee).negate()).map(ServerTextChannel::getId).collect(Collectors.toSet());
            api.forEachCachedMessageWhere(msg -> unreadableChannels.contains(msg.getChannel().getId()), msg -> {
                api.removeMessageFromCache(msg.getId());
                ((MessageCacheImpl) msg.getChannel().getMessageCache()).removeMessage(msg);
            });
        }
        // https://github.com/discord/discord-api-docs/pull/1307#issuecomment-581561519
        if (oldMember.getUser() != null) {
            UserImpl oldUser = (UserImpl) oldMember.getUser();
            boolean userChanged = false;
            UserImpl updatedUser = oldUser.replacePartialUserData(packet.get("user"));
            if (packet.get("user").has("username")) {
                String newName = packet.get("user").get("username").asText();
                String oldName = oldUser.getName();
                if (!oldName.equals(newName)) {
                    dispatchUserChangeNameEvent(updatedUser, newName, oldName);
                    userChanged = true;
                }
            }
            if (packet.get("user").has("discriminator")) {
                String newDiscriminator = packet.get("user").get("discriminator").asText();
                String oldDiscriminator = oldUser.getDiscriminator();
                if (!oldDiscriminator.equals(newDiscriminator)) {
                    dispatchUserChangeDiscriminatorEvent(updatedUser, newDiscriminator, oldDiscriminator);
                    userChanged = true;
                }
            }
            if (packet.get("user").has("avatar")) {
                String newAvatarHash = packet.get("user").get("avatar").asText(null);
                String oldAvatarHash = oldUser.getAvatarHash().orElse(null);
                if (!Objects.deepEquals(newAvatarHash, oldAvatarHash)) {
                    dispatchUserChangeAvatarEvent(updatedUser, newAvatarHash, oldAvatarHash);
                    userChanged = true;
                }
            }
            if (userChanged) {
                api.updateUserOfAllMembers(updatedUser);
            }
        }
    });
}
Also used : UserChangeNameEventImpl(org.javacord.core.event.user.UserChangeNameEventImpl) Member(org.javacord.core.entity.user.Member) UserChangeServerAvatarEventImpl(org.javacord.core.event.user.UserChangeServerAvatarEventImpl) UserChangePendingEvent(org.javacord.api.event.user.UserChangePendingEvent) UserRoleRemoveEventImpl(org.javacord.core.event.server.role.UserRoleRemoveEventImpl) UserChangeAvatarEvent(org.javacord.api.event.user.UserChangeAvatarEvent) UserChangeAvatarEventImpl(org.javacord.core.event.user.UserChangeAvatarEventImpl) UserChangeNicknameEvent(org.javacord.api.event.user.UserChangeNicknameEvent) UserChangeServerAvatarEvent(org.javacord.api.event.user.UserChangeServerAvatarEvent) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MessageCacheImpl(org.javacord.core.util.cache.MessageCacheImpl) UserRoleRemoveEvent(org.javacord.api.event.server.role.UserRoleRemoveEvent) UserChangePendingEventImpl(org.javacord.core.event.user.UserChangePendingEventImpl) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) JsonNode(com.fasterxml.jackson.databind.JsonNode) UserChangeDiscriminatorEventImpl(org.javacord.core.event.user.UserChangeDiscriminatorEventImpl) ServerImpl(org.javacord.core.entity.server.ServerImpl) Predicate(java.util.function.Predicate) UserImpl(org.javacord.core.entity.user.UserImpl) Collection(java.util.Collection) MemberImpl(org.javacord.core.entity.user.MemberImpl) Set(java.util.Set) Collectors(java.util.stream.Collectors) UserRoleAddEvent(org.javacord.api.event.server.role.UserRoleAddEvent) UserChangeNameEvent(org.javacord.api.event.user.UserChangeNameEvent) Objects(java.util.Objects) LoggerUtil(org.javacord.core.util.logging.LoggerUtil) Logger(org.apache.logging.log4j.Logger) User(org.javacord.api.entity.user.User) UserRoleAddEventImpl(org.javacord.core.event.server.role.UserRoleAddEventImpl) Role(org.javacord.api.entity.permission.Role) PacketHandler(org.javacord.core.util.gateway.PacketHandler) DiscordApi(org.javacord.api.DiscordApi) UserChangeNicknameEventImpl(org.javacord.core.event.user.UserChangeNicknameEventImpl) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector) Collections(java.util.Collections) UserChangeDiscriminatorEvent(org.javacord.api.event.user.UserChangeDiscriminatorEvent) UserChangePendingEvent(org.javacord.api.event.user.UserChangePendingEvent) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) UserChangeNicknameEventImpl(org.javacord.core.event.user.UserChangeNicknameEventImpl) UserChangePendingEventImpl(org.javacord.core.event.user.UserChangePendingEventImpl) ServerImpl(org.javacord.core.entity.server.ServerImpl) UserRoleRemoveEvent(org.javacord.api.event.server.role.UserRoleRemoveEvent) UserImpl(org.javacord.core.entity.user.UserImpl) Member(org.javacord.core.entity.user.Member) HashSet(java.util.HashSet) UserRoleAddEventImpl(org.javacord.core.event.server.role.UserRoleAddEventImpl) MemberImpl(org.javacord.core.entity.user.MemberImpl) UserChangeServerAvatarEventImpl(org.javacord.core.event.user.UserChangeServerAvatarEventImpl) Role(org.javacord.api.entity.permission.Role) UserRoleAddEvent(org.javacord.api.event.server.role.UserRoleAddEvent) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) MessageCacheImpl(org.javacord.core.util.cache.MessageCacheImpl) UserChangeServerAvatarEvent(org.javacord.api.event.user.UserChangeServerAvatarEvent) UserChangeNicknameEvent(org.javacord.api.event.user.UserChangeNicknameEvent) UserRoleRemoveEventImpl(org.javacord.core.event.server.role.UserRoleRemoveEventImpl)

Example 2 with DispatchQueueSelector

use of org.javacord.core.util.event.DispatchQueueSelector in project Javacord by BtoBastian.

the class GuildRoleUpdateHandler method handle.

@Override
public void handle(JsonNode packet) {
    JsonNode roleJson = packet.get("role");
    long roleId = roleJson.get("id").asLong();
    api.getRoleById(roleId).map(role -> (RoleImpl) role).ifPresent(role -> {
        Color oldColorObject = role.getColor().orElse(null);
        int oldColor = role.getColorAsInt();
        int newColor = roleJson.get("color").asInt(0);
        if (oldColor != newColor) {
            role.setColor(newColor);
            RoleChangeColorEvent event = new RoleChangeColorEventImpl(role, role.getColor().orElse(null), oldColorObject);
            api.getEventDispatcher().dispatchRoleChangeColorEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
        }
        boolean oldHoist = role.isDisplayedSeparately();
        boolean newHoist = roleJson.get("hoist").asBoolean(false);
        if (oldHoist != newHoist) {
            role.setHoist(newHoist);
            RoleChangeHoistEvent event = new RoleChangeHoistEventImpl(role, oldHoist);
            api.getEventDispatcher().dispatchRoleChangeHoistEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
        }
        boolean oldMentionable = role.isMentionable();
        boolean newMentionable = roleJson.get("mentionable").asBoolean(false);
        if (oldMentionable != newMentionable) {
            role.setMentionable(newMentionable);
            RoleChangeMentionableEvent event = new RoleChangeMentionableEventImpl(role, oldMentionable);
            api.getEventDispatcher().dispatchRoleChangeMentionableEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
        }
        String oldName = role.getName();
        String newName = roleJson.get("name").asText();
        if (!oldName.equals(newName)) {
            role.setName(newName);
            RoleChangeNameEvent event = new RoleChangeNameEventImpl(role, newName, oldName);
            api.getEventDispatcher().dispatchRoleChangeNameEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
        }
        Permissions oldPermissions = role.getPermissions();
        PermissionsImpl newPermissions = new PermissionsImpl(roleJson.get("permissions").asLong(), 0);
        if (!oldPermissions.equals(newPermissions)) {
            role.setPermissions(newPermissions);
            RoleChangePermissionsEvent event = new RoleChangePermissionsEventImpl(role, newPermissions, oldPermissions);
            api.getEventDispatcher().dispatchRoleChangePermissionsEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
            // If bot is affected remove messages from cache that are no longer visible
            if (role.getUsers().stream().anyMatch(User::isYourself)) {
                Set<Long> unreadableChannels = role.getServer().getTextChannels().stream().filter(((Predicate<ServerTextChannel>) ServerTextChannel::canYouSee).negate()).map(ServerTextChannel::getId).collect(Collectors.toSet());
                api.forEachCachedMessageWhere(msg -> unreadableChannels.contains(msg.getChannel().getId()), msg -> {
                    api.removeMessageFromCache(msg.getId());
                    ((MessageCacheImpl) msg.getChannel().getMessageCache()).removeMessage(msg);
                });
            }
        }
        int oldPosition = role.getPosition();
        int oldRawPosition = role.getRawPosition();
        int newRawPosition = roleJson.get("position").asInt();
        if (oldRawPosition != newRawPosition) {
            role.setRawPosition(newRawPosition);
            int newPosition = role.getPosition();
            RoleChangePositionEvent event = new RoleChangePositionEventImpl(role, newRawPosition, oldRawPosition, newPosition, oldPosition);
            api.getEventDispatcher().dispatchRoleChangePositionEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
        }
    });
}
Also used : Color(java.awt.Color) RoleChangePositionEventImpl(org.javacord.core.event.server.role.RoleChangePositionEventImpl) RoleChangePositionEvent(org.javacord.api.event.server.role.RoleChangePositionEvent) RoleChangeColorEvent(org.javacord.api.event.server.role.RoleChangeColorEvent) RoleChangeColorEventImpl(org.javacord.core.event.server.role.RoleChangeColorEventImpl) Permissions(org.javacord.api.entity.permission.Permissions) RoleImpl(org.javacord.core.entity.permission.RoleImpl) MessageCacheImpl(org.javacord.core.util.cache.MessageCacheImpl) RoleChangeMentionableEventImpl(org.javacord.core.event.server.role.RoleChangeMentionableEventImpl) RoleChangePermissionsEvent(org.javacord.api.event.server.role.RoleChangePermissionsEvent) RoleChangeNameEventImpl(org.javacord.core.event.server.role.RoleChangeNameEventImpl) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) JsonNode(com.fasterxml.jackson.databind.JsonNode) RoleChangePermissionsEventImpl(org.javacord.core.event.server.role.RoleChangePermissionsEventImpl) RoleChangeHoistEventImpl(org.javacord.core.event.server.role.RoleChangeHoistEventImpl) PermissionsImpl(org.javacord.core.entity.permission.PermissionsImpl) Predicate(java.util.function.Predicate) Set(java.util.Set) Collectors(java.util.stream.Collectors) User(org.javacord.api.entity.user.User) RoleChangeMentionableEvent(org.javacord.api.event.server.role.RoleChangeMentionableEvent) PacketHandler(org.javacord.core.util.gateway.PacketHandler) DiscordApi(org.javacord.api.DiscordApi) RoleChangeNameEvent(org.javacord.api.event.server.role.RoleChangeNameEvent) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector) RoleChangeHoistEvent(org.javacord.api.event.server.role.RoleChangeHoistEvent) User(org.javacord.api.entity.user.User) RoleChangeMentionableEvent(org.javacord.api.event.server.role.RoleChangeMentionableEvent) RoleChangeHoistEventImpl(org.javacord.core.event.server.role.RoleChangeHoistEventImpl) RoleChangePositionEvent(org.javacord.api.event.server.role.RoleChangePositionEvent) JsonNode(com.fasterxml.jackson.databind.JsonNode) RoleChangeHoistEvent(org.javacord.api.event.server.role.RoleChangeHoistEvent) RoleChangeNameEvent(org.javacord.api.event.server.role.RoleChangeNameEvent) PermissionsImpl(org.javacord.core.entity.permission.PermissionsImpl) Permissions(org.javacord.api.entity.permission.Permissions) RoleChangeColorEvent(org.javacord.api.event.server.role.RoleChangeColorEvent) RoleChangeMentionableEventImpl(org.javacord.core.event.server.role.RoleChangeMentionableEventImpl) RoleChangeNameEventImpl(org.javacord.core.event.server.role.RoleChangeNameEventImpl) RoleChangePositionEventImpl(org.javacord.core.event.server.role.RoleChangePositionEventImpl) RoleImpl(org.javacord.core.entity.permission.RoleImpl) Color(java.awt.Color) RoleChangePermissionsEventImpl(org.javacord.core.event.server.role.RoleChangePermissionsEventImpl) RoleChangeColorEventImpl(org.javacord.core.event.server.role.RoleChangeColorEventImpl) RoleChangePermissionsEvent(org.javacord.api.event.server.role.RoleChangePermissionsEvent) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) MessageCacheImpl(org.javacord.core.util.cache.MessageCacheImpl)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Set (java.util.Set)2 Predicate (java.util.function.Predicate)2 Collectors (java.util.stream.Collectors)2 DiscordApi (org.javacord.api.DiscordApi)2 ServerTextChannel (org.javacord.api.entity.channel.ServerTextChannel)2 User (org.javacord.api.entity.user.User)2 MessageCacheImpl (org.javacord.core.util.cache.MessageCacheImpl)2 DispatchQueueSelector (org.javacord.core.util.event.DispatchQueueSelector)2 PacketHandler (org.javacord.core.util.gateway.PacketHandler)2 Color (java.awt.Color)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Objects (java.util.Objects)1 Logger (org.apache.logging.log4j.Logger)1 Permissions (org.javacord.api.entity.permission.Permissions)1 Role (org.javacord.api.entity.permission.Role)1 RoleChangeColorEvent (org.javacord.api.event.server.role.RoleChangeColorEvent)1