Search in sources :

Example 6 with Message

use of org.javacord.api.entity.message.Message in project Javacord by BtoBastian.

the class MessageCreateHandler method handle.

private void handle(TextChannel channel, JsonNode packet) {
    Message message = api.getOrCreateMessage(channel, packet);
    MessageCreateEvent event = new MessageCreateEventImpl(message);
    Optional<Server> optionalServer = channel.asServerChannel().map(ServerChannel::getServer);
    MessageAuthor author = message.getAuthor();
    api.getEventDispatcher().dispatchMessageCreateEvent(optionalServer.map(DispatchQueueSelector.class::cast).orElse(api), optionalServer.orElse(null), channel, author.asUser().orElse(null), author.isWebhook() ? author.getId() : null, event);
}
Also used : MessageCreateEvent(org.javacord.api.event.message.MessageCreateEvent) Message(org.javacord.api.entity.message.Message) MessageCreateEventImpl(org.javacord.core.event.message.MessageCreateEventImpl) Server(org.javacord.api.entity.server.Server) MessageAuthor(org.javacord.api.entity.message.MessageAuthor) ServerChannel(org.javacord.api.entity.channel.ServerChannel) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector)

Example 7 with Message

use of org.javacord.api.entity.message.Message in project Javacord by BtoBastian.

the class MessageReactionAddHandler method handle.

@Override
public void handle(JsonNode packet) {
    long channelId = packet.get("channel_id").asLong();
    long messageId = packet.get("message_id").asLong();
    long userId = packet.get("user_id").asLong();
    String serverId = packet.hasNonNull("guild_id") ? packet.get("guild_id").asText() : null;
    TextChannel channel;
    if (serverId == null) {
        // if private channel:
        channel = PrivateChannelImpl.getOrCreatePrivateChannel(api, channelId, userId, null);
    } else {
        channel = api.getTextChannelById(channelId).orElse(null);
    }
    if (channel == null) {
        LoggerUtil.logMissingChannel(logger, channelId);
        return;
    }
    Optional<Server> server = api.getServerById(serverId);
    Member member = null;
    if (packet.hasNonNull("member") && server.isPresent()) {
        member = new MemberImpl(api, (ServerImpl) server.get(), packet.get("member"), null);
    }
    Optional<Message> message = api.getCachedMessageById(messageId);
    Emoji emoji;
    JsonNode emojiJson = packet.get("emoji");
    if (!emojiJson.has("id") || emojiJson.get("id").isNull()) {
        emoji = UnicodeEmojiImpl.fromString(emojiJson.get("name").asText());
    } else {
        emoji = api.getKnownCustomEmojiOrCreateCustomEmoji(emojiJson);
    }
    message.ifPresent(msg -> ((MessageImpl) msg).addReaction(emoji, userId == api.getYourself().getId()));
    ReactionAddEvent event = new ReactionAddEventImpl(api, messageId, channel, emoji, userId, member);
    api.getEventDispatcher().dispatchReactionAddEvent(server.map(DispatchQueueSelector.class::cast).orElse(api), messageId, server.orElse(null), channel, userId, event);
}
Also used : ReactionAddEventImpl(org.javacord.core.event.message.reaction.ReactionAddEventImpl) Server(org.javacord.api.entity.server.Server) Message(org.javacord.api.entity.message.Message) MemberImpl(org.javacord.core.entity.user.MemberImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) ReactionAddEvent(org.javacord.api.event.message.reaction.ReactionAddEvent) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector) TextChannel(org.javacord.api.entity.channel.TextChannel) ServerImpl(org.javacord.core.entity.server.ServerImpl) Emoji(org.javacord.api.entity.emoji.Emoji) Member(org.javacord.core.entity.user.Member)

Example 8 with Message

use of org.javacord.api.entity.message.Message in project Javacord by BtoBastian.

the class MessageReactionRemoveHandler method handle.

@Override
public void handle(JsonNode packet) {
    long messageId = packet.get("message_id").asLong();
    long userId = packet.get("user_id").asLong();
    Optional<Message> message = api.getCachedMessageById(messageId);
    long channelId = packet.get("channel_id").asLong();
    TextChannel channel;
    if (packet.hasNonNull("guild_id")) {
        channel = api.getTextChannelById(channelId).orElse(null);
    } else {
        // if private channel:
        channel = PrivateChannelImpl.getOrCreatePrivateChannel(api, channelId, userId, null);
    }
    if (channel == null) {
        LoggerUtil.logMissingChannel(logger, channelId);
        return;
    }
    Emoji emoji;
    JsonNode emojiJson = packet.get("emoji");
    if (!emojiJson.has("id") || emojiJson.get("id").isNull()) {
        emoji = UnicodeEmojiImpl.fromString(emojiJson.get("name").asText());
    } else {
        emoji = api.getKnownCustomEmojiOrCreateCustomEmoji(emojiJson);
    }
    message.ifPresent(msg -> ((MessageImpl) msg).removeReaction(emoji, userId == api.getYourself().getId()));
    ReactionRemoveEvent event = new ReactionRemoveEventImpl(api, messageId, channel, emoji, userId);
    Optional<Server> optionalServer = channel.asServerChannel().map(ServerChannel::getServer);
    api.getEventDispatcher().dispatchReactionRemoveEvent(optionalServer.map(DispatchQueueSelector.class::cast).orElse(api), messageId, optionalServer.orElse(null), channel, userId, event);
}
Also used : ReactionRemoveEvent(org.javacord.api.event.message.reaction.ReactionRemoveEvent) TextChannel(org.javacord.api.entity.channel.TextChannel) Message(org.javacord.api.entity.message.Message) Server(org.javacord.api.entity.server.Server) ReactionRemoveEventImpl(org.javacord.core.event.message.reaction.ReactionRemoveEventImpl) Emoji(org.javacord.api.entity.emoji.Emoji) JsonNode(com.fasterxml.jackson.databind.JsonNode) ServerChannel(org.javacord.api.entity.channel.ServerChannel) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector)

