Search in sources :

Example 11 with TextChannel

use of org.javacord.api.entity.channel.TextChannel 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)

Example 12 with TextChannel

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

the class MessageDeleteHandler method handle.

@Override
public void handle(JsonNode packet) {
    long messageId = packet.get("id").asLong();
    long channelId = packet.get("channel_id").asLong();
    Optional<TextChannel> optionalChannel = api.getTextChannelById(channelId);
    if (optionalChannel.isPresent()) {
        TextChannel channel = optionalChannel.get();
        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);
        api.removeObjectListeners(Message.class, messageId);
    } else {
        LoggerUtil.logMissingChannel(logger, channelId);
    }
}
Also used : TextChannel(org.javacord.api.entity.channel.TextChannel) Server(org.javacord.api.entity.server.Server) 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)

Aggregations

TextChannel (org.javacord.api.entity.channel.TextChannel)12 Server (org.javacord.api.entity.server.Server)7 DispatchQueueSelector (org.javacord.core.util.event.DispatchQueueSelector)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 ServerChannel (org.javacord.api.entity.channel.ServerChannel)6 Message (org.javacord.api.entity.message.Message)5 ServerImpl (org.javacord.core.entity.server.ServerImpl)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Emoji (org.javacord.api.entity.emoji.Emoji)2 ComponentType (org.javacord.api.entity.message.component.ComponentType)2 DiscordApiImpl (org.javacord.core.DiscordApiImpl)2 Member (org.javacord.core.entity.user.Member)2 MemberImpl (org.javacord.core.entity.user.MemberImpl)2 RestEndpoint (org.javacord.core.util.rest.RestEndpoint)2 RestMethod (org.javacord.core.util.rest.RestMethod)2