Search in sources :

Example 1 with CustomEmoji

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

the class DiscordApiImpl method getKnownCustomEmojiOrCreateCustomEmoji.

/**
 * Gets a known custom emoji or creates a new (unknown) custom emoji object.
 *
 * @param data The data of the emoji.
 * @return The emoji for the given json object.
 */
public CustomEmoji getKnownCustomEmojiOrCreateCustomEmoji(JsonNode data) {
    long id = Long.parseLong(data.get("id").asText());
    CustomEmoji emoji = customEmojis.get(id);
    return emoji == null ? new CustomEmojiImpl(this, data) : emoji;
}
Also used : CustomEmoji(org.javacord.api.entity.emoji.CustomEmoji) KnownCustomEmoji(org.javacord.api.entity.emoji.KnownCustomEmoji) KnownCustomEmojiImpl(org.javacord.core.entity.emoji.KnownCustomEmojiImpl) CustomEmojiImpl(org.javacord.core.entity.emoji.CustomEmojiImpl)

Example 2 with CustomEmoji

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

the class MessageImpl method getCustomEmojis.

@Override
public List<CustomEmoji> getCustomEmojis() {
    String content = getContent();
    List<CustomEmoji> emojis = new ArrayList<>();
    Matcher customEmoji = DiscordRegexPattern.CUSTOM_EMOJI.matcher(content);
    while (customEmoji.find()) {
        long id = Long.parseLong(customEmoji.group("id"));
        String name = customEmoji.group("name");
        boolean animated = customEmoji.group(0).charAt(1) == 'a';
        // TODO Maybe it would be better to cache the custom emoji objects inside the message object
        CustomEmoji emoji = ((DiscordApiImpl) getApi()).getKnownCustomEmojiOrCreateCustomEmoji(id, name, animated);
        emojis.add(emoji);
    }
    return Collections.unmodifiableList(emojis);
}
Also used : CustomEmoji(org.javacord.api.entity.emoji.CustomEmoji) DiscordApiImpl(org.javacord.core.DiscordApiImpl) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList)

Example 3 with CustomEmoji

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

the class ButtonImpl method toJsonNode.

/**
 * Gets the button as a {@link ObjectNode}. This is what is sent to Discord.
 *
 * @param object The object, the data should be added to.
 * @return The button as a ObjectNode.
 */
public ObjectNode toJsonNode(ObjectNode object) {
    object.put("type", ComponentType.BUTTON.value());
    // 1. Style is not optional; Buttons without a style are not accepted
    if (style == null) {
        throw new IllegalStateException("Button style is null.");
    }
    object.put("style", style.getValue());
    if (label != null && !label.equals("")) {
        object.put("label", label);
    }
    // 2. Non-link buttons must have a custom_id, and cannot have a url
    if (style != ButtonStyle.LINK) {
        if (customId == null || customId.equals("")) {
            throw new IllegalStateException("Button is missing a custom identifier.");
        } else if (url != null) {
            throw new IllegalStateException("A non-button link must not have a URL.");
        }
        object.put("custom_id", customId);
    }
    // 3. Link buttons must have an url, and cannot have a custom_id
    if (style == ButtonStyle.LINK) {
        if (url == null || url.equals("")) {
            throw new IllegalStateException("Button link is missing a URL.");
        }
        if (customId != null) {
            throw new IllegalStateException("Button link must not have a custom identifier");
        }
        object.put("url", url);
    }
    if (disabled != null) {
        object.put("disabled", disabled);
    }
    if (emoji != null) {
        ObjectNode emojiObj = JsonNodeFactory.instance.objectNode();
        if (emoji.isUnicodeEmoji()) {
            Optional<String> unicodeEmojiOptional = emoji.asUnicodeEmoji();
            unicodeEmojiOptional.ifPresent(emojiName -> emojiObj.put("name", emojiName));
        } else if (emoji.isCustomEmoji()) {
            Optional<CustomEmoji> customEmojiOptional = emoji.asCustomEmoji();
            customEmojiOptional.ifPresent(customEmoji -> emojiObj.put("id", customEmoji.getId()));
        }
        object.set("emoji", emojiObj);
    }
    return object;
}
Also used : ButtonStyle(org.javacord.api.entity.message.component.ButtonStyle) UnicodeEmojiImpl(org.javacord.core.entity.emoji.UnicodeEmojiImpl) Button(org.javacord.api.entity.message.component.Button) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Emoji(org.javacord.api.entity.emoji.Emoji) Optional(java.util.Optional) JsonNode(com.fasterxml.jackson.databind.JsonNode) CustomEmoji(org.javacord.api.entity.emoji.CustomEmoji) ComponentType(org.javacord.api.entity.message.component.ComponentType) CustomEmojiImpl(org.javacord.core.entity.emoji.CustomEmojiImpl) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Optional(java.util.Optional)

Aggregations

CustomEmoji (org.javacord.api.entity.emoji.CustomEmoji)3 CustomEmojiImpl (org.javacord.core.entity.emoji.CustomEmojiImpl)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1 Matcher (java.util.regex.Matcher)1 Emoji (org.javacord.api.entity.emoji.Emoji)1 KnownCustomEmoji (org.javacord.api.entity.emoji.KnownCustomEmoji)1 Button (org.javacord.api.entity.message.component.Button)1 ButtonStyle (org.javacord.api.entity.message.component.ButtonStyle)1 ComponentType (org.javacord.api.entity.message.component.ComponentType)1 DiscordApiImpl (org.javacord.core.DiscordApiImpl)1 KnownCustomEmojiImpl (org.javacord.core.entity.emoji.KnownCustomEmojiImpl)1 UnicodeEmojiImpl (org.javacord.core.entity.emoji.UnicodeEmojiImpl)1