Example 9 with Message

use of org.javacord.api.entity.message.Message in project Javacord by BtoBastian.

the class MessageReactionRemoveAllHandler method handle.

@Override
public void handle(JsonNode packet) {
    long messageId = packet.get("message_id").asLong();
    Optional<Message> message = api.getCachedMessageById(messageId);
    message.ifPresent(msg -> ((MessageImpl) msg).removeAllReactionsFromCache());
    long channelId = packet.get("channel_id").asLong();
    TextChannel channel = api.getTextChannelById(channelId).orElse(null);
    if (channel == null) {
        if (packet.hasNonNull("guild_id")) {
            // we don't know anything about the channel as it is part of a server and not cached
            LoggerUtil.logMissingChannel(logger, channelId);
            return;
        }
        // channel is a private channel:
        channel = PrivateChannelImpl.dispatchPrivateChannelCreateEvent(api, new PrivateChannelImpl(api, channelId, null, null));
    }
    ReactionRemoveAllEvent event = new ReactionRemoveAllEventImpl(api, messageId, channel);
    Optional<Server> optionalServer = channel.asServerChannel().map(ServerChannel::getServer);
    api.getEventDispatcher().dispatchReactionRemoveAllEvent(optionalServer.map(DispatchQueueSelector.class::cast).orElse(api), messageId, optionalServer.orElse(null), channel, event);
}
Also used : TextChannel(org.javacord.api.entity.channel.TextChannel) Message(org.javacord.api.entity.message.Message) Server(org.javacord.api.entity.server.Server) ReactionRemoveAllEventImpl(org.javacord.core.event.message.reaction.ReactionRemoveAllEventImpl) ReactionRemoveAllEvent(org.javacord.api.event.message.reaction.ReactionRemoveAllEvent) PrivateChannelImpl(org.javacord.core.entity.channel.PrivateChannelImpl) ServerChannel(org.javacord.api.entity.channel.ServerChannel) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector)

Example 10 with Message

use of org.javacord.api.entity.message.Message 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;
}
Also used : MessageSet(org.javacord.api.entity.message.MessageSet) MessageSet(org.javacord.api.entity.message.MessageSet) Arrays(java.util.Arrays) Spliterators(java.util.Spliterators) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) RestMethod(org.javacord.core.util.rest.RestMethod) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) DiscordEntity(org.javacord.api.entity.DiscordEntity) RestRequest(org.javacord.core.util.rest.RestRequest) TextChannel(org.javacord.api.entity.channel.TextChannel) JsonNode(com.fasterxml.jackson.databind.JsonNode) StreamSupport(java.util.stream.StreamSupport) Iterator(java.util.Iterator) Predicate(java.util.function.Predicate) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) NavigableSet(java.util.NavigableSet) DiscordApiImpl(org.javacord.core.DiscordApiImpl) Message(org.javacord.api.entity.message.Message) RestEndpoint(org.javacord.core.util.rest.RestEndpoint) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) Comparator(java.util.Comparator) Collections(java.util.Collections) Spliterator(java.util.Spliterator) CompletableFuture(java.util.concurrent.CompletableFuture) Message(org.javacord.api.entity.message.Message) RestEndpoint(org.javacord.core.util.rest.RestEndpoint) DiscordEntity(org.javacord.api.entity.DiscordEntity)

Aggregations

Message (org.javacord.api.entity.message.Message)14 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 RestRequest (org.javacord.core.util.rest.RestRequest)6 Arrays (java.util.Arrays)5 Collections (java.util.Collections)5 TextChannel (org.javacord.api.entity.channel.TextChannel)5 EmbedBuilder (org.javacord.api.entity.message.embed.EmbedBuilder)5 DiscordApiImpl (org.javacord.core.DiscordApiImpl)5 RestEndpoint (org.javacord.core.util.rest.RestEndpoint)5 RestMethod (org.javacord.core.util.rest.RestMethod)5 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)4 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)4 Stream (java.util.stream.Stream)4 StreamSupport (java.util.stream.StreamSupport)4 DiscordApi (org.javacord.api.DiscordApi)4 Emoji (org.javacord.api.entity.emoji.Emoji)4