Search in sources :

Example 21 with ServerImpl

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

the class ThreadCreateHandler method handleThread.

/**
 * Handles the creation of a thread.
 *
 * @param channel The channel data from which to build the thread.
 */
private void handleThread(final JsonNode channel) {
    final long serverId = channel.get("guild_id").asLong();
    api.getPossiblyUnreadyServerById(serverId).ifPresent(server -> {
        final ServerThreadChannel serverThreadChannel = ((ServerImpl) server).getOrCreateServerThreadChannel(channel);
        final ThreadCreateEvent event = new ThreadCreateEventImpl(serverThreadChannel);
        api.getEventDispatcher().dispatchThreadCreateEvent((DispatchQueueSelector) server, serverThreadChannel, event);
    });
}
Also used : ServerImpl(org.javacord.core.entity.server.ServerImpl) ServerThreadChannel(org.javacord.api.entity.channel.ServerThreadChannel) ThreadCreateEventImpl(org.javacord.core.event.channel.thread.ThreadCreateEventImpl) ThreadCreateEvent(org.javacord.api.event.channel.thread.ThreadCreateEvent)

Example 22 with ServerImpl

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

the class GuildEmojisUpdateHandler method handle.

@Override
public void handle(JsonNode packet) {
    long id = packet.get("guild_id").asLong();
    api.getPossiblyUnreadyServerById(id).map(server -> (ServerImpl) server).ifPresent(server -> {
        HashMap<Long, JsonNode> emojis = new HashMap<>();
        for (JsonNode emojiJson : packet.get("emojis")) {
            emojis.put(emojiJson.get("id").asLong(), emojiJson);
        }
        emojis.forEach((key, value) -> {
            Optional<KnownCustomEmoji> optionalEmoji = server.getCustomEmojiById(key);
            if (optionalEmoji.isPresent()) {
                KnownCustomEmoji emoji = optionalEmoji.get();
                String oldName = emoji.getName();
                String newName = value.get("name").asText();
                if (!Objects.deepEquals(oldName, newName)) {
                    KnownCustomEmojiChangeNameEvent event = new KnownCustomEmojiChangeNameEventImpl(emoji, newName, oldName);
                    ((KnownCustomEmojiImpl) emoji).setName(newName);
                    api.getEventDispatcher().dispatchKnownCustomEmojiChangeNameEvent(server, emoji, server, event);
                }
                Collection<Role> oldWhitelist = emoji.getWhitelistedRoles().orElse(Collections.emptySet());
                JsonNode newWhitelistJson = value.get("roles");
                Collection<Role> newWhitelist = new ArrayList<>();
                if (newWhitelistJson != null && !newWhitelistJson.isNull()) {
                    for (JsonNode role : newWhitelistJson) {
                        server.getRoleById(role.asLong()).ifPresent(newWhitelist::add);
                    }
                }
                if (!newWhitelist.containsAll(oldWhitelist) || !oldWhitelist.containsAll(newWhitelist)) {
                    KnownCustomEmojiChangeWhitelistedRolesEvent event = new KnownCustomEmojiChangeWhitelistedRolesEventImpl(emoji, newWhitelist, oldWhitelist);
                    ((KnownCustomEmojiImpl) emoji).setWhitelist(newWhitelist);
                    api.getEventDispatcher().dispatchKnownCustomEmojiChangeWhitelistedRolesEvent(server, emoji, server, event);
                }
            } else {
                KnownCustomEmoji emoji = api.getOrCreateKnownCustomEmoji(server, value);
                server.addCustomEmoji(emoji);
                KnownCustomEmojiCreateEvent event = new KnownCustomEmojiCreateEventImpl(emoji);
                api.getEventDispatcher().dispatchKnownCustomEmojiCreateEvent(server, server, event);
            }
        });
        Set<Long> emojiIds = emojis.keySet();
        server.getCustomEmojis().stream().filter(emoji -> !emojiIds.contains(emoji.getId())).forEach(emoji -> {
            api.removeCustomEmoji(emoji);
            server.removeCustomEmoji(emoji);
            KnownCustomEmojiDeleteEvent event = new KnownCustomEmojiDeleteEventImpl(emoji);
            api.getEventDispatcher().dispatchKnownCustomEmojiDeleteEvent(server, emoji, server, event);
        });
    });
}
Also used : ServerImpl(org.javacord.core.entity.server.ServerImpl) KnownCustomEmojiChangeNameEvent(org.javacord.api.event.server.emoji.KnownCustomEmojiChangeNameEvent) KnownCustomEmojiDeleteEventImpl(org.javacord.core.event.server.emoji.KnownCustomEmojiDeleteEventImpl) KnownCustomEmoji(org.javacord.api.entity.emoji.KnownCustomEmoji) KnownCustomEmojiChangeNameEventImpl(org.javacord.core.event.server.emoji.KnownCustomEmojiChangeNameEventImpl) Collection(java.util.Collection) Set(java.util.Set) HashMap(java.util.HashMap) KnownCustomEmojiCreateEventImpl(org.javacord.core.event.server.emoji.KnownCustomEmojiCreateEventImpl) ArrayList(java.util.ArrayList) Objects(java.util.Objects) KnownCustomEmojiChangeWhitelistedRolesEventImpl(org.javacord.core.event.server.emoji.KnownCustomEmojiChangeWhitelistedRolesEventImpl) Role(org.javacord.api.entity.permission.Role) PacketHandler(org.javacord.core.util.gateway.PacketHandler) DiscordApi(org.javacord.api.DiscordApi) KnownCustomEmojiCreateEvent(org.javacord.api.event.server.emoji.KnownCustomEmojiCreateEvent) Optional(java.util.Optional) JsonNode(com.fasterxml.jackson.databind.JsonNode) KnownCustomEmojiDeleteEvent(org.javacord.api.event.server.emoji.KnownCustomEmojiDeleteEvent) Collections(java.util.Collections) KnownCustomEmojiChangeWhitelistedRolesEvent(org.javacord.api.event.server.emoji.KnownCustomEmojiChangeWhitelistedRolesEvent) KnownCustomEmojiImpl(org.javacord.core.entity.emoji.KnownCustomEmojiImpl) KnownCustomEmoji(org.javacord.api.entity.emoji.KnownCustomEmoji) KnownCustomEmojiChangeNameEvent(org.javacord.api.event.server.emoji.KnownCustomEmojiChangeNameEvent) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KnownCustomEmojiChangeWhitelistedRolesEventImpl(org.javacord.core.event.server.emoji.KnownCustomEmojiChangeWhitelistedRolesEventImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) Role(org.javacord.api.entity.permission.Role) ServerImpl(org.javacord.core.entity.server.ServerImpl) KnownCustomEmojiChangeWhitelistedRolesEvent(org.javacord.api.event.server.emoji.KnownCustomEmojiChangeWhitelistedRolesEvent) KnownCustomEmojiCreateEvent(org.javacord.api.event.server.emoji.KnownCustomEmojiCreateEvent) KnownCustomEmojiDeleteEventImpl(org.javacord.core.event.server.emoji.KnownCustomEmojiDeleteEventImpl) KnownCustomEmojiImpl(org.javacord.core.entity.emoji.KnownCustomEmojiImpl) KnownCustomEmojiChangeNameEventImpl(org.javacord.core.event.server.emoji.KnownCustomEmojiChangeNameEventImpl) KnownCustomEmojiDeleteEvent(org.javacord.api.event.server.emoji.KnownCustomEmojiDeleteEvent) KnownCustomEmojiCreateEventImpl(org.javacord.core.event.server.emoji.KnownCustomEmojiCreateEventImpl)

