Search in sources :

Example 1 with IncomingWebhook

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

the class WebhookBuilderDelegateImpl method create.

@Override
public CompletableFuture<IncomingWebhook> create() {
    if (name == null) {
        throw new IllegalStateException("Name is no optional parameter!");
    }
    ObjectNode body = JsonNodeFactory.instance.objectNode();
    body.put("name", name);
    if (avatar != null) {
        return avatar.asByteArray(channel.getApi()).thenAccept(bytes -> {
            String base64Avatar = "data:image/" + avatar.getFileType() + ";base64," + Base64.getEncoder().encodeToString(bytes);
            body.put("avatar", base64Avatar);
        }).thenCompose(aVoid -> new RestRequest<IncomingWebhook>(channel.getApi(), RestMethod.POST, RestEndpoint.CHANNEL_WEBHOOK).setUrlParameters(channel.getIdAsString()).setBody(body).setAuditLogReason(reason).execute(result -> new IncomingWebhookImpl(channel.getApi(), result.getJsonBody())));
    }
    return new RestRequest<IncomingWebhook>(channel.getApi(), RestMethod.POST, RestEndpoint.CHANNEL_WEBHOOK).setUrlParameters(channel.getIdAsString()).setBody(body).setAuditLogReason(reason).execute(result -> new IncomingWebhookImpl(channel.getApi(), result.getJsonBody()));
}
Also used : 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) WebhookBuilderDelegate(org.javacord.api.entity.webhook.internal.WebhookBuilderDelegate) 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) InputStream(java.io.InputStream) RestRequest(org.javacord.core.util.rest.RestRequest) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) IncomingWebhook(org.javacord.api.entity.webhook.IncomingWebhook)

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 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 WebhookBuilderDelegate (org.javacord.api.entity.webhook.internal.WebhookBuilderDelegate)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