Search in sources :

Example 11 with DiscordApiImpl

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()));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DiscordApiImpl(org.javacord.core.DiscordApiImpl) MessageContextMenu(org.javacord.api.interaction.MessageContextMenu)

Example 12 with DiscordApiImpl

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);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DiscordApiImpl(org.javacord.core.DiscordApiImpl) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 13 with DiscordApiImpl

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);
}
Also used : CustomEmoji(org.javacord.api.entity.emoji.CustomEmoji) DiscordApiImpl(org.javacord.core.DiscordApiImpl) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList)

Aggregations

DiscordApiImpl (org.javacord.core.DiscordApiImpl)13 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)10 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)4 ArrayList (java.util.ArrayList)4 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)3 BufferedImage (java.awt.image.BufferedImage)3 File (java.io.File)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 Collection (java.util.Collection)3 HashSet (java.util.HashSet)3 List (java.util.List)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Icon (org.javacord.api.entity.Icon)3 FileContainer (org.javacord.core.util.FileContainer)3 RestEndpoint (org.javacord.core.util.rest.RestEndpoint)3 RestMethod (org.javacord.core.util.rest.RestMethod)3 RestRequest (org.javacord.core.util.rest.RestRequest)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Arrays (java.util.Arrays)2