Search in sources :

Example 1 with StickerChangeTagsEvent

use of org.javacord.api.event.server.sticker.StickerChangeTagsEvent in project Javacord by BtoBastian.

the class GuildStickersUpdateHandler method handle.

@Override
protected void handle(JsonNode packet) {
    long serverId = packet.get("guild_id").asLong();
    api.getPossiblyUnreadyServerById(serverId).map(server -> (ServerImpl) server).ifPresent(server -> {
        HashMap<Long, JsonNode> stickers = new HashMap<>();
        for (JsonNode sticketJson : packet.get("stickers")) {
            stickers.put(sticketJson.get("id").asLong(), sticketJson);
        }
        stickers.forEach((key, value) -> {
            Optional<Sticker> optionalSticker = server.getStickerById(key);
            if (optionalSticker.isPresent()) {
                Sticker sticker = optionalSticker.get();
                String oldName = sticker.getName();
                String newName = value.get("name").asText();
                if (!Objects.deepEquals(oldName, newName)) {
                    StickerChangeNameEvent event = new StickerChangeNameEventImpl(sticker, oldName, newName);
                    ((StickerImpl) sticker).setName(newName);
                    api.getEventDispatcher().dispatchStickerChangeNameEvent(server, server, sticker, event);
                }
                String oldDescription = sticker.getDescription();
                String newDescription = value.get("description").asText();
                if (!Objects.deepEquals(oldDescription, newDescription)) {
                    StickerChangeDescriptionEvent event = new StickerChangeDescriptionEventImpl(sticker, oldDescription, newDescription);
                    ((StickerImpl) sticker).setDescription(newDescription);
                    api.getEventDispatcher().dispatchStickerChangeDescriptionEvent(server, server, sticker, event);
                }
                String oldTags = sticker.getTags();
                String newTags = value.get("tags").asText();
                if (!Objects.deepEquals(oldTags, newTags)) {
                    StickerChangeTagsEvent event = new StickerChangeTagsEventImpl(sticker, oldTags, newTags);
                    ((StickerImpl) sticker).setTags(newTags);
                    api.getEventDispatcher().dispatchStickerChangeTagsEvent(server, server, sticker, event);
                }
            } else {
                Sticker sticker = api.getOrCreateSticker(value);
                server.addSticker(sticker);
                StickerCreateEvent event = new StickerCreateEventImpl(sticker);
                api.getEventDispatcher().dispatchStickerCreateEvent(server, server, event);
            }
        });
        Set<Long> stickerIds = stickers.keySet();
        server.getStickers().stream().filter(sticker -> !stickerIds.contains(sticker.getId())).forEach(sticker -> {
            api.removeSticker(sticker);
            server.removeSticker(sticker);
            StickerDeleteEvent event = new StickerDeleteEventImpl(sticker);
            api.getEventDispatcher().dispatchStickerDeleteEvent(server, server, sticker, event);
        });
    });
}
Also used : ServerImpl(org.javacord.core.entity.server.ServerImpl) StickerChangeNameEventImpl(org.javacord.core.event.server.sticker.StickerChangeNameEventImpl) StickerCreateEvent(org.javacord.api.event.server.sticker.StickerCreateEvent) StickerDeleteEvent(org.javacord.api.event.server.sticker.StickerDeleteEvent) StickerChangeDescriptionEvent(org.javacord.api.event.server.sticker.StickerChangeDescriptionEvent) StickerDeleteEventImpl(org.javacord.core.event.server.sticker.StickerDeleteEventImpl) Set(java.util.Set) HashMap(java.util.HashMap) StickerChangeTagsEventImpl(org.javacord.core.event.server.sticker.StickerChangeTagsEventImpl) StickerCreateEventImpl(org.javacord.core.event.server.sticker.StickerCreateEventImpl) StickerImpl(org.javacord.core.entity.sticker.StickerImpl) Objects(java.util.Objects) Sticker(org.javacord.api.entity.sticker.Sticker) PacketHandler(org.javacord.core.util.gateway.PacketHandler) DiscordApi(org.javacord.api.DiscordApi) StickerChangeNameEvent(org.javacord.api.event.server.sticker.StickerChangeNameEvent) Optional(java.util.Optional) JsonNode(com.fasterxml.jackson.databind.JsonNode) StickerChangeTagsEvent(org.javacord.api.event.server.sticker.StickerChangeTagsEvent) StickerChangeDescriptionEventImpl(org.javacord.core.event.server.sticker.StickerChangeDescriptionEventImpl) StickerDeleteEvent(org.javacord.api.event.server.sticker.StickerDeleteEvent) HashMap(java.util.HashMap) StickerCreateEvent(org.javacord.api.event.server.sticker.StickerCreateEvent) StickerChangeNameEventImpl(org.javacord.core.event.server.sticker.StickerChangeNameEventImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) StickerChangeDescriptionEvent(org.javacord.api.event.server.sticker.StickerChangeDescriptionEvent) StickerImpl(org.javacord.core.entity.sticker.StickerImpl) Sticker(org.javacord.api.entity.sticker.Sticker) StickerChangeTagsEventImpl(org.javacord.core.event.server.sticker.StickerChangeTagsEventImpl) StickerChangeNameEvent(org.javacord.api.event.server.sticker.StickerChangeNameEvent) ServerImpl(org.javacord.core.entity.server.ServerImpl) StickerChangeDescriptionEventImpl(org.javacord.core.event.server.sticker.StickerChangeDescriptionEventImpl) StickerCreateEventImpl(org.javacord.core.event.server.sticker.StickerCreateEventImpl) StickerDeleteEventImpl(org.javacord.core.event.server.sticker.StickerDeleteEventImpl) StickerChangeTagsEvent(org.javacord.api.event.server.sticker.StickerChangeTagsEvent)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 HashMap (java.util.HashMap)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 DiscordApi (org.javacord.api.DiscordApi)1 Sticker (org.javacord.api.entity.sticker.Sticker)1 StickerChangeDescriptionEvent (org.javacord.api.event.server.sticker.StickerChangeDescriptionEvent)1 StickerChangeNameEvent (org.javacord.api.event.server.sticker.StickerChangeNameEvent)1 StickerChangeTagsEvent (org.javacord.api.event.server.sticker.StickerChangeTagsEvent)1 StickerCreateEvent (org.javacord.api.event.server.sticker.StickerCreateEvent)1 StickerDeleteEvent (org.javacord.api.event.server.sticker.StickerDeleteEvent)1 ServerImpl (org.javacord.core.entity.server.ServerImpl)1 StickerImpl (org.javacord.core.entity.sticker.StickerImpl)1 StickerChangeDescriptionEventImpl (org.javacord.core.event.server.sticker.StickerChangeDescriptionEventImpl)1 StickerChangeNameEventImpl (org.javacord.core.event.server.sticker.StickerChangeNameEventImpl)1 StickerChangeTagsEventImpl (org.javacord.core.event.server.sticker.StickerChangeTagsEventImpl)1 StickerCreateEventImpl (org.javacord.core.event.server.sticker.StickerCreateEventImpl)1 StickerDeleteEventImpl (org.javacord.core.event.server.sticker.StickerDeleteEventImpl)1 PacketHandler (org.javacord.core.util.gateway.PacketHandler)1