Search in sources :

Example 1 with Webhook

use of org.javacord.api.entity.webhook.Webhook in project Javacord by BtoBastian.

the class WebhookUpdaterDelegateImpl method update.

@Override
public CompletableFuture<Webhook> update() {
    boolean patchWebhook = false;
    ObjectNode body = JsonNodeFactory.instance.objectNode();
    if (name != null) {
        body.put("name", name);
        patchWebhook = true;
    }
    if (channel != null) {
        body.put("channel_id", channel.getIdAsString());
        patchWebhook = true;
    }
    if (updateAvatar) {
        if (avatar == null) {
            body.putNull("avatar");
        }
        patchWebhook = true;
    }
    if (patchWebhook) {
        if (avatar != null) {
            return avatar.asByteArray(webhook.getApi()).thenAccept(bytes -> {
                String base64Avatar = "data:image/" + avatar.getFileType() + ";base64," + Base64.getEncoder().encodeToString(bytes);
                body.put("avatar", base64Avatar);
            }).thenCompose(aVoid -> setUrlParameters(new RestRequest<>(webhook.getApi(), RestMethod.PATCH, RestEndpoint.WEBHOOK)).setBody(body).setAuditLogReason(reason).execute(result -> WebhookImpl.createWebhook(webhook.getApi(), result.getJsonBody())));
        }
        return setUrlParameters(new RestRequest<>(webhook.getApi(), RestMethod.PATCH, RestEndpoint.WEBHOOK)).setBody(body).setAuditLogReason(reason).execute(result -> WebhookImpl.createWebhook(webhook.getApi(), result.getJsonBody()));
    } else {
        return CompletableFuture.completedFuture(webhook);
    }
}
Also used : Webhook(org.javacord.api.entity.webhook.Webhook) BufferedImage(java.awt.image.BufferedImage) URL(java.net.URL) CompletableFuture(java.util.concurrent.CompletableFuture) RestMethod(org.javacord.core.util.rest.RestMethod) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) IncomingWebhook(org.javacord.api.entity.webhook.IncomingWebhook) FileContainer(org.javacord.core.util.FileContainer) File(java.io.File) WebhookUpdaterDelegate(org.javacord.api.entity.webhook.internal.WebhookUpdaterDelegate) RestEndpoint(org.javacord.core.util.rest.RestEndpoint) Base64(java.util.Base64) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Icon(org.javacord.api.entity.Icon) RestRequest(org.javacord.core.util.rest.RestRequest) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) Optional(java.util.Optional) InputStream(java.io.InputStream) RestRequest(org.javacord.core.util.rest.RestRequest) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode)

Aggregations

JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 Base64 (java.util.Base64)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Icon (org.javacord.api.entity.Icon)1 ServerTextChannel (org.javacord.api.entity.channel.ServerTextChannel)1 IncomingWebhook (org.javacord.api.entity.webhook.IncomingWebhook)1 Webhook (org.javacord.api.entity.webhook.Webhook)1 WebhookUpdaterDelegate (org.javacord.api.entity.webhook.internal.WebhookUpdaterDelegate)1 FileContainer (org.javacord.core.util.FileContainer)1 RestEndpoint (org.javacord.core.util.rest.RestEndpoint)1 RestMethod (org.javacord.core.util.rest.RestMethod)1 RestRequest (org.javacord.core.util.rest.RestRequest)1