use of org.javacord.api.entity.message.Message in project Javacord by BtoBastian.
the class MessageCacheImpl method clean.
/**
* Cleans the cache.
*/
public void clean() {
Instant minAge = Instant.now().minus(storageTimeInSeconds, ChronoUnit.SECONDS);
api.getMessageCacheLock().lock();
try {
messages.removeIf(messageRef -> Optional.ofNullable(messageRef.get()).map(message -> !message.isCachedForever() && message.getCreationTimestamp().isBefore(minAge)).orElse(true));
long foreverCachedAmount = messages.stream().map(Reference::get).filter(Objects::nonNull).filter(Message::isCachedForever).count();
messages.removeAll(messages.stream().filter(messageRef -> Optional.ofNullable(messageRef.get()).map(message -> !message.isCachedForever()).orElse(true)).limit(Math.max(0, messages.size() - capacity - foreverCachedAmount)).collect(Collectors.toList()));
} finally {
api.getMessageCacheLock().unlock();
}
}
use of org.javacord.api.entity.message.Message 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()));
}
}
use of org.javacord.api.entity.message.Message in project Javacord by BtoBastian.
the class MessageBuilderBaseDelegateImpl method edit.
@Override
public CompletableFuture<Message> edit(Message message, boolean updateAll) {
ObjectNode body = JsonNodeFactory.instance.objectNode();
if (updateAll || contentChanged) {
body.put("content", strBuilder.toString());
}
prepareAllowedMentions(body);
prepareEmbeds(body, updateAll || embedsChanged);
prepareComponents(body, updateAll || componentsChanged);
RestRequest<Message> request = new RestRequest<Message>(message.getApi(), RestMethod.PATCH, RestEndpoint.MESSAGE).setUrlParameters(Long.toUnsignedString(message.getChannel().getId()), Long.toUnsignedString(message.getId()));
if (updateAll || attachmentsChanged) {
return checkForAttachmentsAndExecuteRequest(message.getChannel(), body, request, true);
} else {
return executeRequestWithoutAttachments(message.getChannel(), body, request);
}
}
use of org.javacord.api.entity.message.Message in project Javacord by BtoBastian.
the class MessageBuilderBaseDelegateImpl method send.
@Override
public CompletableFuture<Message> send(TextChannel channel) {
ObjectNode body = JsonNodeFactory.instance.objectNode().put("content", toString() == null ? "" : toString()).put("tts", tts);
body.putArray("mentions");
prepareAllowedMentions(body);
prepareEmbeds(body, false);
prepareComponents(body, false);
if (nonce != null) {
body.put("nonce", nonce);
}
if (!stickerIds.isEmpty()) {
ArrayNode stickersNode = JsonNodeFactory.instance.objectNode().arrayNode();
for (long stickerId : stickerIds) {
stickersNode.add(stickerId);
}
body.set("sticker_ids", stickersNode);
}
if (replyingTo != null) {
body.putObject("message_reference").put("message_id", replyingTo);
}
RestRequest<Message> request = new RestRequest<Message>(channel.getApi(), RestMethod.POST, RestEndpoint.MESSAGE).setUrlParameters(channel.getIdAsString());
return checkForAttachmentsAndExecuteRequest(channel, body, request, false);
}
Aggregations