Search in sources :

Example 1 with ServerTextChannel

use of org.javacord.api.entity.channel.ServerTextChannel in project Javacord by BtoBastian.

the class GuildUpdateHandler method handle.

@Override
public void handle(JsonNode packet) {
    if (packet.has("unavailable") && packet.get("unavailable").asBoolean()) {
        return;
    }
    long id = packet.get("id").asLong();
    api.getPossiblyUnreadyServerById(id).map(server -> (ServerImpl) server).ifPresent(server -> {
        long oldApplicationId = server.getApplicationId().orElse(-1L);
        long newApplicationId = packet.hasNonNull("application_id") ? packet.get("application_id").asLong() : -1L;
        if (oldApplicationId != newApplicationId) {
            server.setApplicationId(newApplicationId);
        }
        String newName = packet.get("name").asText();
        String oldName = server.getName();
        if (!Objects.deepEquals(oldName, newName)) {
            server.setName(newName);
            ServerChangeNameEvent event = new ServerChangeNameEventImpl(server, newName, oldName);
            api.getEventDispatcher().dispatchServerChangeNameEvent(server, server, event);
        }
        String newIconHash = packet.get("icon").asText(null);
        String oldIconHash = server.getIconHash();
        if (!Objects.deepEquals(oldIconHash, newIconHash)) {
            server.setIconHash(newIconHash);
            ServerChangeIconEvent event = new ServerChangeIconEventImpl(server, newIconHash, oldIconHash);
            api.getEventDispatcher().dispatchServerChangeIconEvent(server, server, event);
        }
        String newSplashHash = packet.get("splash").asText(null);
        String oldSplashHash = server.getSplashHash();
        if (!Objects.deepEquals(oldSplashHash, newSplashHash)) {
            server.setSplashHash(newSplashHash);
            ServerChangeSplashEvent event = new ServerChangeSplashEventImpl(server, newSplashHash, oldSplashHash);
            api.getEventDispatcher().dispatchServerChangeSplashEvent(server, server, event);
        }
        VerificationLevel newVerificationLevel = VerificationLevel.fromId(packet.get("verification_level").asInt());
        VerificationLevel oldVerificationLevel = server.getVerificationLevel();
        if (newVerificationLevel != oldVerificationLevel) {
            server.setVerificationLevel(newVerificationLevel);
            ServerChangeVerificationLevelEvent event = new ServerChangeVerificationLevelEventImpl(server, newVerificationLevel, oldVerificationLevel);
            api.getEventDispatcher().dispatchServerChangeVerificationLevelEvent(server, server, event);
        }
        Region newRegion = Region.getRegionByKey(packet.get("region").asText());
        Region oldRegion = server.getRegion();
        if (oldRegion != newRegion) {
            server.setRegion(newRegion);
            ServerChangeRegionEvent event = new ServerChangeRegionEventImpl(server, newRegion, oldRegion);
            api.getEventDispatcher().dispatchServerChangeRegionEvent(server, server, event);
        }
        DefaultMessageNotificationLevel newDefaultMessageNotificationLevel = DefaultMessageNotificationLevel.fromId(packet.get("default_message_notifications").asInt());
        DefaultMessageNotificationLevel oldDefaultMessageNotificationLevel = server.getDefaultMessageNotificationLevel();
        if (newDefaultMessageNotificationLevel != oldDefaultMessageNotificationLevel) {
            server.setDefaultMessageNotificationLevel(newDefaultMessageNotificationLevel);
            ServerChangeDefaultMessageNotificationLevelEvent event = new ServerChangeDefaultMessageNotificationLevelEventImpl(server, newDefaultMessageNotificationLevel, oldDefaultMessageNotificationLevel);
            api.getEventDispatcher().dispatchServerChangeDefaultMessageNotificationLevelEvent(server, server, event);
        }
        long newOwnerId = packet.get("owner_id").asLong();
        long oldOwnerId = server.getOwnerId();
        if (newOwnerId != oldOwnerId) {
            server.setOwnerId(newOwnerId);
            ServerChangeOwnerEvent event = new ServerChangeOwnerEventImpl(server, newOwnerId, oldOwnerId);
            api.getEventDispatcher().dispatchServerChangeOwnerEvent(server, server, event);
        }
        if (packet.has("system_channel_id")) {
            ServerTextChannel newSystemChannel = packet.get("system_channel_id").isNull() ? null : server.getTextChannelById(packet.get("system_channel_id").asLong()).orElse(null);
            ServerTextChannel oldSystemChannel = server.getSystemChannel().orElse(null);
            if (oldSystemChannel != newSystemChannel) {
                server.setSystemChannelId(newSystemChannel == null ? -1 : newSystemChannel.getId());
                ServerChangeSystemChannelEvent event = new ServerChangeSystemChannelEventImpl(server, newSystemChannel, oldSystemChannel);
                api.getEventDispatcher().dispatchServerChangeSystemChannelEvent(server, server, event);
            }
        }
        if (packet.has("afk_channel_id")) {
            ServerVoiceChannel newAfkChannel = packet.get("afk_channel_id").isNull() ? null : server.getVoiceChannelById(packet.get("afk_channel_id").asLong()).orElse(null);
            ServerVoiceChannel oldAfkChannel = server.getAfkChannel().orElse(null);
            if (oldAfkChannel != newAfkChannel) {
                server.setAfkChannelId(newAfkChannel == null ? -1 : newAfkChannel.getId());
                ServerChangeAfkChannelEvent event = new ServerChangeAfkChannelEventImpl(server, newAfkChannel, oldAfkChannel);
                api.getEventDispatcher().dispatchServerChangeAfkChannelEvent(server, server, event);
            }
        }
        int newAfkTimeout = packet.get("afk_timeout").asInt();
        int oldAfkTimeout = server.getAfkTimeoutInSeconds();
        if (oldAfkTimeout != newAfkTimeout) {
            server.setAfkTimeout(newAfkTimeout);
            ServerChangeAfkTimeoutEvent event = new ServerChangeAfkTimeoutEventImpl(server, newAfkTimeout, oldAfkTimeout);
            api.getEventDispatcher().dispatchServerChangeAfkTimeoutEvent(server, server, event);
        }
        ExplicitContentFilterLevel newExplicitContentFilterLevel = ExplicitContentFilterLevel.fromId(packet.get("explicit_content_filter").asInt());
        ExplicitContentFilterLevel oldExplicitContentFilterLevel = server.getExplicitContentFilterLevel();
        if (oldExplicitContentFilterLevel != newExplicitContentFilterLevel) {
            server.setExplicitContentFilterLevel(newExplicitContentFilterLevel);
            ServerChangeExplicitContentFilterLevelEvent event = new ServerChangeExplicitContentFilterLevelEventImpl(server, newExplicitContentFilterLevel, oldExplicitContentFilterLevel);
            api.getEventDispatcher().dispatchServerChangeExplicitContentFilterLevelEvent(server, server, event);
        }
        MultiFactorAuthenticationLevel newMultiFactorAuthenticationLevel = MultiFactorAuthenticationLevel.fromId(packet.get("mfa_level").asInt());
        MultiFactorAuthenticationLevel oldMultiFactorAuthenticationLevel = server.getMultiFactorAuthenticationLevel();
        if (oldMultiFactorAuthenticationLevel != newMultiFactorAuthenticationLevel) {
            server.setMultiFactorAuthenticationLevel(newMultiFactorAuthenticationLevel);
            ServerChangeMultiFactorAuthenticationLevelEvent event = new ServerChangeMultiFactorAuthenticationLevelEventImpl(server, newMultiFactorAuthenticationLevel, oldMultiFactorAuthenticationLevel);
            api.getEventDispatcher().dispatchServerChangeMultiFactorAuthenticationLevelEvent(server, server, event);
        }
        if (packet.has("rules_channel_id")) {
            ServerTextChannel newRulesChannel = packet.get("rules_channel_id").isNull() ? null : server.getTextChannelById(packet.get("rules_channel_id").asLong()).orElse(null);
            ServerTextChannel oldRulesChannel = server.getRulesChannel().orElse(null);
            if (oldRulesChannel != newRulesChannel) {
                server.setRulesChannelId(newRulesChannel == null ? -1 : newRulesChannel.getId());
                ServerChangeRulesChannelEvent event = new ServerChangeRulesChannelEventImpl(server, newRulesChannel, oldRulesChannel);
                api.getEventDispatcher().dispatchServerChangeRulesChannelEvent(server, server, event);
            }
        }
        if (packet.has("public_updates_channel_id")) {
            ServerTextChannel newModeratorsOnlyChannel = packet.get("public_updates_channel_id").isNull() ? null : server.getTextChannelById(packet.get("public_updates_channel_id").asLong()).orElse(null);
            ServerTextChannel oldModeratorsOnlyChannel = server.getModeratorsOnlyChannel().orElse(null);
            if (oldModeratorsOnlyChannel != newModeratorsOnlyChannel) {
                server.setModeratorsOnlyChannelId(newModeratorsOnlyChannel == null ? -1 : newModeratorsOnlyChannel.getId());
                ServerChangeModeratorsOnlyChannelEvent event = new ServerChangeModeratorsOnlyChannelEventImpl(server, newModeratorsOnlyChannel, oldModeratorsOnlyChannel);
                api.getEventDispatcher().dispatchServerChangeModeratorsOnlyChannelEvent(server, server, event);
            }
        }
        BoostLevel oldBoostLevel = server.getBoostLevel();
        BoostLevel newBoostLevel = BoostLevel.fromId(packet.get("premium_tier").asInt());
        if (oldBoostLevel != newBoostLevel) {
            server.setBoostLevel(newBoostLevel);
            ServerChangeBoostLevelEvent event = new ServerChangeBoostLevelEventImpl(server, newBoostLevel, oldBoostLevel);
            api.getEventDispatcher().dispatchServerChangeBoostLevelEvent(server, server, event);
        }
        NsfwLevel oldNsfwLevel = server.getNsfwLevel();
        NsfwLevel newNsfwLevel = NsfwLevel.fromId(packet.get("nsfw_level").asInt());
        if (oldNsfwLevel != newNsfwLevel) {
            server.setNsfwLevel(newNsfwLevel);
            ServerChangeNsfwLevelEvent event = new ServerChangeNsfwLevelEventImpl(server, newNsfwLevel, oldNsfwLevel);
            api.getEventDispatcher().dispatchServerChangeNsfwLevelEvent(server, server, event);
        }
        Locale newPreferredLocale = new Locale.Builder().setLanguageTag(packet.get("preferred_locale").asText()).build();
        Locale oldPreferredLocale = server.getPreferredLocale();
        if (!oldPreferredLocale.equals(newPreferredLocale)) {
            server.setPreferredLocale(newPreferredLocale);
            ServerChangePreferredLocaleEvent event = new ServerChangePreferredLocaleEventImpl(server, newPreferredLocale, oldPreferredLocale);
            api.getEventDispatcher().dispatchServerChangePreferredLocaleEvent(server, server, event);
        }
        int oldBoostCount = server.getBoostCount();
        int newBoostCount = packet.has("premium_subscription_count") ? packet.get("premium_subscription_count").asInt() : 0;
        if (oldBoostCount != newBoostCount) {
            server.setServerBoostCount(newBoostCount);
            ServerChangeBoostCountEvent event = new ServerChangeBoostCountEventImpl(server, newBoostCount, oldBoostCount);
            api.getEventDispatcher().dispatchServerChangeBoostCountEvent(server, server, event);
        }
        String oldDescription = server.getDescription().isPresent() ? server.getDescription().get() : null;
        String newDescription = packet.hasNonNull("description") ? packet.get("description").asText() : null;
        if (!Objects.deepEquals(oldDescription, newDescription)) {
            server.setDescription(newDescription);
            ServerChangeDescriptionEvent event = new ServerChangeDescriptionEventImpl(server, newDescription, oldDescription);
            api.getEventDispatcher().dispatchServerChangeDescriptionEvent(server, server, event);
        }
        String newDiscoverySplashHash = packet.get("discovery_splash").asText(null);
        String oldDiscoverySplashHash = server.getDiscoverySplashHash();
        if (!Objects.deepEquals(oldDiscoverySplashHash, newDiscoverySplashHash)) {
            server.setDiscoverySplashHash(newDiscoverySplashHash);
            ServerChangeDiscoverySplashEvent event = new ServerChangeDiscoverySplashEventImpl(server, newDiscoverySplashHash, oldDiscoverySplashHash);
            api.getEventDispatcher().dispatchServerChangeDiscoverySplashEvent(server, server, event);
        }
        String oldVanityCode = server.getVanityUrlCode().map(VanityUrlCode::getCode).orElse(null);
        String newVanityCode = packet.hasNonNull("vanity_url_code") ? packet.get("vanity_url_code").asText() : null;
        if (!Objects.deepEquals(oldVanityCode, newVanityCode)) {
            server.setVanityUrlCode(new VanityUrlCodeImpl(packet.get("vanity_url_code").asText()));
            ServerChangeVanityUrlCodeEvent event = new ServerChangeVanityUrlCodeEventImpl(server, newVanityCode, oldVanityCode);
            api.getEventDispatcher().dispatchServerChangeVanityUrlCodeEvent(server, server, event);
        }
        Collection<ServerFeature> oldServerFeature = server.getFeatures();
        Collection<ServerFeature> newServerFeature = new ArrayList<>();
        if (packet.has("features")) {
            packet.get("features").forEach(jsonNode -> {
                try {
                    newServerFeature.add(ServerFeature.valueOf(jsonNode.asText()));
                } catch (Exception ignored) {
                    logger.debug("Encountered server with unknown feature {}. Please update to the latest " + "Javacord version or create an issue on the Javacord GitHub page if you are already " + "on the latest version.", jsonNode.asText());
                }
            });
        }
        if (!(oldServerFeature.containsAll(newServerFeature) && newServerFeature.containsAll(oldServerFeature))) {
            server.setServerFeatures(newServerFeature);
            ServerChangeServerFeaturesEvent event = new ServerChangeServerFeaturesEventImpl(server, newServerFeature, oldServerFeature);
            api.getEventDispatcher().dispatchServerChangeServerFeaturesEvent(server, server, event);
        }
        if (packet.has("system_channel_flags")) {
            server.setSystemChannelFlag(packet.get("system_channel_flags").asInt());
        }
    });
}
Also used : ServerChangeDefaultMessageNotificationLevelEvent(org.javacord.api.event.server.ServerChangeDefaultMessageNotificationLevelEvent) ServerChangeExplicitContentFilterLevelEvent(org.javacord.api.event.server.ServerChangeExplicitContentFilterLevelEvent) ServerChangeExplicitContentFilterLevelEventImpl(org.javacord.core.event.server.ServerChangeExplicitContentFilterLevelEventImpl) ServerChangeVanityUrlCodeEvent(org.javacord.api.event.server.ServerChangeVanityUrlCodeEvent) BoostLevel(org.javacord.api.entity.server.BoostLevel) Locale(java.util.Locale) JsonNode(com.fasterxml.jackson.databind.JsonNode) ServerChangeOwnerEventImpl(org.javacord.core.event.server.ServerChangeOwnerEventImpl) VanityUrlCodeImpl(org.javacord.core.entity.VanityUrlCodeImpl) ServerChangeDiscoverySplashEventImpl(org.javacord.core.event.server.ServerChangeDiscoverySplashEventImpl) ServerImpl(org.javacord.core.entity.server.ServerImpl) NsfwLevel(org.javacord.api.entity.server.NsfwLevel) ServerChangeMultiFactorAuthenticationLevelEvent(org.javacord.api.event.server.ServerChangeMultiFactorAuthenticationLevelEvent) Collection(java.util.Collection) ServerChangeSystemChannelEvent(org.javacord.api.event.server.ServerChangeSystemChannelEvent) ServerChangeVerificationLevelEvent(org.javacord.api.event.server.ServerChangeVerificationLevelEvent) MultiFactorAuthenticationLevel(org.javacord.api.entity.server.MultiFactorAuthenticationLevel) ServerChangeIconEvent(org.javacord.api.event.server.ServerChangeIconEvent) ServerChangeAfkChannelEventImpl(org.javacord.core.event.server.ServerChangeAfkChannelEventImpl) ServerChangeOwnerEvent(org.javacord.api.event.server.ServerChangeOwnerEvent) ServerChangeRegionEvent(org.javacord.api.event.server.ServerChangeRegionEvent) VanityUrlCode(org.javacord.api.entity.VanityUrlCode) ServerChangeDescriptionEvent(org.javacord.api.event.server.ServerChangeDescriptionEvent) ServerVoiceChannel(org.javacord.api.entity.channel.ServerVoiceChannel) ServerChangeNameEvent(org.javacord.api.event.server.ServerChangeNameEvent) Objects(java.util.Objects) ExplicitContentFilterLevel(org.javacord.api.entity.server.ExplicitContentFilterLevel) ServerChangePreferredLocaleEvent(org.javacord.api.event.server.ServerChangePreferredLocaleEvent) Logger(org.apache.logging.log4j.Logger) ServerChangeBoostLevelEventImpl(org.javacord.core.event.server.ServerChangeBoostLevelEventImpl) ServerChangeRulesChannelEvent(org.javacord.api.event.server.ServerChangeRulesChannelEvent) ServerChangeBoostCountEvent(org.javacord.api.event.server.ServerChangeBoostCountEvent) ServerChangeMultiFactorAuthenticationLevelEventImpl(org.javacord.core.event.server.ServerChangeMultiFactorAuthenticationLevelEventImpl) ServerChangeRulesChannelEventImpl(org.javacord.core.event.server.ServerChangeRulesChannelEventImpl) ServerChangeServerFeaturesEventImpl(org.javacord.core.event.server.ServerChangeServerFeaturesEventImpl) ServerChangeAfkTimeoutEvent(org.javacord.api.event.server.ServerChangeAfkTimeoutEvent) ServerChangeRegionEventImpl(org.javacord.core.event.server.ServerChangeRegionEventImpl) ArrayList(java.util.ArrayList) ServerChangeModeratorsOnlyChannelEvent(org.javacord.api.event.server.ServerChangeModeratorsOnlyChannelEvent) ServerChangeAfkTimeoutEventImpl(org.javacord.core.event.server.ServerChangeAfkTimeoutEventImpl) Region(org.javacord.api.entity.Region) ServerChangeModeratorsOnlyChannelEventImpl(org.javacord.core.event.server.ServerChangeModeratorsOnlyChannelEventImpl) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) ServerChangePreferredLocaleEventImpl(org.javacord.core.event.server.ServerChangePreferredLocaleEventImpl) ServerChangeServerFeaturesEvent(org.javacord.api.event.server.ServerChangeServerFeaturesEvent) ServerChangeNameEventImpl(org.javacord.core.event.server.ServerChangeNameEventImpl) ServerChangeNsfwLevelEvent(org.javacord.api.event.server.ServerChangeNsfwLevelEvent) DefaultMessageNotificationLevel(org.javacord.api.entity.server.DefaultMessageNotificationLevel) ServerChangeIconEventImpl(org.javacord.core.event.server.ServerChangeIconEventImpl) ServerChangeNsfwLevelEventImpl(org.javacord.core.event.server.ServerChangeNsfwLevelEventImpl) ServerChangeVerificationLevelEventImpl(org.javacord.core.event.server.ServerChangeVerificationLevelEventImpl) ServerChangeDiscoverySplashEvent(org.javacord.api.event.server.ServerChangeDiscoverySplashEvent) ServerChangeSystemChannelEventImpl(org.javacord.core.event.server.ServerChangeSystemChannelEventImpl) ServerChangeBoostCountEventImpl(org.javacord.core.event.server.ServerChangeBoostCountEventImpl) ServerChangeAfkChannelEvent(org.javacord.api.event.server.ServerChangeAfkChannelEvent) ServerChangeSplashEvent(org.javacord.api.event.server.ServerChangeSplashEvent) LoggerUtil(org.javacord.core.util.logging.LoggerUtil) ServerChangeDefaultMessageNotificationLevelEventImpl(org.javacord.core.event.server.ServerChangeDefaultMessageNotificationLevelEventImpl) ServerFeature(org.javacord.api.entity.server.ServerFeature) VerificationLevel(org.javacord.api.entity.server.VerificationLevel) PacketHandler(org.javacord.core.util.gateway.PacketHandler) DiscordApi(org.javacord.api.DiscordApi) ServerChangeBoostLevelEvent(org.javacord.api.event.server.ServerChangeBoostLevelEvent) ServerChangeVanityUrlCodeEventImpl(org.javacord.core.event.server.ServerChangeVanityUrlCodeEventImpl) ServerChangeDescriptionEventImpl(org.javacord.core.event.server.ServerChangeDescriptionEventImpl) ServerChangeSplashEventImpl(org.javacord.core.event.server.ServerChangeSplashEventImpl) Locale(java.util.Locale) ServerChangeVanityUrlCodeEvent(org.javacord.api.event.server.ServerChangeVanityUrlCodeEvent) ServerChangeDefaultMessageNotificationLevelEventImpl(org.javacord.core.event.server.ServerChangeDefaultMessageNotificationLevelEventImpl) ServerChangeOwnerEvent(org.javacord.api.event.server.ServerChangeOwnerEvent) ServerChangeIconEventImpl(org.javacord.core.event.server.ServerChangeIconEventImpl) ServerChangeAfkTimeoutEvent(org.javacord.api.event.server.ServerChangeAfkTimeoutEvent) NsfwLevel(org.javacord.api.entity.server.NsfwLevel) ArrayList(java.util.ArrayList) ServerChangeNameEvent(org.javacord.api.event.server.ServerChangeNameEvent) ServerChangePreferredLocaleEventImpl(org.javacord.core.event.server.ServerChangePreferredLocaleEventImpl) DefaultMessageNotificationLevel(org.javacord.api.entity.server.DefaultMessageNotificationLevel) ServerChangeSplashEvent(org.javacord.api.event.server.ServerChangeSplashEvent) ServerChangePreferredLocaleEvent(org.javacord.api.event.server.ServerChangePreferredLocaleEvent) ServerChangeServerFeaturesEventImpl(org.javacord.core.event.server.ServerChangeServerFeaturesEventImpl) ServerChangeModeratorsOnlyChannelEventImpl(org.javacord.core.event.server.ServerChangeModeratorsOnlyChannelEventImpl) ServerChangeDiscoverySplashEventImpl(org.javacord.core.event.server.ServerChangeDiscoverySplashEventImpl) ServerChangeSystemChannelEvent(org.javacord.api.event.server.ServerChangeSystemChannelEvent) ServerChangeNameEventImpl(org.javacord.core.event.server.ServerChangeNameEventImpl) ServerChangeSplashEventImpl(org.javacord.core.event.server.ServerChangeSplashEventImpl) ServerChangeBoostLevelEventImpl(org.javacord.core.event.server.ServerChangeBoostLevelEventImpl) ServerChangeDescriptionEventImpl(org.javacord.core.event.server.ServerChangeDescriptionEventImpl) ServerChangeBoostCountEvent(org.javacord.api.event.server.ServerChangeBoostCountEvent) ServerChangeVerificationLevelEventImpl(org.javacord.core.event.server.ServerChangeVerificationLevelEventImpl) ServerChangeNsfwLevelEventImpl(org.javacord.core.event.server.ServerChangeNsfwLevelEventImpl) Region(org.javacord.api.entity.Region) BoostLevel(org.javacord.api.entity.server.BoostLevel) ServerVoiceChannel(org.javacord.api.entity.channel.ServerVoiceChannel) ServerChangeAfkTimeoutEventImpl(org.javacord.core.event.server.ServerChangeAfkTimeoutEventImpl) ServerChangeAfkChannelEventImpl(org.javacord.core.event.server.ServerChangeAfkChannelEventImpl) MultiFactorAuthenticationLevel(org.javacord.api.entity.server.MultiFactorAuthenticationLevel) ServerChangeAfkChannelEvent(org.javacord.api.event.server.ServerChangeAfkChannelEvent) ServerChangeRulesChannelEventImpl(org.javacord.core.event.server.ServerChangeRulesChannelEventImpl) ServerChangeIconEvent(org.javacord.api.event.server.ServerChangeIconEvent) ExplicitContentFilterLevel(org.javacord.api.entity.server.ExplicitContentFilterLevel) ServerChangeDiscoverySplashEvent(org.javacord.api.event.server.ServerChangeDiscoverySplashEvent) ServerChangeExplicitContentFilterLevelEvent(org.javacord.api.event.server.ServerChangeExplicitContentFilterLevelEvent) ServerChangeMultiFactorAuthenticationLevelEvent(org.javacord.api.event.server.ServerChangeMultiFactorAuthenticationLevelEvent) ServerChangeOwnerEventImpl(org.javacord.core.event.server.ServerChangeOwnerEventImpl) ServerChangeRegionEvent(org.javacord.api.event.server.ServerChangeRegionEvent) ServerImpl(org.javacord.core.entity.server.ServerImpl) ServerChangeServerFeaturesEvent(org.javacord.api.event.server.ServerChangeServerFeaturesEvent) ServerChangeModeratorsOnlyChannelEvent(org.javacord.api.event.server.ServerChangeModeratorsOnlyChannelEvent) ServerChangeExplicitContentFilterLevelEventImpl(org.javacord.core.event.server.ServerChangeExplicitContentFilterLevelEventImpl) ServerChangeDescriptionEvent(org.javacord.api.event.server.ServerChangeDescriptionEvent) VerificationLevel(org.javacord.api.entity.server.VerificationLevel) ServerChangeMultiFactorAuthenticationLevelEventImpl(org.javacord.core.event.server.ServerChangeMultiFactorAuthenticationLevelEventImpl) ServerChangeRulesChannelEvent(org.javacord.api.event.server.ServerChangeRulesChannelEvent) ServerChangeVerificationLevelEvent(org.javacord.api.event.server.ServerChangeVerificationLevelEvent) ServerFeature(org.javacord.api.entity.server.ServerFeature) ServerChangeDefaultMessageNotificationLevelEvent(org.javacord.api.event.server.ServerChangeDefaultMessageNotificationLevelEvent) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) ServerChangeNsfwLevelEvent(org.javacord.api.event.server.ServerChangeNsfwLevelEvent) ServerChangeVanityUrlCodeEventImpl(org.javacord.core.event.server.ServerChangeVanityUrlCodeEventImpl) VanityUrlCodeImpl(org.javacord.core.entity.VanityUrlCodeImpl) ServerChangeRegionEventImpl(org.javacord.core.event.server.ServerChangeRegionEventImpl) ServerChangeBoostLevelEvent(org.javacord.api.event.server.ServerChangeBoostLevelEvent) ServerChangeBoostCountEventImpl(org.javacord.core.event.server.ServerChangeBoostCountEventImpl) ServerChangeSystemChannelEventImpl(org.javacord.core.event.server.ServerChangeSystemChannelEventImpl)

