Search in sources :

Example 6 with ServerChannel

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

the class ChannelPinsUpdateHandler method handle.

@Override
public void handle(JsonNode packet) {
    long channelId = packet.get("channel_id").asLong();
    Optional<TextChannel> optionalChannel = api.getTextChannelById(channelId);
    if (optionalChannel.isPresent()) {
        TextChannel channel = optionalChannel.get();
        Instant lastPinTimestamp = packet.hasNonNull("last_pin_timestamp") ? OffsetDateTime.parse(packet.get("last_pin_timestamp").asText()).toInstant() : null;
        ChannelPinsUpdateEvent event = new ChannelPinsUpdateEventImpl(channel, lastPinTimestamp);
        Optional<Server> optionalServer = channel.asServerChannel().map(ServerChannel::getServer);
        api.getEventDispatcher().dispatchChannelPinsUpdateEvent(optionalServer.map(DispatchQueueSelector.class::cast).orElse(api), optionalServer.orElse(null), channel, event);
    } else {
        LoggerUtil.logMissingChannel(logger, channelId);
    }
}
Also used : TextChannel(org.javacord.api.entity.channel.TextChannel) Server(org.javacord.api.entity.server.Server) Instant(java.time.Instant) ChannelPinsUpdateEvent(org.javacord.api.event.message.ChannelPinsUpdateEvent) ChannelPinsUpdateEventImpl(org.javacord.core.event.message.ChannelPinsUpdateEventImpl) ServerChannel(org.javacord.api.entity.channel.ServerChannel) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector)

Example 7 with ServerChannel

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

the class MessageUpdateHandler method handle.

