Search in sources :

Example 1 with ThreadListSyncEvent

use of org.javacord.api.event.channel.thread.ThreadListSyncEvent in project Javacord by BtoBastian.

the class ThreadListSyncHandler method handle.

@Override
public void handle(final JsonNode packet) {
    final long serverId = packet.get("guild_id").asLong();
    final ServerImpl server = api.getServerById(serverId).map(ServerImpl.class::cast).orElse(null);
    if (server == null) {
        logger.warn("Unable to find server with id {}", serverId);
        return;
    }
    final List<Long> channelIds = new ArrayList<>();
    if (packet.has("channel_ids")) {
        for (final JsonNode channelId : packet.get("channel_ids")) {
            channelIds.add(channelId.asLong());
        }
    }
    final List<ServerThreadChannel> threads = new ArrayList<>();
    for (final JsonNode thread : packet.get("threads")) {
        threads.add(server.getOrCreateServerThreadChannel(thread));
    }
    final List<Long> threadIds = new ArrayList<>();
    for (final ServerThreadChannel thread : threads) {
        threadIds.add(thread.getId());
    }
    final List<ThreadMember> members = new ArrayList<>();
    for (final JsonNode member : packet.get("members")) {
        members.add(new ThreadMemberImpl(api, server, member));
    }
    // Removes lost threads from cache
    for (final Channel channel : api.getChannels()) {
        if (channel.getType() == ChannelType.SERVER_PRIVATE_THREAD || channel.getType() == ChannelType.SERVER_PUBLIC_THREAD && !threadIds.contains(channel.getId())) {
            api.removeChannelFromCache(channel.getId());
        }
    }
    final ThreadListSyncEvent event = new ThreadListSyncEventImpl(server, channelIds, threads, members);
    api.getEventDispatcher().dispatchThreadListSyncEvent(server, server, event);
}
Also used : Channel(org.javacord.api.entity.channel.Channel) ServerThreadChannel(org.javacord.api.entity.channel.ServerThreadChannel) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ThreadMember(org.javacord.api.entity.channel.ThreadMember) ThreadListSyncEventImpl(org.javacord.core.event.channel.thread.ThreadListSyncEventImpl) ThreadMemberImpl(org.javacord.core.entity.channel.ThreadMemberImpl) ServerImpl(org.javacord.core.entity.server.ServerImpl) ServerThreadChannel(org.javacord.api.entity.channel.ServerThreadChannel) ThreadListSyncEvent(org.javacord.api.event.channel.thread.ThreadListSyncEvent)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayList (java.util.ArrayList)1 Channel (org.javacord.api.entity.channel.Channel)1 ServerThreadChannel (org.javacord.api.entity.channel.ServerThreadChannel)1 ThreadMember (org.javacord.api.entity.channel.ThreadMember)1 ThreadListSyncEvent (org.javacord.api.event.channel.thread.ThreadListSyncEvent)1 ThreadMemberImpl (org.javacord.core.entity.channel.ThreadMemberImpl)1 ServerImpl (org.javacord.core.entity.server.ServerImpl)1 ThreadListSyncEventImpl (org.javacord.core.event.channel.thread.ThreadListSyncEventImpl)1