Search in sources :

Example 1 with ServerThreadChannelImpl

use of org.javacord.core.entity.channel.ServerThreadChannelImpl in project Javacord by BtoBastian.

the class ThreadUpdateHandler method handleThread.

private void handleThread(final JsonNode jsonChannel) {
    final long channelId = jsonChannel.get("id").asLong();
    final long serverId = jsonChannel.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 = (ServerThreadChannelImpl) server.getOrCreateServerThreadChannel(jsonChannel);
    // Handling whether the name has changed
    final String oldName = thread.getName();
    final String newName = jsonChannel.get("name").asText();
    if (!Objects.deepEquals(oldName, newName)) {
        thread.setName(newName);
        final ServerChannelChangeNameEvent event = new ServerChannelChangeNameEventImpl(thread, newName, oldName);
        if (server.isReady()) {
            api.getEventDispatcher().dispatchServerChannelChangeNameEvent((DispatchQueueSelector) thread.getServer(), thread.getServer(), thread, event);
        }
    }
}
Also used : ServerThreadChannelImpl(org.javacord.core.entity.channel.ServerThreadChannelImpl) ServerImpl(org.javacord.core.entity.server.ServerImpl) ServerChannelChangeNameEvent(org.javacord.api.event.channel.server.ServerChannelChangeNameEvent) ServerChannelChangeNameEventImpl(org.javacord.core.event.channel.server.ServerChannelChangeNameEventImpl)

Example 2 with ServerThreadChannelImpl

use of org.javacord.core.entity.channel.ServerThreadChannelImpl 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)

Example 3 with ServerThreadChannelImpl

use of org.javacord.core.entity.channel.ServerThreadChannelImpl in project Javacord by BtoBastian.

the class ServerImpl method getOrCreateServerThreadChannel.

/**
 * Gets or creates a server text channel.
 *
 * @param data The json data of the channel.
 * @return The server text channel.
 */
public ServerThreadChannel getOrCreateServerThreadChannel(final JsonNode data) {
    final long id = Long.parseLong(data.get("id").asText());
    final ChannelType type = ChannelType.fromId(data.get("type").asInt());
    synchronized (this) {
        switch(type) {
            case SERVER_PUBLIC_THREAD:
            case SERVER_PRIVATE_THREAD:
            case SERVER_NEWS_THREAD:
                return getThreadChannelById(id).orElseGet(() -> new ServerThreadChannelImpl(api, this, data));
            default:
                // Invalid channel type
                return null;
        }
    }
}
Also used : ServerThreadChannelImpl(org.javacord.core.entity.channel.ServerThreadChannelImpl) ChannelType(org.javacord.api.entity.channel.ChannelType)

Aggregations

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