@Override
public void handle(JsonNode packet) {
    long messageId = packet.get("id").asLong();
    long channelId = packet.get("channel_id").asLong();
    Optional<TextChannel> optionalChannel = api.getTextChannelById(channelId);
    if (!optionalChannel.isPresent()) {
        LoggerUtil.logMissingChannel(logger, channelId);
        return;
    }
    TextChannel channel = optionalChannel.get();
    Optional<MessageImpl> message = api.getCachedMessageById(messageId).map(msg -> (MessageImpl) msg);
    message.ifPresent(msg -> {
        boolean newPinnedFlag = packet.hasNonNull("pinned") ? packet.get("pinned").asBoolean() : msg.isPinned();
        boolean oldPinnedFlag = msg.isPinned();
        if (newPinnedFlag != oldPinnedFlag) {
            msg.setPinned(newPinnedFlag);
            if (newPinnedFlag) {
                CachedMessagePinEvent event = new CachedMessagePinEventImpl(msg);
                Optional<Server> optionalServer = msg.getChannel().asServerChannel().map(ServerChannel::getServer);
                api.getEventDispatcher().dispatchCachedMessagePinEvent(optionalServer.map(DispatchQueueSelector.class::cast).orElse(api), msg, optionalServer.orElse(null), msg.getChannel(), event);
            } else {
                CachedMessageUnpinEvent event = new CachedMessageUnpinEventImpl(msg);
                Optional<Server> optionalServer = msg.getChannel().asServerChannel().map(ServerChannel::getServer);
                api.getEventDispatcher().dispatchCachedMessageUnpinEvent(optionalServer.map(DispatchQueueSelector.class::cast).orElse(api), msg, optionalServer.orElse(null), msg.getChannel(), event);
            }
        }
    });
    MessageEditEvent editEvent = null;
    if (packet.has("edited_timestamp") && !packet.get("edited_timestamp").isNull()) {
        message.ifPresent(msg -> {
            msg.setLastEditTime(OffsetDateTime.parse(packet.get("edited_timestamp").asText()).toInstant());
            msg.setMentionsEveryone(packet.get("mention_everyone").asBoolean());
        });
        long editTimestamp = OffsetDateTime.parse(packet.get("edited_timestamp").asText()).toInstant().toEpochMilli();
        long lastKnownEditTimestamp = lastKnownEditTimestamps.getOrDefault(messageId, 0L);
        lastKnownEditTimestamps.put(messageId, editTimestamp);
        boolean isMostLikelyAnEdit = true;
        long offset = api.getTimeOffset() == null ? 0 : api.getTimeOffset();
        if (editTimestamp == lastKnownEditTimestamp) {
            isMostLikelyAnEdit = false;
        } else if (System.currentTimeMillis() + offset - editTimestamp > 5000) {
            isMostLikelyAnEdit = false;
        }
        String oldContent = message.map(Message::getContent).orElse(null);
        List<Embed> oldEmbeds = message.map(Message::getEmbeds).orElse(null);
        String newContent = null;
        if (packet.has("content")) {
            newContent = packet.get("content").asText();
            String finalNewContent = newContent;
            message.ifPresent(msg -> msg.setContent(finalNewContent));
        }
        List<Embed> newEmbeds = null;
        if (packet.has("embeds")) {
            newEmbeds = new ArrayList<>();
            for (JsonNode embedJson : packet.get("embeds")) {
                Embed embed = new EmbedImpl(embedJson);
                newEmbeds.add(embed);
            }
            List<Embed> finalNewEmbeds = newEmbeds;
            message.ifPresent(msg -> msg.setEmbeds(finalNewEmbeds));
        }
        if (oldContent != null && newContent != null && !oldContent.equals(newContent)) {
            // If the old content doesn't match the new content it's for sure an edit
            isMostLikelyAnEdit = true;
        }
        if (oldEmbeds != null && newEmbeds != null) {
            if (newEmbeds.size() != oldEmbeds.size()) {
                isMostLikelyAnEdit = true;
            } else {
                for (int i = 0; i < newEmbeds.size(); i++) {
                    if (!((EmbedBuilderDelegateImpl) newEmbeds.get(i).toBuilder().getDelegate()).toJsonNode().toString().equals(((EmbedBuilderDelegateImpl) oldEmbeds.get(i).toBuilder().getDelegate()).toJsonNode().toString())) {
                        isMostLikelyAnEdit = true;
                    }
                }
            }
        }
        if (isMostLikelyAnEdit) {
            editEvent = new MessageEditEventImpl(api, messageId, channel, newContent, newEmbeds, oldContent, oldEmbeds);
        }
    }
    if (editEvent != null) {
        dispatchEditEvent(editEvent);
    }
}
Also used : CachedMessagePinEventImpl(org.javacord.core.event.message.CachedMessagePinEventImpl) Server(org.javacord.api.entity.server.Server) CachedMessageUnpinEvent(org.javacord.api.event.message.CachedMessageUnpinEvent) Embed(org.javacord.api.entity.message.embed.Embed) CachedMessageUnpinEventImpl(org.javacord.core.event.message.CachedMessageUnpinEventImpl) MessageEditEventImpl(org.javacord.core.event.message.MessageEditEventImpl) CachedMessagePinEvent(org.javacord.api.event.message.CachedMessagePinEvent) JsonNode(com.fasterxml.jackson.databind.JsonNode) EmbedImpl(org.javacord.core.entity.message.embed.EmbedImpl) ServerChannel(org.javacord.api.entity.channel.ServerChannel) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector) MessageEditEvent(org.javacord.api.event.message.MessageEditEvent) TextChannel(org.javacord.api.entity.channel.TextChannel) MessageImpl(org.javacord.core.entity.message.MessageImpl)

Example 8 with ServerChannel

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

the class MessageDeleteHandler method handle.

@Override
public void handle(JsonNode packet) {
    long messageId = packet.get("id").asLong();
    long channelId = packet.get("channel_id").asLong();
    Optional<TextChannel> optionalChannel = api.getTextChannelById(channelId);
    if (optionalChannel.isPresent()) {
        TextChannel channel = optionalChannel.get();
        MessageDeleteEvent event = new MessageDeleteEventImpl(api, messageId, channel);
        api.getCachedMessageById(messageId).ifPresent(((MessageCacheImpl) channel.getMessageCache())::removeMessage);
        api.removeMessageFromCache(messageId);
        Optional<Server> optionalServer = channel.asServerChannel().map(ServerChannel::getServer);
        api.getEventDispatcher().dispatchMessageDeleteEvent(optionalServer.map(DispatchQueueSelector.class::cast).orElse(api), messageId, optionalServer.orElse(null), channel, event);
        api.removeObjectListeners(Message.class, messageId);
    } else {
        LoggerUtil.logMissingChannel(logger, channelId);
    }
}
Also used : TextChannel(org.javacord.api.entity.channel.TextChannel) Server(org.javacord.api.entity.server.Server) MessageDeleteEvent(org.javacord.api.event.message.MessageDeleteEvent) ServerChannel(org.javacord.api.entity.channel.ServerChannel) MessageDeleteEventImpl(org.javacord.core.event.message.MessageDeleteEventImpl) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector)

