Search in sources :

Example 1 with RoleChangePermissionsEventImpl

use of org.javacord.core.event.server.role.RoleChangePermissionsEventImpl 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)1 Color (java.awt.Color)1 Set (java.util.Set)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 DiscordApi (org.javacord.api.DiscordApi)1 ServerTextChannel (org.javacord.api.entity.channel.ServerTextChannel)1 Permissions (org.javacord.api.entity.permission.Permissions)1 User (org.javacord.api.entity.user.User)1 RoleChangeColorEvent (org.javacord.api.event.server.role.RoleChangeColorEvent)1 RoleChangeHoistEvent (org.javacord.api.event.server.role.RoleChangeHoistEvent)1 RoleChangeMentionableEvent (org.javacord.api.event.server.role.RoleChangeMentionableEvent)1 RoleChangeNameEvent (org.javacord.api.event.server.role.RoleChangeNameEvent)1 RoleChangePermissionsEvent (org.javacord.api.event.server.role.RoleChangePermissionsEvent)1 RoleChangePositionEvent (org.javacord.api.event.server.role.RoleChangePositionEvent)1 PermissionsImpl (org.javacord.core.entity.permission.PermissionsImpl)1 RoleImpl (org.javacord.core.entity.permission.RoleImpl)1 RoleChangeColorEventImpl (org.javacord.core.event.server.role.RoleChangeColorEventImpl)1 RoleChangeHoistEventImpl (org.javacord.core.event.server.role.RoleChangeHoistEventImpl)1 RoleChangeMentionableEventImpl (org.javacord.core.event.server.role.RoleChangeMentionableEventImpl)1