Search in sources :

Example 1 with EmbedBuilderDelegateImpl

use of org.javacord.core.entity.message.embed.EmbedBuilderDelegateImpl in project Javacord by BtoBastian.

the class MessageBuilderBaseDelegateImpl method checkForAttachmentsAndExecuteRequest.

// //////////////////////////////////////////////////////////////////////////////
// Internal MessageBuilder utility methods
// //////////////////////////////////////////////////////////////////////////////
private CompletableFuture<Message> checkForAttachmentsAndExecuteRequest(TextChannel channel, ObjectNode body, RestRequest<Message> request, boolean clearAttachmentsIfAppropriate) {
    if (attachments.isEmpty() && embeds.stream().noneMatch(EmbedBuilder::requiresAttachments)) {
        if (clearAttachmentsIfAppropriate) {
            body.set("attachments", JsonNodeFactory.instance.objectNode().arrayNode());
        }
        return executeRequestWithoutAttachments(channel, body, request);
    }
    CompletableFuture<Message> future = new CompletableFuture<>();
    // We access files etc. so this should be async
    channel.getApi().getThreadPool().getExecutorService().submit(() -> {
        try {
            List<FileContainer> tempAttachments = new ArrayList<>(attachments);
            // Add the attachments required for the embeds
            for (EmbedBuilder embed : embeds) {
                tempAttachments.addAll(((EmbedBuilderDelegateImpl) embed.getDelegate()).getRequiredAttachments());
            }
            addMultipartBodyToRequest(request, body, tempAttachments, channel.getApi());
            request.execute(result -> ((DiscordApiImpl) channel.getApi()).getOrCreateMessage(channel, result.getJsonBody())).whenComplete((newMessage, throwable) -> {
                if (throwable != null) {
                    future.completeExceptionally(throwable);
                } else {
                    future.complete(newMessage);
                }
            });
        } catch (Throwable t) {
            future.completeExceptionally(t);
        }
    });
    return future;
}
Also used : MessageAttachment(org.javacord.api.entity.message.MessageAttachment) Arrays(java.util.Arrays) Member(org.javacord.core.entity.user.Member) URL(java.net.URL) RestMethod(org.javacord.core.util.rest.RestMethod) FileContainer(org.javacord.core.util.FileContainer) HighLevelComponent(org.javacord.api.entity.message.component.HighLevelComponent) RestRequest(org.javacord.core.util.rest.RestRequest) EmbedBuilderDelegateImpl(org.javacord.core.entity.message.embed.EmbedBuilderDelegateImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) ComponentType(org.javacord.api.entity.message.component.ComponentType) MediaType(okhttp3.MediaType) ActionRowBuilder(org.javacord.api.entity.message.component.ActionRowBuilder) MessageBuilderBaseDelegate(org.javacord.api.entity.message.internal.MessageBuilderBaseDelegate) BufferedImage(java.awt.image.BufferedImage) Messageable(org.javacord.api.entity.message.Messageable) Collection(java.util.Collection) Set(java.util.Set) EmbedBuilder(org.javacord.api.entity.message.embed.EmbedBuilder) AllowedMentions(org.javacord.api.entity.message.mention.AllowedMentions) Message(org.javacord.api.entity.message.Message) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) RestEndpoint(org.javacord.core.util.rest.RestEndpoint) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MultipartBody(okhttp3.MultipartBody) Mentionable(org.javacord.api.entity.Mentionable) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) AllowedMentionsImpl(org.javacord.core.entity.message.mention.AllowedMentionsImpl) CompletableFuture(java.util.concurrent.CompletableFuture) ComponentImpl(org.javacord.core.entity.message.component.ComponentImpl) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) IncomingWebhook(org.javacord.api.entity.webhook.IncomingWebhook) RequestBody(okhttp3.RequestBody) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MessageDecoration(org.javacord.api.entity.message.MessageDecoration) URLConnection(java.net.URLConnection) Icon(org.javacord.api.entity.Icon) TextChannel(org.javacord.api.entity.channel.TextChannel) DiscordApiImpl(org.javacord.core.DiscordApiImpl) ActionRow(org.javacord.api.entity.message.component.ActionRow) LowLevelComponent(org.javacord.api.entity.message.component.LowLevelComponent) File(java.io.File) LoggerUtil(org.javacord.core.util.logging.LoggerUtil) User(org.javacord.api.entity.user.User) DiscordApi(org.javacord.api.DiscordApi) Collections(java.util.Collections) InputStream(java.io.InputStream) CompletableFuture(java.util.concurrent.CompletableFuture) EmbedBuilder(org.javacord.api.entity.message.embed.EmbedBuilder) Message(org.javacord.api.entity.message.Message) DiscordApiImpl(org.javacord.core.DiscordApiImpl) ArrayList(java.util.ArrayList) FileContainer(org.javacord.core.util.FileContainer)

