use of org.javacord.api.entity.message.MessageSet in project Javacord by BtoBastian.
the class MessageSetImpl method getMessagesAround.
/**
* Gets up to a given amount of messages in the given channel around a given message in any channel.
* The given message will be part of the result in addition to the messages around if it was sent in the given
* channel and does not count towards the limit.
* Half of the messages will be older than the given message and half of the messages will be newer.
* If there aren't enough older or newer messages, the actual amount of messages will be less than the given limit.
* It's also not guaranteed to be perfectly balanced.
*
* @param channel The channel of the messages.
* @param limit The limit of messages to get.
* @param around Get messages around the message with this id.
* @return The messages.
* @see #getMessagesAroundAsStream(TextChannel, long)
*/
public static CompletableFuture<MessageSet> getMessagesAround(TextChannel channel, int limit, long around) {
CompletableFuture<MessageSet> future = new CompletableFuture<>();
channel.getApi().getThreadPool().getExecutorService().submit(() -> {
try {
// calculate the half limit.
int halfLimit = limit / 2;
// get the newer half
MessageSet newerMessages = getMessagesAfter(channel, halfLimit, around).join();
// get the older half + around message
MessageSet olderMessages = getMessagesBefore(channel, halfLimit + 1, around + 1).join();
// for example because the around message was from a different channel
if (olderMessages.getNewestMessage().map(DiscordEntity::getId).map(id -> id != around).orElse(false)) {
olderMessages = olderMessages.tailSet(olderMessages.getOldestMessage().orElseThrow(AssertionError::new), false);
}
// combine the messages into one collection
Collection<Message> messages = Stream.of(olderMessages, newerMessages).flatMap(Collection::stream).collect(toList());
// we are done
future.complete(new MessageSetImpl(messages));
} catch (Throwable t) {
future.completeExceptionally(t);
}
});
return future;
}
use of org.javacord.api.entity.message.MessageSet in project Javacord by BtoBastian.
the class MessageSetImpl method getMessages.
/**
* Gets up to a given amount of messages in the given channel.
*
* @param channel The channel of the messages.
* @param limit The limit of messages to get.
* @param before Get messages before the message with this id.
* @param after Get messages after the message with this id.
*
* @return The messages.
* @see #getMessagesAsStream(TextChannel, long, long)
*/
private static CompletableFuture<MessageSet> getMessages(TextChannel channel, int limit, long before, long after) {
CompletableFuture<MessageSet> future = new CompletableFuture<>();
channel.getApi().getThreadPool().getExecutorService().submit(() -> {
try {
// get the initial batch with the first <= 100 messages
int initialBatchSize = ((limit % 100) == 0) ? 100 : limit % 100;
MessageSet initialMessages = requestAsMessages(channel, initialBatchSize, before, after);
// initialMessages is empty => READ_MESSAGE_HISTORY permission is denied or no more messages available
if ((limit <= 100) || initialMessages.isEmpty()) {
future.complete(initialMessages);
return;
}
// calculate the amount and direction of remaining message to get
// this will be a multiple of 100 and at least 100
int remainingMessages = limit - initialBatchSize;
int steps = remainingMessages / 100;
// "before" is set or both are not set
boolean older = (before != -1) || (after == -1);
boolean newer = after != -1;
// get remaining messages
List<MessageSet> messageSets = new ArrayList<>();
MessageSet lastMessages = initialMessages;
messageSets.add(lastMessages);
for (int step = 0; step < steps; ++step) {
lastMessages = requestAsMessages(channel, 100, lastMessages.getOldestMessage().filter(message -> older).map(DiscordEntity::getId).orElse(-1L), lastMessages.getNewestMessage().filter(message -> newer).map(DiscordEntity::getId).orElse(-1L));
// no more messages available
if (lastMessages.isEmpty()) {
break;
}
messageSets.add(lastMessages);
}
// combine the message sets
future.complete(new MessageSetImpl(messageSets.stream().flatMap(Collection::stream).collect(toList())));
} catch (Throwable t) {
future.completeExceptionally(t);
}
});
return future;
}
Aggregations