Example 9 with ServerChannel

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

the class ServerImpl method getChannels.

@Override
public List<ServerChannel> getChannels() {
    final List<ServerChannel> channels = getUnorderedChannels().stream().filter(channel -> channel.asCategorizable().map(categorizable -> !categorizable.getCategory().isPresent()).orElse(false)).map(Channel::asRegularServerChannel).filter(Optional::isPresent).map(Optional::get).sorted(Comparator.<RegularServerChannel>comparingInt(channel -> channel.getType().getId()).thenComparing(RegularServerChannelImpl.COMPARE_BY_RAW_POSITION)).collect(Collectors.toList());
    getChannelCategories().forEach(category -> {
        channels.add(category);
        channels.addAll(category.getChannels());
    });
    final Map<ServerTextChannel, List<ServerThreadChannel>> serverTextChannelThreads = new HashMap<>();
    getThreadChannels().forEach(serverThreadChannel -> {
        final ServerTextChannel serverTextChannel = serverThreadChannel.getParent();
        serverTextChannelThreads.merge(serverTextChannel, new ArrayList<>(Collections.singletonList(serverThreadChannel)), (serverThreadChannels, serverThreadChannels2) -> {
            serverThreadChannels.addAll(serverThreadChannels2);
            return new ArrayList<>(serverThreadChannels);
        });
    });
    serverTextChannelThreads.forEach((serverTextChannel, serverThreadChannels) -> channels.addAll(channels.indexOf(serverTextChannel) + 1, serverThreadChannels));
    return Collections.unmodifiableList(channels);
}
Also used : AudioConnection(org.javacord.api.audio.AudioConnection) RegularServerChannelImpl(org.javacord.core.entity.channel.RegularServerChannelImpl) ServerChannel(org.javacord.api.entity.channel.ServerChannel) ServerVoiceChannelImpl(org.javacord.core.entity.channel.ServerVoiceChannelImpl) IconImpl(org.javacord.core.entity.IconImpl) Member(org.javacord.core.entity.user.Member) ServerThreadChannel(org.javacord.api.entity.channel.ServerThreadChannel) URL(java.net.URL) AuditLogImpl(org.javacord.core.entity.auditlog.AuditLogImpl) RestMethod(org.javacord.core.util.rest.RestMethod) ServerStageVoiceChannelImpl(org.javacord.core.entity.channel.ServerStageVoiceChannelImpl) StickerImpl(org.javacord.core.entity.sticker.StickerImpl) Ban(org.javacord.api.entity.server.Ban) BoostLevel(org.javacord.api.entity.server.BoostLevel) RoleImpl(org.javacord.core.entity.permission.RoleImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerTextChannelImpl(org.javacord.core.entity.channel.ServerTextChannelImpl) Locale(java.util.Locale) Map(java.util.Map) RestRequest(org.javacord.core.util.rest.RestRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) VanityUrlCodeImpl(org.javacord.core.entity.VanityUrlCodeImpl) AuditLog(org.javacord.api.entity.auditlog.AuditLog) Webhook(org.javacord.api.entity.webhook.Webhook) NsfwLevel(org.javacord.api.entity.server.NsfwLevel) Activity(org.javacord.api.entity.activity.Activity) InternalServerAttachableListenerManager(org.javacord.core.listener.server.InternalServerAttachableListenerManager) InviteImpl(org.javacord.core.entity.server.invite.InviteImpl) UserImpl(org.javacord.core.entity.user.UserImpl) Collection(java.util.Collection) ChannelCategoryImpl(org.javacord.core.entity.channel.ChannelCategoryImpl) WebhookImpl(org.javacord.core.entity.webhook.WebhookImpl) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) SlashCommand(org.javacord.api.interaction.SlashCommand) MultiFactorAuthenticationLevel(org.javacord.api.entity.server.MultiFactorAuthenticationLevel) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) VanityUrlCode(org.javacord.api.entity.VanityUrlCode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ServerVoiceChannel(org.javacord.api.entity.channel.ServerVoiceChannel) Objects(java.util.Objects) ExplicitContentFilterLevel(org.javacord.api.entity.server.ExplicitContentFilterLevel) RestEndpoint(org.javacord.core.util.rest.RestEndpoint) Sticker(org.javacord.api.entity.sticker.Sticker) List(java.util.List) Logger(org.apache.logging.log4j.Logger) AudioConnectionImpl(org.javacord.core.audio.AudioConnectionImpl) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Role(org.javacord.api.entity.permission.Role) Optional(java.util.Optional) IncomingWebhookImpl(org.javacord.core.entity.webhook.IncomingWebhookImpl) AuditLogActionType(org.javacord.api.entity.auditlog.AuditLogActionType) ActiveThreads(org.javacord.api.entity.server.ActiveThreads) RichInvite(org.javacord.api.entity.server.invite.RichInvite) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector) ChannelType(org.javacord.api.entity.channel.ChannelType) KnownCustomEmoji(org.javacord.api.entity.emoji.KnownCustomEmoji) ActivityImpl(org.javacord.core.entity.activity.ActivityImpl) ServerThreadChannelImpl(org.javacord.core.entity.channel.ServerThreadChannelImpl) Channel(org.javacord.api.entity.channel.Channel) DiscordClient(org.javacord.api.entity.DiscordClient) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) IncomingWebhook(org.javacord.api.entity.webhook.IncomingWebhook) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DiscordEntity(org.javacord.api.entity.DiscordEntity) Region(org.javacord.api.entity.Region) Icon(org.javacord.api.entity.Icon) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) Cleanupable(org.javacord.core.util.Cleanupable) DefaultMessageNotificationLevel(org.javacord.api.entity.server.DefaultMessageNotificationLevel) ReentrantLock(java.util.concurrent.locks.ReentrantLock) MalformedURLException(java.net.MalformedURLException) MemberImpl(org.javacord.core.entity.user.MemberImpl) DiscordApiImpl(org.javacord.core.DiscordApiImpl) AuditLogEntry(org.javacord.api.entity.auditlog.AuditLogEntry) Javacord(org.javacord.api.Javacord) Consumer(java.util.function.Consumer) LoggerUtil(org.javacord.core.util.logging.LoggerUtil) ServerFeature(org.javacord.api.entity.server.ServerFeature) ChannelCategory(org.javacord.api.entity.channel.ChannelCategory) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) ServerStageVoiceChannel(org.javacord.api.entity.channel.ServerStageVoiceChannel) Intent(org.javacord.api.entity.intent.Intent) User(org.javacord.api.entity.user.User) RestRequestResult(org.javacord.core.util.rest.RestRequestResult) VerificationLevel(org.javacord.api.entity.server.VerificationLevel) UserStatus(org.javacord.api.entity.user.UserStatus) DiscordApi(org.javacord.api.DiscordApi) Server(org.javacord.api.entity.server.Server) Comparator(java.util.Comparator) Collections(java.util.Collections) RegularServerChannel(org.javacord.api.entity.channel.RegularServerChannel) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) Optional(java.util.Optional) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ServerChannel(org.javacord.api.entity.channel.ServerChannel) ServerThreadChannel(org.javacord.api.entity.channel.ServerThreadChannel) ServerVoiceChannel(org.javacord.api.entity.channel.ServerVoiceChannel) Channel(org.javacord.api.entity.channel.Channel) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) ServerStageVoiceChannel(org.javacord.api.entity.channel.ServerStageVoiceChannel) RegularServerChannel(org.javacord.api.entity.channel.RegularServerChannel) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ServerChannel(org.javacord.api.entity.channel.ServerChannel) RegularServerChannel(org.javacord.api.entity.channel.RegularServerChannel)

Aggregations

ServerChannel (org.javacord.api.entity.channel.ServerChannel)9 Server (org.javacord.api.entity.server.Server)8 DispatchQueueSelector (org.javacord.core.util.event.DispatchQueueSelector)8 TextChannel (org.javacord.api.entity.channel.TextChannel)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 Message (org.javacord.api.entity.message.Message)3 Instant (java.time.Instant)2 InviteImpl (org.javacord.core.entity.server.invite.InviteImpl)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Locale (java.util.Locale)1