use of org.javacord.api.entity.message.embed.EmbedBuilder in project Javacord by BtoBastian.
the class MessageBuilderBaseDelegateImpl method prepareEmbeds.
private void prepareEmbeds(ObjectNode body, boolean evenIfEmpty) {
if (!embeds.isEmpty() || evenIfEmpty) {
ArrayNode embedsNode = JsonNodeFactory.instance.objectNode().arrayNode();
for (EmbedBuilder embed : embeds) {
embedsNode.add(((EmbedBuilderDelegateImpl) embed.getDelegate()).toJsonNode());
}
body.set("embeds", embedsNode);
}
}
use of org.javacord.api.entity.message.embed.EmbedBuilder 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;
}
use of org.javacord.api.entity.message.embed.EmbedBuilder 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;
}
use of org.javacord.api.entity.message.embed.EmbedBuilder 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()));
}
use of org.javacord.api.entity.message.embed.EmbedBuilder 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()));
}
}
Aggregations