Example 23 with ServerImpl

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

the class GuildBanRemoveHandler 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);
        ServerMemberUnbanEvent event = new ServerMemberUnbanEventImpl(server, user);
        api.getEventDispatcher().dispatchServerMemberUnbanEvent(server, server, user, event);
    });
}
Also used : ServerImpl(org.javacord.core.entity.server.ServerImpl) ServerMemberUnbanEventImpl(org.javacord.core.event.server.member.ServerMemberUnbanEventImpl) 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) MemberImpl(org.javacord.core.entity.user.MemberImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) ServerMemberUnbanEvent(org.javacord.api.event.server.member.ServerMemberUnbanEvent) ServerMemberUnbanEventImpl(org.javacord.core.event.server.member.ServerMemberUnbanEventImpl) User(org.javacord.api.entity.user.User) ServerImpl(org.javacord.core.entity.server.ServerImpl) UserImpl(org.javacord.core.entity.user.UserImpl) ServerMemberUnbanEvent(org.javacord.api.event.server.member.ServerMemberUnbanEvent)

Example 24 with ServerImpl

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

the class GuildMemberAddHandler method handle.

@Override
public void handle(JsonNode packet) {
    api.getPossiblyUnreadyServerById(packet.get("guild_id").asLong()).map(server -> (ServerImpl) server).ifPresent(server -> {
        MemberImpl member = server.addMember(packet);
        server.incrementMemberCount();
        User user = new UserImpl(api, packet.get("user"), member, server);
        ServerMemberJoinEvent event = new ServerMemberJoinEventImpl(server, user);
        api.getEventDispatcher().dispatchServerMemberJoinEvent(server, server, user, event);
    });
}
Also used : ServerImpl(org.javacord.core.entity.server.ServerImpl) ServerMemberJoinEventImpl(org.javacord.core.event.server.member.ServerMemberJoinEventImpl) ServerMemberJoinEvent(org.javacord.api.event.server.member.ServerMemberJoinEvent) 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) MemberImpl(org.javacord.core.entity.user.MemberImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) ServerMemberJoinEvent(org.javacord.api.event.server.member.ServerMemberJoinEvent) User(org.javacord.api.entity.user.User) ServerImpl(org.javacord.core.entity.server.ServerImpl) ServerMemberJoinEventImpl(org.javacord.core.event.server.member.ServerMemberJoinEventImpl) MemberImpl(org.javacord.core.entity.user.MemberImpl) UserImpl(org.javacord.core.entity.user.UserImpl)

Example 25 with ServerImpl

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

the class GuildRoleDeleteHandler method handle.

@Override
public void handle(JsonNode packet) {
    long serverId = packet.get("guild_id").asLong();
    api.getPossiblyUnreadyServerById(serverId).map(server -> ((ServerImpl) server)).ifPresent(server -> {
        long roleId = packet.get("role_id").asLong();
        server.getRoleById(roleId).ifPresent(role -> {
            server.removeRole(roleId);
            RoleDeleteEvent event = new RoleDeleteEventImpl(role);
            api.getEventDispatcher().dispatchRoleDeleteEvent(server, role, server, event);
        });
    });
}
Also used : ServerImpl(org.javacord.core.entity.server.ServerImpl) RoleDeleteEventImpl(org.javacord.core.event.server.role.RoleDeleteEventImpl) RoleDeleteEvent(org.javacord.api.event.server.role.RoleDeleteEvent) PacketHandler(org.javacord.core.util.gateway.PacketHandler) DiscordApi(org.javacord.api.DiscordApi) JsonNode(com.fasterxml.jackson.databind.JsonNode) RoleDeleteEvent(org.javacord.api.event.server.role.RoleDeleteEvent) RoleDeleteEventImpl(org.javacord.core.event.server.role.RoleDeleteEventImpl)

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