Search in sources :

Example 1 with KnownCustomEmoji

use of org.javacord.api.entity.emoji.KnownCustomEmoji in project Javacord by BtoBastian.

the class CustomEmojiBuilderDelegateImpl method create.

@Override
public CompletableFuture<KnownCustomEmoji> create() {
    if (name == null) {
        throw new IllegalStateException("The name is no optional parameter!");
    }
    if (image == null) {
        throw new IllegalStateException("The image is no optional parameter!");
    }
    ObjectNode body = JsonNodeFactory.instance.objectNode().put("name", name);
    if (whitelist != null) {
        ArrayNode jsonRoles = body.putArray("roles");
        whitelist.stream().map(Role::getIdAsString).forEach(jsonRoles::add);
    }
    return image.asByteArray(server.getApi()).thenAccept(bytes -> {
        String base64Icon = "data:image/" + image.getFileType() + ";base64," + Base64.getEncoder().encodeToString(bytes);
        body.put("image", base64Icon);
    }).thenCompose(aVoid -> new RestRequest<KnownCustomEmoji>(server.getApi(), RestMethod.POST, RestEndpoint.CUSTOM_EMOJI).setUrlParameters(server.getIdAsString()).setBody(body).setAuditLogReason(reason).execute(result -> ((DiscordApiImpl) server.getApi()).getOrCreateKnownCustomEmoji(server, result.getJsonBody())));
}
Also used : ServerImpl(org.javacord.core.entity.server.ServerImpl) KnownCustomEmoji(org.javacord.api.entity.emoji.KnownCustomEmoji) CustomEmojiBuilderDelegate(org.javacord.api.entity.emoji.internal.CustomEmojiBuilderDelegate) Arrays(java.util.Arrays) BufferedImage(java.awt.image.BufferedImage) URL(java.net.URL) Collection(java.util.Collection) CompletableFuture(java.util.concurrent.CompletableFuture) DiscordApiImpl(org.javacord.core.DiscordApiImpl) RestMethod(org.javacord.core.util.rest.RestMethod) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FileContainer(org.javacord.core.util.FileContainer) File(java.io.File) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) HashSet(java.util.HashSet) RestEndpoint(org.javacord.core.util.rest.RestEndpoint) Base64(java.util.Base64) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Role(org.javacord.api.entity.permission.Role) Icon(org.javacord.api.entity.Icon) RestRequest(org.javacord.core.util.rest.RestRequest) InputStream(java.io.InputStream) KnownCustomEmoji(org.javacord.api.entity.emoji.KnownCustomEmoji) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DiscordApiImpl(org.javacord.core.DiscordApiImpl) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 2 with KnownCustomEmoji

use of org.javacord.api.entity.emoji.KnownCustomEmoji 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)

Aggregations

Collection (java.util.Collection)2 KnownCustomEmoji (org.javacord.api.entity.emoji.KnownCustomEmoji)2 Role (org.javacord.api.entity.permission.Role)2 ServerImpl (org.javacord.core.entity.server.ServerImpl)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Base64 (java.util.Base64)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1