use of org.javacord.core.DiscordApiImpl in project Javacord by BtoBastian.
the class MessageContextMenuUpdaterDelegateImpl method updateGlobal.
@Override
public CompletableFuture<MessageContextMenu> updateGlobal(DiscordApi api) {
ObjectNode body = JsonNodeFactory.instance.objectNode();
prepareBody(body);
return new RestRequest<MessageContextMenu>(api, RestMethod.PATCH, RestEndpoint.APPLICATION_COMMANDS).setUrlParameters(String.valueOf(api.getClientId()), String.valueOf(commandId)).setBody(body).execute(result -> new MessageContextMenuImpl((DiscordApiImpl) api, result.getJsonBody()));
}
use of org.javacord.core.DiscordApiImpl in project Javacord by BtoBastian.
the class MessageSetImpl method getMessagesAroundAsStream.
/**
* Gets a stream of messages in the given channel around a given message in any channel.
* The first message in the stream will be the given message if it was sent in the given channel.
* After that you will always get an older message and a newer message alternating as long as on both sides
* messages are available. If only on one side further messages are available, only those are delivered further on.
* It's not guaranteed to be perfectly balanced.
*
* <p>The messages are retrieved in batches synchronously from Discord,
* so consider not using this method from a listener directly.
*
* @param channel The channel of the messages.
* @param around Get messages around the message with this id.
* @return The stream.
* @see #getMessagesAround(TextChannel, int, long)
*/
public static Stream<Message> getMessagesAroundAsStream(TextChannel channel, long around) {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(new Iterator<Message>() {
private final DiscordApiImpl api = ((DiscordApiImpl) channel.getApi());
private final AtomicBoolean firstBatch = new AtomicBoolean(true);
private final AtomicBoolean nextIsOlder = new AtomicBoolean();
private long olderReferenceMessageId = around;
private long newerReferenceMessageId = around - 1;
private final List<JsonNode> olderMessageJsons = Collections.synchronizedList(new ArrayList<>());
private final List<JsonNode> newerMessageJsons = Collections.synchronizedList(new ArrayList<>());
private final AtomicBoolean hasMoreOlderMessages = new AtomicBoolean(true);
private final AtomicBoolean hasMoreNewerMessages = new AtomicBoolean(true);
private void ensureMessagesAvailable() {
if (olderMessageJsons.isEmpty() && hasMoreOlderMessages.get()) {
synchronized (olderMessageJsons) {
if (olderMessageJsons.isEmpty() && hasMoreOlderMessages.get()) {
olderMessageJsons.addAll(requestAsSortedJsonNodes(channel, 100, olderReferenceMessageId, -1, true));
if (olderMessageJsons.isEmpty()) {
hasMoreOlderMessages.set(false);
} else {
olderReferenceMessageId = olderMessageJsons.get(olderMessageJsons.size() - 1).get("id").asLong();
}
}
}
}
if (newerMessageJsons.isEmpty() && hasMoreNewerMessages.get()) {
synchronized (newerMessageJsons) {
if (newerMessageJsons.isEmpty() && hasMoreNewerMessages.get()) {
newerMessageJsons.addAll(requestAsSortedJsonNodes(channel, 100, -1, newerReferenceMessageId, false));
if (newerMessageJsons.isEmpty()) {
hasMoreNewerMessages.set(false);
} else {
newerReferenceMessageId = newerMessageJsons.get(newerMessageJsons.size() - 1).get("id").asLong();
if (firstBatch.getAndSet(false)) {
nextIsOlder.set(newerMessageJsons.get(0).get("id").asLong() != around);
}
}
}
}
}
}
@Override
public boolean hasNext() {
ensureMessagesAvailable();
return !(olderMessageJsons.isEmpty() && newerMessageJsons.isEmpty());
}
@Override
public Message next() {
ensureMessagesAvailable();
boolean nextIsOlder = this.nextIsOlder.get();
this.nextIsOlder.set(!nextIsOlder);
JsonNode messageJson = ((nextIsOlder && !olderMessageJsons.isEmpty()) || newerMessageJsons.isEmpty()) ? olderMessageJsons.remove(0) : newerMessageJsons.remove(0);
return api.getOrCreateMessage(channel, messageJson);
}
}, Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.NONNULL | Spliterator.CONCURRENT), false);
}
use of org.javacord.core.DiscordApiImpl in project Javacord by BtoBastian.
the class MessageImpl method getCustomEmojis.
@Override
public List<CustomEmoji> getCustomEmojis() {
String content = getContent();
List<CustomEmoji> emojis = new ArrayList<>();
Matcher customEmoji = DiscordRegexPattern.CUSTOM_EMOJI.matcher(content);
while (customEmoji.find()) {
long id = Long.parseLong(customEmoji.group("id"));
String name = customEmoji.group("name");
boolean animated = customEmoji.group(0).charAt(1) == 'a';
// TODO Maybe it would be better to cache the custom emoji objects inside the message object
CustomEmoji emoji = ((DiscordApiImpl) getApi()).getKnownCustomEmojiOrCreateCustomEmoji(id, name, animated);
emojis.add(emoji);
}
return Collections.unmodifiableList(emojis);
}
Aggregations