Example 2 with EmbedBuilderDelegateImpl

use of org.javacord.core.entity.message.embed.EmbedBuilderDelegateImpl in project Javacord by BtoBastian.

the class UncachedMessageUtilImpl method edit.

@Override
public CompletableFuture<Message> edit(long channelId, long messageId, String content, boolean updateContent, List<EmbedBuilder> embeds, boolean updateEmbed) {
    ObjectNode body = JsonNodeFactory.instance.objectNode();
    if (updateContent) {
        if (content == null || content.isEmpty()) {
            body.putNull("content");
        } else {
            body.put("content", content);
        }
    }
    if (updateEmbed) {
        ArrayNode embedArray = body.putArray("embeds");
        embeds.stream().map(embedBuilder -> ((EmbedBuilderDelegateImpl) embedBuilder.getDelegate()).toJsonNode()).forEach(embedArray::add);
    }
    return new RestRequest<Message>(api, RestMethod.PATCH, RestEndpoint.MESSAGE).setUrlParameters(Long.toUnsignedString(channelId), Long.toUnsignedString(messageId)).setBody(body).execute(result -> new MessageImpl(api, api.getTextChannelById(channelId).orElseThrow(() -> new IllegalStateException("TextChannel is missing.")), result.getJsonBody()));
}
Also used : Arrays(java.util.Arrays) CompletableFuture(java.util.concurrent.CompletableFuture) UncachedMessageUtil(org.javacord.api.entity.message.UncachedMessageUtil) RestMethod(org.javacord.core.util.rest.RestMethod) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) DiscordEntity(org.javacord.api.entity.DiscordEntity) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) RestRequest(org.javacord.core.util.rest.RestRequest) Emoji(org.javacord.api.entity.emoji.Emoji) EmbedBuilderDelegateImpl(org.javacord.core.entity.message.embed.EmbedBuilderDelegateImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) StreamSupport(java.util.stream.StreamSupport) UserImpl(org.javacord.core.entity.user.UserImpl) MemberImpl(org.javacord.core.entity.user.MemberImpl) EmbedBuilder(org.javacord.api.entity.message.embed.EmbedBuilder) CustomEmoji(org.javacord.api.entity.emoji.CustomEmoji) DiscordApiImpl(org.javacord.core.DiscordApiImpl) Instant(java.time.Instant) Message(org.javacord.api.entity.message.Message) Collectors(java.util.stream.Collectors) InternalUncachedMessageAttachableListenerManager(org.javacord.core.listener.message.InternalUncachedMessageAttachableListenerManager) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) RestEndpoint(org.javacord.core.util.rest.RestEndpoint) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) Stream(java.util.stream.Stream) User(org.javacord.api.entity.user.User) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) DiscordApi(org.javacord.api.DiscordApi) Collections(java.util.Collections) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Message(org.javacord.api.entity.message.Message) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) EmbedBuilderDelegateImpl(org.javacord.core.entity.message.embed.EmbedBuilderDelegateImpl)

Example 3 with EmbedBuilderDelegateImpl

