Search in sources :

Example 1 with FileContainer

use of org.javacord.core.util.FileContainer in project Javacord by BtoBastian.

the class MessageBuilderBaseDelegateImpl method addAttachmentAsSpoiler.

@Override
public void addAttachmentAsSpoiler(URL url) {
    if (url == null) {
        throw new IllegalArgumentException("url cannot be null!");
    }
    attachments.add(new FileContainer(url, true));
    attachmentsChanged = true;
}
Also used : FileContainer(org.javacord.core.util.FileContainer)

Example 2 with FileContainer

use of org.javacord.core.util.FileContainer 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 3 with FileContainer

use of org.javacord.core.util.FileContainer in project Javacord by BtoBastian.

the class MessageBuilderBaseDelegateImpl method addAttachmentAsSpoiler.

@Override
public void addAttachmentAsSpoiler(File file) {
    if (file == null) {
        throw new IllegalArgumentException("file cannot be null!");
    }
    attachments.add(new FileContainer(file, true));
    attachmentsChanged = true;
}
Also used : FileContainer(org.javacord.core.util.FileContainer)

Example 4 with FileContainer

use of org.javacord.core.util.FileContainer in project Javacord by BtoBastian.

the class MessageBuilderBaseDelegateImpl method addAttachment.

@Override
public void addAttachment(File file) {
    if (file == null) {
        throw new IllegalArgumentException("file cannot be null!");
    }
    attachments.add(new FileContainer(file));
    attachmentsChanged = true;
}
Also used : FileContainer(org.javacord.core.util.FileContainer)

Example 5 with FileContainer

use of org.javacord.core.util.FileContainer in project Javacord by BtoBastian.

the class MessageBuilderBaseDelegateImpl method addAttachment.

@Override
public void addAttachment(URL url) {
    if (url == null) {
        throw new IllegalArgumentException("url cannot be null!");
    }
    attachments.add(new FileContainer(url));
    attachmentsChanged = true;
}
Also used : FileContainer(org.javacord.core.util.FileContainer)

Aggregations

FileContainer (org.javacord.core.util.FileContainer)29 RestRequest (org.javacord.core.util.rest.RestRequest)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 ArrayList (java.util.ArrayList)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Message (org.javacord.api.entity.message.Message)3 EmbedBuilder (org.javacord.api.entity.message.embed.EmbedBuilder)3 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)2 List (java.util.List)2 MultipartBody (okhttp3.MultipartBody)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1