Search in sources :

Example 11 with ServerImpl

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

Example 12 with ServerImpl

use of org.javacord.core.entity.server.ServerImpl in project Javacord by BtoBastian.

the class ReadyHandler method handle.

@Override
public void handle(JsonNode packet) {
    // Purge the cache first
    api.purgeCache();
    JsonNode guilds = packet.get("guilds");
    for (JsonNode guildJson : guilds) {
        if (guildJson.has("unavailable") && guildJson.get("unavailable").asBoolean()) {
            api.addUnavailableServerToCache(guildJson.get("id").asLong());
            continue;
        }
        new ServerImpl(api, guildJson);
    }
    // https://github.com/hammerandchisel/discord-api-docs/issues/184
    if (packet.has("private_channels")) {
        JsonNode privateChannels = packet.get("private_channels");
        for (JsonNode channelJson : privateChannels) {
            switch(ChannelType.fromId(channelJson.get("type").asInt())) {
                case PRIVATE_CHANNEL:
                    new PrivateChannelImpl(api, channelJson);
                    break;
                case GROUP_CHANNEL:
                    new GroupChannelImpl(api, channelJson);
                    break;
                default:
                    logger.warn("Unknown or unexpected channel type. Your Javacord version might be out of date!");
            }
        }
    }
    api.setYourself(new UserImpl(api, packet.get("user"), (MemberImpl) null, null));
}
Also used : ServerImpl(org.javacord.core.entity.server.ServerImpl) MemberImpl(org.javacord.core.entity.user.MemberImpl) UserImpl(org.javacord.core.entity.user.UserImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) PrivateChannelImpl(org.javacord.core.entity.channel.PrivateChannelImpl) GroupChannelImpl(org.javacord.core.entity.channel.GroupChannelImpl)

Example 13 with ServerImpl

use of org.javacord.core.entity.server.ServerImpl in project Javacord by BtoBastian.

the class ChannelCreateHandler method handleChannelCategory.

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

Example 14 with ServerImpl

use of org.javacord.core.entity.server.ServerImpl 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 15 with ServerImpl

use of org.javacord.core.entity.server.ServerImpl in project Javacord by BtoBastian.

the class GuildBanAddHandler method handle.

@Override
public void handle(JsonNode packet) {
    api.getPossiblyUnreadyServerById(packet.get("guild_id").asLong()).map(server -> (ServerImpl) server).ifPresent(server -> {
        User user = new UserImpl(api, packet.get("user"), (MemberImpl) null, server);
        server.removeMember(user.getId());
        ServerMemberBanEvent event = new ServerMemberBanEventImpl(server, user);
        api.getEventDispatcher().dispatchServerMemberBanEvent(server, server, user, event);
    });
}
Also used : ServerImpl(org.javacord.core.entity.server.ServerImpl) ServerMemberBanEvent(org.javacord.api.event.server.member.ServerMemberBanEvent) User(org.javacord.api.entity.user.User) PacketHandler(org.javacord.core.util.gateway.PacketHandler) DiscordApi(org.javacord.api.DiscordApi) UserImpl(org.javacord.core.entity.user.UserImpl) ServerMemberBanEventImpl(org.javacord.core.event.server.member.ServerMemberBanEventImpl) MemberImpl(org.javacord.core.entity.user.MemberImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) User(org.javacord.api.entity.user.User) ServerImpl(org.javacord.core.entity.server.ServerImpl) UserImpl(org.javacord.core.entity.user.UserImpl) ServerMemberBanEvent(org.javacord.api.event.server.member.ServerMemberBanEvent) ServerMemberBanEventImpl(org.javacord.core.event.server.member.ServerMemberBanEventImpl)

Aggregations

ServerImpl (org.javacord.core.entity.server.ServerImpl)25 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 DiscordApi (org.javacord.api.DiscordApi)9 PacketHandler (org.javacord.core.util.gateway.PacketHandler)9 MemberImpl (org.javacord.core.entity.user.MemberImpl)8 UserImpl (org.javacord.core.entity.user.UserImpl)7 User (org.javacord.api.entity.user.User)6 ArrayList (java.util.ArrayList)5 Objects (java.util.Objects)4 Role (org.javacord.api.entity.permission.Role)4 ServerChannelCreateEvent (org.javacord.api.event.channel.server.ServerChannelCreateEvent)4 ServerChannelCreateEventImpl (org.javacord.core.event.channel.server.ServerChannelCreateEventImpl)4 Collection (java.util.Collection)3 Set (java.util.Set)3 ServerTextChannel (org.javacord.api.entity.channel.ServerTextChannel)3 TextChannel (org.javacord.api.entity.channel.TextChannel)3 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Optional (java.util.Optional)2