Example 2 with ServerTextChannel

use of org.javacord.api.entity.channel.ServerTextChannel 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 3 with ServerTextChannel

use of org.javacord.api.entity.channel.ServerTextChannel in project Javacord by BtoBastian.

the class ChannelUpdateHandler method handleServerTextChannel.

/**
 * Handles a server text channel update.
 *
 * @param jsonChannel The json channel data.
 */
private void handleServerTextChannel(JsonNode jsonChannel) {
    long channelId = jsonChannel.get("id").asLong();
    Optional<ServerTextChannel> optionalChannel = api.getServerTextChannelById(channelId);
    if (!optionalChannel.isPresent()) {
        LoggerUtil.logMissingChannel(logger, channelId);
        return;
    }
    ServerTextChannelImpl channel = (ServerTextChannelImpl) optionalChannel.get();
    String oldTopic = channel.getTopic();
    String newTopic = jsonChannel.has("topic") && !jsonChannel.get("topic").isNull() ? jsonChannel.get("topic").asText() : "";
    if (!oldTopic.equals(newTopic)) {
        channel.setTopic(newTopic);
        ServerTextChannelChangeTopicEvent event = new ServerTextChannelChangeTopicEventImpl(channel, newTopic, oldTopic);
        api.getEventDispatcher().dispatchServerTextChannelChangeTopicEvent((DispatchQueueSelector) channel.getServer(), channel.getServer(), channel, event);
    }
    boolean oldNsfwFlag = channel.isNsfw();
    boolean newNsfwFlag = jsonChannel.get("nsfw").asBoolean();
    if (oldNsfwFlag != newNsfwFlag) {
        channel.setNsfwFlag(newNsfwFlag);
        ServerChannelChangeNsfwFlagEvent event = new ServerChannelChangeNsfwFlagEventImpl(channel, newNsfwFlag, oldNsfwFlag);
        api.getEventDispatcher().dispatchServerChannelChangeNsfwFlagEvent((DispatchQueueSelector) channel.getServer(), null, channel.getServer(), channel, event);
    }
    int oldSlowmodeDelay = channel.getSlowmodeDelayInSeconds();
    // Check if "rate_limit_per_user" exists as a temporary fix until SERVER_NEWS_CHANNEL is handled separately.
    int newSlowmodeDelay = jsonChannel.has("rate_limit_per_user") ? jsonChannel.get("rate_limit_per_user").asInt(0) : 0;
    if (oldSlowmodeDelay != newSlowmodeDelay) {
        channel.setSlowmodeDelayInSeconds(newSlowmodeDelay);
        ServerTextChannelChangeSlowmodeEvent event = new ServerTextChannelChangeSlowmodeEventImpl(channel, oldSlowmodeDelay, newSlowmodeDelay);
        api.getEventDispatcher().dispatchServerTextChannelChangeSlowmodeEvent((DispatchQueueSelector) channel.getServer(), channel.getServer(), channel, event);
    }
    int oldDefaultAutoArchiveDuration = channel.getDefaultAutoArchiveDuration();
    int newDefaultAutoArchiveDuration = jsonChannel.has("default_auto_archive_duration") ? jsonChannel.get("default_auto_archive_duration").asInt() : 1440;
    if (oldDefaultAutoArchiveDuration != newDefaultAutoArchiveDuration) {
        channel.setDefaultAutoArchiveDuration(newDefaultAutoArchiveDuration);
        ServerTextChannelChangeDefaultAutoArchiveDurationEvent event = new ServerTextChannelChangeDefaultAutoArchiveDurationEventImpl(channel, oldDefaultAutoArchiveDuration, newDefaultAutoArchiveDuration);
        api.getEventDispatcher().dispatchServerTextChannelChangeDefaultAutoArchiveDurationEvent((DispatchQueueSelector) channel.getServer(), channel.getServer(), channel, event);
    }
}
Also used : ServerChannelChangeNsfwFlagEventImpl(org.javacord.core.event.channel.server.ServerChannelChangeNsfwFlagEventImpl) ServerTextChannelChangeTopicEvent(org.javacord.api.event.channel.server.text.ServerTextChannelChangeTopicEvent) ServerTextChannelChangeTopicEventImpl(org.javacord.core.event.channel.server.text.ServerTextChannelChangeTopicEventImpl) ServerTextChannelChangeDefaultAutoArchiveDurationEventImpl(org.javacord.core.event.channel.server.text.ServerTextChannelChangeDefaultAutoArchiveDurationEventImpl) ServerChannelChangeNsfwFlagEvent(org.javacord.api.event.channel.server.ServerChannelChangeNsfwFlagEvent) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) ServerTextChannelChangeSlowmodeEvent(org.javacord.api.event.channel.server.text.ServerTextChannelChangeSlowmodeEvent) ServerTextChannelChangeDefaultAutoArchiveDurationEvent(org.javacord.api.event.channel.server.text.ServerTextChannelChangeDefaultAutoArchiveDurationEvent) ServerTextChannelChangeSlowmodeEventImpl(org.javacord.core.event.channel.server.text.ServerTextChannelChangeSlowmodeEventImpl) ServerTextChannelImpl(org.javacord.core.entity.channel.ServerTextChannelImpl)

