Search in sources :

Example 1 with Emoji

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

the class ButtonBuilderDelegateImpl method copy.

@Override
public void copy(Button button) {
    Optional<String> customId = button.getCustomId();
    Optional<String> url = button.getUrl();
    Optional<String> label = button.getLabel();
    Optional<Emoji> emoji = button.getEmoji();
    Optional<Boolean> isDisabled = button.isDisabled();
    ButtonStyle style = button.getStyle();
    this.setStyle(style);
    customId.ifPresent(this::setCustomId);
    url.ifPresent(this::setUrl);
    label.ifPresent(this::setLabel);
    emoji.ifPresent(this::setEmoji);
    isDisabled.ifPresent(this::setDisabled);
}
Also used : Emoji(org.javacord.api.entity.emoji.Emoji) CustomEmoji(org.javacord.api.entity.emoji.CustomEmoji) ButtonStyle(org.javacord.api.entity.message.component.ButtonStyle)

Example 2 with Emoji

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

the class MessageReactionAddHandler method handle.

@Override
public void handle(JsonNode packet) {
    long channelId = packet.get("channel_id").asLong();
    long messageId = packet.get("message_id").asLong();
    long userId = packet.get("user_id").asLong();
    String serverId = packet.hasNonNull("guild_id") ? packet.get("guild_id").asText() : null;
    TextChannel channel;
    if (serverId == null) {
        // if private channel:
        channel = PrivateChannelImpl.getOrCreatePrivateChannel(api, channelId, userId, null);
    } else {
        channel = api.getTextChannelById(channelId).orElse(null);
    }
    if (channel == null) {
        LoggerUtil.logMissingChannel(logger, channelId);
        return;
    }
    Optional<Server> server = api.getServerById(serverId);
    Member member = null;
    if (packet.hasNonNull("member") && server.isPresent()) {
        member = new MemberImpl(api, (ServerImpl) server.get(), packet.get("member"), null);
    }
    Optional<Message> message = api.getCachedMessageById(messageId);
    Emoji emoji;
    JsonNode emojiJson = packet.get("emoji");
    if (!emojiJson.has("id") || emojiJson.get("id").isNull()) {
        emoji = UnicodeEmojiImpl.fromString(emojiJson.get("name").asText());
    } else {
        emoji = api.getKnownCustomEmojiOrCreateCustomEmoji(emojiJson);
    }
    message.ifPresent(msg -> ((MessageImpl) msg).addReaction(emoji, userId == api.getYourself().getId()));
    ReactionAddEvent event = new ReactionAddEventImpl(api, messageId, channel, emoji, userId, member);
    api.getEventDispatcher().dispatchReactionAddEvent(server.map(DispatchQueueSelector.class::cast).orElse(api), messageId, server.orElse(null), channel, userId, event);
}
Also used : ReactionAddEventImpl(org.javacord.core.event.message.reaction.ReactionAddEventImpl) Server(org.javacord.api.entity.server.Server) Message(org.javacord.api.entity.message.Message) MemberImpl(org.javacord.core.entity.user.MemberImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) ReactionAddEvent(org.javacord.api.event.message.reaction.ReactionAddEvent) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector) TextChannel(org.javacord.api.entity.channel.TextChannel) ServerImpl(org.javacord.core.entity.server.ServerImpl) Emoji(org.javacord.api.entity.emoji.Emoji) Member(org.javacord.core.entity.user.Member)

Example 3 with Emoji

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

the class MessageReactionRemoveHandler method handle.

@Override
public void handle(JsonNode packet) {
    long messageId = packet.get("message_id").asLong();
    long userId = packet.get("user_id").asLong();
    Optional<Message> message = api.getCachedMessageById(messageId);
    long channelId = packet.get("channel_id").asLong();
    TextChannel channel;
    if (packet.hasNonNull("guild_id")) {
        channel = api.getTextChannelById(channelId).orElse(null);
    } else {
        // if private channel:
        channel = PrivateChannelImpl.getOrCreatePrivateChannel(api, channelId, userId, null);
    }
    if (channel == null) {
        LoggerUtil.logMissingChannel(logger, channelId);
        return;
    }
    Emoji emoji;
    JsonNode emojiJson = packet.get("emoji");
    if (!emojiJson.has("id") || emojiJson.get("id").isNull()) {
        emoji = UnicodeEmojiImpl.fromString(emojiJson.get("name").asText());
    } else {
        emoji = api.getKnownCustomEmojiOrCreateCustomEmoji(emojiJson);
    }
    message.ifPresent(msg -> ((MessageImpl) msg).removeReaction(emoji, userId == api.getYourself().getId()));
    ReactionRemoveEvent event = new ReactionRemoveEventImpl(api, messageId, channel, emoji, userId);
    Optional<Server> optionalServer = channel.asServerChannel().map(ServerChannel::getServer);
    api.getEventDispatcher().dispatchReactionRemoveEvent(optionalServer.map(DispatchQueueSelector.class::cast).orElse(api), messageId, optionalServer.orElse(null), channel, userId, event);
}
Also used : ReactionRemoveEvent(org.javacord.api.event.message.reaction.ReactionRemoveEvent) TextChannel(org.javacord.api.entity.channel.TextChannel) Message(org.javacord.api.entity.message.Message) Server(org.javacord.api.entity.server.Server) ReactionRemoveEventImpl(org.javacord.core.event.message.reaction.ReactionRemoveEventImpl) Emoji(org.javacord.api.entity.emoji.Emoji) JsonNode(com.fasterxml.jackson.databind.JsonNode) ServerChannel(org.javacord.api.entity.channel.ServerChannel) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector)

Example 4 with Emoji

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

Emoji (org.javacord.api.entity.emoji.Emoji)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 TextChannel (org.javacord.api.entity.channel.TextChannel)2 CustomEmoji (org.javacord.api.entity.emoji.CustomEmoji)2 Message (org.javacord.api.entity.message.Message)2 ButtonStyle (org.javacord.api.entity.message.component.ButtonStyle)2 Server (org.javacord.api.entity.server.Server)2 DispatchQueueSelector (org.javacord.core.util.event.DispatchQueueSelector)2 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Optional (java.util.Optional)1 ServerChannel (org.javacord.api.entity.channel.ServerChannel)1 Button (org.javacord.api.entity.message.component.Button)1 ComponentType (org.javacord.api.entity.message.component.ComponentType)1 ReactionAddEvent (org.javacord.api.event.message.reaction.ReactionAddEvent)1 ReactionRemoveEvent (org.javacord.api.event.message.reaction.ReactionRemoveEvent)1 CustomEmojiImpl (org.javacord.core.entity.emoji.CustomEmojiImpl)1 UnicodeEmojiImpl (org.javacord.core.entity.emoji.UnicodeEmojiImpl)1 ServerImpl (org.javacord.core.entity.server.ServerImpl)1 Member (org.javacord.core.entity.user.Member)1