use of org.javacord.core.entity.message.embed.EmbedBuilderDelegateImpl in project Javacord by BtoBastian.

the class InteractionMessageBuilderDelegateImpl method checkForAttachmentsAndExecuteRequest.

private CompletableFuture<Message> checkForAttachmentsAndExecuteRequest(RestRequest<Message> request, ObjectNode body) {
    if (!attachments.isEmpty() || embeds.stream().anyMatch(EmbedBuilder::requiresAttachments)) {
        CompletableFuture<Message> future = new CompletableFuture<>();
        // We access files etc. so this should be async
        request.getApi().getThreadPool().getExecutorService().submit(() -> {
            try {
                List<FileContainer> tempAttachments = new ArrayList<>(attachments);
                // Add the attachments required for the embed
                for (EmbedBuilder embed : embeds) {
                    tempAttachments.addAll(((EmbedBuilderDelegateImpl) embed.getDelegate()).getRequiredAttachments());
                }
                addMultipartBodyToRequest(request, body, tempAttachments, request.getApi());
                request.execute(result -> request.getApi().getOrCreateMessage(request.getApi().getTextChannelById(result.getJsonBody().get("channel_id").asLong()).orElseThrow(() -> new NoSuchElementException("TextChannel is not cached")), result.getJsonBody())).whenComplete((message, throwable) -> {
                    if (throwable != null) {
                        future.completeExceptionally(throwable);
                    } else {
                        future.complete(message);
                    }
                });
            } catch (Throwable t) {
                future.completeExceptionally(t);
            }
        });
        return future;
    } else {
        request.setBody(body);
        return request.execute(result -> request.getApi().getOrCreateMessage(request.getApi().getTextChannelById(result.getJsonBody().get("channel_id").asLong()).orElseThrow(() -> new NoSuchElementException("TextChannel is not cached")), result.getJsonBody()));
    }
}
Also used : InteractionCallbackDataFlag(org.javacord.api.interaction.callback.InteractionCallbackDataFlag) EmbedBuilder(org.javacord.api.entity.message.embed.EmbedBuilder) InteractionBase(org.javacord.api.interaction.InteractionBase) CompletableFuture(java.util.concurrent.CompletableFuture) RestMethod(org.javacord.core.util.rest.RestMethod) Message(org.javacord.api.entity.message.Message) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FileContainer(org.javacord.core.util.FileContainer) ArrayList(java.util.ArrayList) RestEndpoint(org.javacord.core.util.rest.RestEndpoint) List(java.util.List) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) MessageComponentInteraction(org.javacord.api.interaction.MessageComponentInteraction) RestRequest(org.javacord.core.util.rest.RestRequest) EmbedBuilderDelegateImpl(org.javacord.core.entity.message.embed.EmbedBuilderDelegateImpl) InteractionImpl(org.javacord.core.interaction.InteractionImpl) NoSuchElementException(java.util.NoSuchElementException) EnumSet(java.util.EnumSet) InteractionMessageBuilderDelegate(org.javacord.api.entity.message.internal.InteractionMessageBuilderDelegate) CompletableFuture(java.util.concurrent.CompletableFuture) EmbedBuilder(org.javacord.api.entity.message.embed.EmbedBuilder) Message(org.javacord.api.entity.message.Message) ArrayList(java.util.ArrayList) FileContainer(org.javacord.core.util.FileContainer) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Message (org.javacord.api.entity.message.Message)3 EmbedBuilder (org.javacord.api.entity.message.embed.EmbedBuilder)3 EmbedBuilderDelegateImpl (org.javacord.core.entity.message.embed.EmbedBuilderDelegateImpl)3 RestEndpoint (org.javacord.core.util.rest.RestEndpoint)3 RestMethod (org.javacord.core.util.rest.RestMethod)3 RestRequest (org.javacord.core.util.rest.RestRequest)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 DiscordApi (org.javacord.api.DiscordApi)2 User (org.javacord.api.entity.user.User)2 DiscordApiImpl (org.javacord.core.DiscordApiImpl)2 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1