Search in sources :

Example 1 with ServerChannel

use of org.javacord.api.entity.channel.ServerChannel 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 2 with ServerChannel

use of org.javacord.api.entity.channel.ServerChannel 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 3 with ServerChannel

use of org.javacord.api.entity.channel.ServerChannel in project Javacord by BtoBastian.

the class MessageDeleteBulkHandler method handle.

@Override
public void handle(JsonNode packet) {
    long channelId = Long.parseLong(packet.get("channel_id").asText());
    Optional<TextChannel> optionalChannel = api.getTextChannelById(channelId);
    if (optionalChannel.isPresent()) {
        TextChannel channel = optionalChannel.get();
        for (JsonNode messageIdJson : packet.get("ids")) {
            long messageId = messageIdJson.asLong();
            MessageDeleteEvent event = new MessageDeleteEventImpl(api, messageId, channel);
            api.getCachedMessageById(messageId).ifPresent(((MessageCacheImpl) channel.getMessageCache())::removeMessage);
            api.removeMessageFromCache(messageId);
            Optional<Server> optionalServer = channel.asServerChannel().map(ServerChannel::getServer);
            api.getEventDispatcher().dispatchMessageDeleteEvent(optionalServer.map(DispatchQueueSelector.class::cast).orElse(api), messageId, optionalServer.orElse(null), channel, event);
        }
    } else {
        LoggerUtil.logMissingChannel(logger, channelId);
    }
}
Also used : TextChannel(org.javacord.api.entity.channel.TextChannel) Server(org.javacord.api.entity.server.Server) JsonNode(com.fasterxml.jackson.databind.JsonNode) MessageDeleteEvent(org.javacord.api.event.message.MessageDeleteEvent) ServerChannel(org.javacord.api.entity.channel.ServerChannel) MessageDeleteEventImpl(org.javacord.core.event.message.MessageDeleteEventImpl) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector)

Example 4 with ServerChannel

use of org.javacord.api.entity.channel.ServerChannel 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 5 with ServerChannel

use of org.javacord.api.entity.channel.ServerChannel in project Javacord by BtoBastian.

the class InviteCreateHandler method handle.

@Override
protected void handle(JsonNode packet) {
    InviteImpl invite = new InviteImpl(api, packet);
    invite.getServer().ifPresent(server -> {
        // An error here means a missing channel for the invite, which should have already thrown an error
        ServerChannel channel = invite.getChannel().orElseThrow(AssertionError::new);
        ServerChannelInviteCreateEvent event = new ServerChannelInviteCreateEventImpl(invite, channel);
        api.getEventDispatcher().dispatchServerChannelInviteCreateEvent((DispatchQueueSelector) server, server, event);
    });
}
Also used : ServerChannelInviteCreateEvent(org.javacord.api.event.channel.server.invite.ServerChannelInviteCreateEvent) ServerChannelInviteCreateEventImpl(org.javacord.core.event.channel.server.invite.ServerChannelInviteCreateEventImpl) ServerChannel(org.javacord.api.entity.channel.ServerChannel) InviteImpl(org.javacord.core.entity.server.invite.InviteImpl)

Aggregations

ServerChannel (org.javacord.api.entity.channel.ServerChannel)9 Server (org.javacord.api.entity.server.Server)8 DispatchQueueSelector (org.javacord.core.util.event.DispatchQueueSelector)8 TextChannel (org.javacord.api.entity.channel.TextChannel)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 Message (org.javacord.api.entity.message.Message)3 Instant (java.time.Instant)2 InviteImpl (org.javacord.core.entity.server.invite.InviteImpl)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Locale (java.util.Locale)1