Search in sources :

Example 1 with PrivateChannelImpl

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

use of org.javacord.core.entity.channel.PrivateChannelImpl in project Javacord by BtoBastian.

the class ReadyHandler method handle.

@Override
public void handle(JsonNode packet) {
    // Purge the cache first
    api.purgeCache();
    JsonNode guilds = packet.get("guilds");
    for (JsonNode guildJson : guilds) {
        if (guildJson.has("unavailable") && guildJson.get("unavailable").asBoolean()) {
            api.addUnavailableServerToCache(guildJson.get("id").asLong());
            continue;
        }
        new ServerImpl(api, guildJson);
    }
    // https://github.com/hammerandchisel/discord-api-docs/issues/184
    if (packet.has("private_channels")) {
        JsonNode privateChannels = packet.get("private_channels");
        for (JsonNode channelJson : privateChannels) {
            switch(ChannelType.fromId(channelJson.get("type").asInt())) {
                case PRIVATE_CHANNEL:
                    new PrivateChannelImpl(api, channelJson);
                    break;
                case GROUP_CHANNEL:
                    new GroupChannelImpl(api, channelJson);
                    break;
                default:
                    logger.warn("Unknown or unexpected channel type. Your Javacord version might be out of date!");
            }
        }
    }
    api.setYourself(new UserImpl(api, packet.get("user"), (MemberImpl) null, null));
}
Also used : ServerImpl(org.javacord.core.entity.server.ServerImpl) MemberImpl(org.javacord.core.entity.user.MemberImpl) UserImpl(org.javacord.core.entity.user.UserImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) PrivateChannelImpl(org.javacord.core.entity.channel.PrivateChannelImpl) GroupChannelImpl(org.javacord.core.entity.channel.GroupChannelImpl)

Example 3 with PrivateChannelImpl

use of org.javacord.core.entity.channel.PrivateChannelImpl in project Javacord by BtoBastian.

the class ChannelCreateHandler method handlePrivateChannel.

/**
 * Handles a private channel creation.
 *
 * @param channel The channel data.
 */
private void handlePrivateChannel(JsonNode channel) {
    // A CHANNEL_CREATE packet was sent every time a bot account receives a message, see
    // https://github.com/discord/discord-api-docs/issues/184 and
    // https://github.com/discord/discord-api-docs/issues/2248
    UserImpl recipient = new UserImpl(api, channel.get("recipients").get(0), (MemberImpl) null, null);
    if (!recipient.getPrivateChannel().isPresent()) {
        PrivateChannel privateChannel = new PrivateChannelImpl(api, channel.get("id").asText(), recipient, recipient.getId());
        PrivateChannelCreateEvent event = new PrivateChannelCreateEventImpl(privateChannel);
        api.getEventDispatcher().dispatchPrivateChannelCreateEvent(api, recipient, event);
    }
}
Also used : PrivateChannel(org.javacord.api.entity.channel.PrivateChannel) PrivateChannelCreateEventImpl(org.javacord.core.event.channel.user.PrivateChannelCreateEventImpl) UserImpl(org.javacord.core.entity.user.UserImpl) PrivateChannelCreateEvent(org.javacord.api.event.channel.user.PrivateChannelCreateEvent) PrivateChannelImpl(org.javacord.core.entity.channel.PrivateChannelImpl)

Example 4 with PrivateChannelImpl

use of org.javacord.core.entity.channel.PrivateChannelImpl in project Javacord by BtoBastian.

the class MessageCreateHandler method handle.

@Override
public void handle(JsonNode packet) {
    long channelId = packet.get("channel_id").asLong();
    // See https://github.com/discord/discord-api-docs/issues/2248
    if (!packet.hasNonNull("guild_id")) {
        // Check for EPHEMERAL messages as they do NOT include a guild_id when the EPHEMERAL flag is set.
        if (packet.hasNonNull("flags") && (packet.get("flags").asInt() & MessageFlag.EPHEMERAL.getId()) > 0) {
            Optional<ServerTextChannel> serverTextChannel = api.getServerTextChannelById(channelId);
            if (serverTextChannel.isPresent()) {
                handle(serverTextChannel.get(), packet);
                return;
            }
            Optional<ServerThreadChannel> serverThreadChannel = api.getServerThreadChannelById(channelId);
            if (serverThreadChannel.isPresent()) {
                handle(serverThreadChannel.get(), packet);
                return;
            }
        }
        UserImpl author = new UserImpl(api, packet.get("author"), (MemberImpl) null, null);
        PrivateChannelImpl privateChannel = PrivateChannelImpl.getOrCreatePrivateChannel(api, channelId, author.getId(), author);
        handle(privateChannel, packet);
        return;
    }
    Optional<TextChannel> optionalChannel = api.getTextChannelById(channelId);
    if (optionalChannel.isPresent()) {
        handle(optionalChannel.get(), packet);
    } else {
        LoggerUtil.logMissingChannel(logger, channelId);
    }
}
Also used : ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) TextChannel(org.javacord.api.entity.channel.TextChannel) ServerThreadChannel(org.javacord.api.entity.channel.ServerThreadChannel) UserImpl(org.javacord.core.entity.user.UserImpl) PrivateChannelImpl(org.javacord.core.entity.channel.PrivateChannelImpl)

Aggregations

PrivateChannelImpl (org.javacord.core.entity.channel.PrivateChannelImpl)4 UserImpl (org.javacord.core.entity.user.UserImpl)3 TextChannel (org.javacord.api.entity.channel.TextChannel)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 PrivateChannel (org.javacord.api.entity.channel.PrivateChannel)1 ServerChannel (org.javacord.api.entity.channel.ServerChannel)1 ServerTextChannel (org.javacord.api.entity.channel.ServerTextChannel)1 ServerThreadChannel (org.javacord.api.entity.channel.ServerThreadChannel)1 Message (org.javacord.api.entity.message.Message)1 Server (org.javacord.api.entity.server.Server)1 PrivateChannelCreateEvent (org.javacord.api.event.channel.user.PrivateChannelCreateEvent)1 ReactionRemoveAllEvent (org.javacord.api.event.message.reaction.ReactionRemoveAllEvent)1 GroupChannelImpl (org.javacord.core.entity.channel.GroupChannelImpl)1 ServerImpl (org.javacord.core.entity.server.ServerImpl)1 MemberImpl (org.javacord.core.entity.user.MemberImpl)1 PrivateChannelCreateEventImpl (org.javacord.core.event.channel.user.PrivateChannelCreateEventImpl)1 ReactionRemoveAllEventImpl (org.javacord.core.event.message.reaction.ReactionRemoveAllEventImpl)1 DispatchQueueSelector (org.javacord.core.util.event.DispatchQueueSelector)1