Search in sources :

Example 6 with FileContainer

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

the class MessageBuilderBaseDelegateImpl method addAttachmentAsSpoiler.

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

Example 7 with FileContainer

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

the class MessageBuilderBaseDelegateImpl method addAttachment.

@Override
public void addAttachment(byte[] bytes, String fileName) {
    if (bytes == null || fileName == null) {
        throw new IllegalArgumentException("bytes and fileName cannot be null!");
    }
    attachments.add(new FileContainer(bytes, fileName));
    attachmentsChanged = true;
}
Also used : FileContainer(org.javacord.core.util.FileContainer)

Example 8 with FileContainer

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

the class MessageBuilderBaseDelegateImpl method send.

/**
 * Send a message to an incoming webhook.
 *
 * @param webhookId The id of the webhook to send the message to
 * @param webhookToken The token of the webhook to send the message to
 * @param displayName The display name the webhook should use
 * @param avatarUrl The avatar the webhook should use
 * @param wait If the completable future will be completed
 * @param api The api instance needed to send and return the message
 * @return The sent message
 */
protected CompletableFuture<Message> send(String webhookId, String webhookToken, String displayName, URL avatarUrl, boolean wait, DiscordApi api) {
    ObjectNode body = JsonNodeFactory.instance.objectNode();
    prepareCommonWebhookMessageBodyParts(body);
    if (displayName != null) {
        body.put("username", displayName);
    }
    if (avatarUrl != null) {
        body.put("avatar_url", avatarUrl.toExternalForm());
    }
    prepareComponents(body);
    if (strBuilder.length() != 0) {
        body.put("content", strBuilder.toString());
    }
    RestRequest<Message> request = new RestRequest<Message>(api, RestMethod.POST, RestEndpoint.WEBHOOK_SEND).addQueryParameter("wait", Boolean.toString(wait)).setUrlParameters(webhookId, webhookToken);
    CompletableFuture<Message> future = new CompletableFuture<>();
    if (!attachments.isEmpty() || embeds.stream().anyMatch(EmbedBuilder::requiresAttachments)) {
        // We access files etc. so this should be async
        api.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, api);
                executeWebhookRest(request, wait, future, api);
            } catch (Throwable t) {
                future.completeExceptionally(t);
            }
        });
    } else {
        request.setBody(body);
        executeWebhookRest(request, wait, future, api);
    }
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) EmbedBuilder(org.javacord.api.entity.message.embed.EmbedBuilder) RestRequest(org.javacord.core.util.rest.RestRequest) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Message(org.javacord.api.entity.message.Message) ArrayList(java.util.ArrayList) FileContainer(org.javacord.core.util.FileContainer)

Example 9 with FileContainer

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

the class EmbedBuilderDelegateImpl method setThumbnail.

@Override
public void setThumbnail(File thumbnail) {
    thumbnailUrl = null;
    if (thumbnail == null) {
        thumbnailContainer = null;
    } else {
        thumbnailContainer = new FileContainer(thumbnail);
        thumbnailContainer.setFileTypeOrName(UUID.randomUUID().toString() + "." + FileUtils.getExtension(thumbnail));
    }
}
Also used : FileContainer(org.javacord.core.util.FileContainer)

Example 10 with FileContainer

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

the class EmbedBuilderDelegateImpl method setAuthor.

@Override
public void setAuthor(String name, String url, File icon) {
    authorName = name;
    authorUrl = url;
    authorIconUrl = null;
    if (icon == null) {
        authorIconContainer = null;
    } else {
        authorIconContainer = new FileContainer(icon);
        authorIconContainer.setFileTypeOrName(UUID.randomUUID().toString() + "." + FileUtils.getExtension(icon));
    }
}
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