Search in sources :

Example 1 with ThreadMembersUpdateEvent

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

the class ThreadMembersUpdateHandler method handle.

@Override
public void handle(final JsonNode packet) {
    final long channelId = packet.get("id").asLong();
    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 ServerThreadChannelImpl thread = server.getThreadChannelById(channelId).map(ServerThreadChannelImpl.class::cast).orElse(null);
    if (thread == null) {
        logger.warn("Unable to find thread with id {}", channelId);
        return;
    }
    final int memberCount = packet.get("member_count").asInt();
    final List<Long> removedMemberIds = new ArrayList<>();
    if (packet.has("removed_member_ids")) {
        for (final JsonNode removedMemberId : packet.get("removed_member_ids")) {
            removedMemberIds.add(removedMemberId.asLong());
        }
    }
    final List<ThreadMember> addedMembers = new ArrayList<>();
    if (packet.has("added_members")) {
        for (final JsonNode addedMember : packet.get("added_members")) {
            addedMembers.add(new ThreadMemberImpl(api, server, addedMember));
        }
    }
    thread.setMembers(addedMembers);
    final ThreadMembersUpdateEvent event = new ThreadMembersUpdateEventImpl(thread, server, memberCount, addedMembers, removedMemberIds);
    api.getEventDispatcher().dispatchThreadMembersUpdateEvent(server, server, thread, event);
}
Also used : ServerThreadChannelImpl(org.javacord.core.entity.channel.ServerThreadChannelImpl) ThreadMembersUpdateEventImpl(org.javacord.core.event.channel.thread.ThreadMembersUpdateEventImpl) ThreadMembersUpdateEvent(org.javacord.api.event.channel.thread.ThreadMembersUpdateEvent) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ThreadMember(org.javacord.api.entity.channel.ThreadMember) ThreadMemberImpl(org.javacord.core.entity.channel.ThreadMemberImpl) ServerImpl(org.javacord.core.entity.server.ServerImpl)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayList (java.util.ArrayList)1 ThreadMember (org.javacord.api.entity.channel.ThreadMember)1 ThreadMembersUpdateEvent (org.javacord.api.event.channel.thread.ThreadMembersUpdateEvent)1 ServerThreadChannelImpl (org.javacord.core.entity.channel.ServerThreadChannelImpl)1 ThreadMemberImpl (org.javacord.core.entity.channel.ThreadMemberImpl)1 ServerImpl (org.javacord.core.entity.server.ServerImpl)1 ThreadMembersUpdateEventImpl (org.javacord.core.event.channel.thread.ThreadMembersUpdateEventImpl)1