Example 4 with ServerTextChannel

use of org.javacord.api.entity.channel.ServerTextChannel in project Javacord by BtoBastian.

the class ChannelCreateHandler method handleServerTextChannel.

/**
 * Handles server text channel creation.
 *
 * @param channel The channel data.
 */
private void handleServerTextChannel(JsonNode channel) {
    long serverId = channel.get("guild_id").asLong();
    api.getPossiblyUnreadyServerById(serverId).ifPresent(server -> {
        ServerTextChannel textChannel = ((ServerImpl) server).getOrCreateServerTextChannel(channel);
        ServerChannelCreateEvent event = new ServerChannelCreateEventImpl(textChannel);
        api.getEventDispatcher().dispatchServerChannelCreateEvent((DispatchQueueSelector) server, server, event);
    });
}
Also used : ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) ServerImpl(org.javacord.core.entity.server.ServerImpl) ServerChannelCreateEvent(org.javacord.api.event.channel.server.ServerChannelCreateEvent) ServerChannelCreateEventImpl(org.javacord.core.event.channel.server.ServerChannelCreateEventImpl)

Example 5 with ServerTextChannel

use of org.javacord.api.entity.channel.ServerTextChannel in project Javacord by BtoBastian.

the class Message method getMentionedChannels.

/**
 * Gets a list with all channels mentioned in this message.
 *
 * @return A list with all channels mentioned in this message.
 */
default List<ServerTextChannel> getMentionedChannels() {
    List<ServerTextChannel> mentionedChannels = new ArrayList<>();
    Matcher channelMention = DiscordRegexPattern.CHANNEL_MENTION.matcher(getContent());
    while (channelMention.find()) {
        String channelId = channelMention.group("id");
        getApi().getServerTextChannelById(channelId).filter(channel -> !mentionedChannels.contains(channel)).ifPresent(mentionedChannels::add);
    }
    return Collections.unmodifiableList(mentionedChannels);
}
Also used : ChannelType(org.javacord.api.entity.channel.ChannelType) Arrays(java.util.Arrays) ServerChannel(org.javacord.api.entity.channel.ServerChannel) Channel(org.javacord.api.entity.channel.Channel) ServerThreadChannel(org.javacord.api.entity.channel.ServerThreadChannel) URL(java.net.URL) GroupChannel(org.javacord.api.entity.channel.GroupChannel) CompletableFuture(java.util.concurrent.CompletableFuture) PrivateChannel(org.javacord.api.entity.channel.PrivateChannel) DiscordRegexPattern(org.javacord.api.util.DiscordRegexPattern) ArrayList(java.util.ArrayList) DiscordEntity(org.javacord.api.entity.DiscordEntity) Matcher(java.util.regex.Matcher) HighLevelComponent(org.javacord.api.entity.message.component.HighLevelComponent) Embed(org.javacord.api.entity.message.embed.Embed) StickerItem(org.javacord.api.entity.sticker.StickerItem) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) TextChannel(org.javacord.api.entity.channel.TextChannel) Emoji(org.javacord.api.entity.emoji.Emoji) UpdatableFromCache(org.javacord.api.entity.UpdatableFromCache) PermissionType(org.javacord.api.entity.permission.PermissionType) MessageAttachableListenerManager(org.javacord.api.listener.message.MessageAttachableListenerManager) MalformedURLException(java.net.MalformedURLException) Predicate(java.util.function.Predicate) Set(java.util.Set) EmbedBuilder(org.javacord.api.entity.message.embed.EmbedBuilder) CustomEmoji(org.javacord.api.entity.emoji.CustomEmoji) Instant(java.time.Instant) Javacord(org.javacord.api.Javacord) AutoArchiveDuration(org.javacord.api.entity.channel.AutoArchiveDuration) List(java.util.List) Stream(java.util.stream.Stream) User(org.javacord.api.entity.user.User) Role(org.javacord.api.entity.permission.Role) DiscordApi(org.javacord.api.DiscordApi) Server(org.javacord.api.entity.server.Server) Optional(java.util.Optional) Collections(java.util.Collections) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList)

Aggregations

ServerTextChannel (org.javacord.api.entity.channel.ServerTextChannel)9 DiscordApi (org.javacord.api.DiscordApi)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ArrayList (java.util.ArrayList)4 Set (java.util.Set)4 User (org.javacord.api.entity.user.User)4 Collection (java.util.Collection)3 Collections (java.util.Collections)3 Objects (java.util.Objects)3 Predicate (java.util.function.Predicate)3 Collectors (java.util.stream.Collectors)3 ServerThreadChannel (org.javacord.api.entity.channel.ServerThreadChannel)3 Role (org.javacord.api.entity.permission.Role)3 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 Instant (java.time.Instant)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Locale (java.util.Locale)2 Optional (java.util.Optional)2