Search in sources :

Example 1 with ServerThreadChannel

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

the class ThreadListSyncHandler method handle.

@Override
public void handle(final JsonNode packet) {
    final long serverId = packet.get("guild_id").asLong();
    final ServerImpl server = api.getServerById(serverId).map(ServerImpl.class::cast).orElse(null);
    if (server == null) {
        logger.warn("Unable to find server with id {}", serverId);
        return;
    }
    final List<Long> channelIds = new ArrayList<>();
    if (packet.has("channel_ids")) {
        for (final JsonNode channelId : packet.get("channel_ids")) {
            channelIds.add(channelId.asLong());
        }
    }
    final List<ServerThreadChannel> threads = new ArrayList<>();
    for (final JsonNode thread : packet.get("threads")) {
        threads.add(server.getOrCreateServerThreadChannel(thread));
    }
    final List<Long> threadIds = new ArrayList<>();
    for (final ServerThreadChannel thread : threads) {
        threadIds.add(thread.getId());
    }
    final List<ThreadMember> members = new ArrayList<>();
    for (final JsonNode member : packet.get("members")) {
        members.add(new ThreadMemberImpl(api, server, member));
    }
    // Removes lost threads from cache
    for (final Channel channel : api.getChannels()) {
        if (channel.getType() == ChannelType.SERVER_PRIVATE_THREAD || channel.getType() == ChannelType.SERVER_PUBLIC_THREAD && !threadIds.contains(channel.getId())) {
            api.removeChannelFromCache(channel.getId());
        }
    }
    final ThreadListSyncEvent event = new ThreadListSyncEventImpl(server, channelIds, threads, members);
    api.getEventDispatcher().dispatchThreadListSyncEvent(server, server, event);
}
Also used : Channel(org.javacord.api.entity.channel.Channel) ServerThreadChannel(org.javacord.api.entity.channel.ServerThreadChannel) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ThreadMember(org.javacord.api.entity.channel.ThreadMember) ThreadListSyncEventImpl(org.javacord.core.event.channel.thread.ThreadListSyncEventImpl) ThreadMemberImpl(org.javacord.core.entity.channel.ThreadMemberImpl) ServerImpl(org.javacord.core.entity.server.ServerImpl) ServerThreadChannel(org.javacord.api.entity.channel.ServerThreadChannel) ThreadListSyncEvent(org.javacord.api.event.channel.thread.ThreadListSyncEvent)

Example 2 with ServerThreadChannel

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

the class ThreadCreateHandler method handleThread.

/**
 * Handles the creation of a thread.
 *
 * @param channel The channel data from which to build the thread.
 */
private void handleThread(final JsonNode channel) {
    final long serverId = channel.get("guild_id").asLong();
    api.getPossiblyUnreadyServerById(serverId).ifPresent(server -> {
        final ServerThreadChannel serverThreadChannel = ((ServerImpl) server).getOrCreateServerThreadChannel(channel);
        final ThreadCreateEvent event = new ThreadCreateEventImpl(serverThreadChannel);
        api.getEventDispatcher().dispatchThreadCreateEvent((DispatchQueueSelector) server, serverThreadChannel, event);
    });
}
Also used : ServerImpl(org.javacord.core.entity.server.ServerImpl) ServerThreadChannel(org.javacord.api.entity.channel.ServerThreadChannel) ThreadCreateEventImpl(org.javacord.core.event.channel.thread.ThreadCreateEventImpl) ThreadCreateEvent(org.javacord.api.event.channel.thread.ThreadCreateEvent)

Example 3 with ServerThreadChannel

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

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

the class ServerImpl method getChannels.

@Override
public List<ServerChannel> getChannels() {
    final List<ServerChannel> channels = getUnorderedChannels().stream().filter(channel -> channel.asCategorizable().map(categorizable -> !categorizable.getCategory().isPresent()).orElse(false)).map(Channel::asRegularServerChannel).filter(Optional::isPresent).map(Optional::get).sorted(Comparator.<RegularServerChannel>comparingInt(channel -> channel.getType().getId()).thenComparing(RegularServerChannelImpl.COMPARE_BY_RAW_POSITION)).collect(Collectors.toList());
    getChannelCategories().forEach(category -> {
        channels.add(category);
        channels.addAll(category.getChannels());
    });
    final Map<ServerTextChannel, List<ServerThreadChannel>> serverTextChannelThreads = new HashMap<>();
    getThreadChannels().forEach(serverThreadChannel -> {
        final ServerTextChannel serverTextChannel = serverThreadChannel.getParent();
        serverTextChannelThreads.merge(serverTextChannel, new ArrayList<>(Collections.singletonList(serverThreadChannel)), (serverThreadChannels, serverThreadChannels2) -> {
            serverThreadChannels.addAll(serverThreadChannels2);
            return new ArrayList<>(serverThreadChannels);
        });
    });
    serverTextChannelThreads.forEach((serverTextChannel, serverThreadChannels) -> channels.addAll(channels.indexOf(serverTextChannel) + 1, serverThreadChannels));
    return Collections.unmodifiableList(channels);
}
Also used : AudioConnection(org.javacord.api.audio.AudioConnection) RegularServerChannelImpl(org.javacord.core.entity.channel.RegularServerChannelImpl) ServerChannel(org.javacord.api.entity.channel.ServerChannel) ServerVoiceChannelImpl(org.javacord.core.entity.channel.ServerVoiceChannelImpl) IconImpl(org.javacord.core.entity.IconImpl) Member(org.javacord.core.entity.user.Member) ServerThreadChannel(org.javacord.api.entity.channel.ServerThreadChannel) URL(java.net.URL) AuditLogImpl(org.javacord.core.entity.auditlog.AuditLogImpl) RestMethod(org.javacord.core.util.rest.RestMethod) ServerStageVoiceChannelImpl(org.javacord.core.entity.channel.ServerStageVoiceChannelImpl) StickerImpl(org.javacord.core.entity.sticker.StickerImpl) Ban(org.javacord.api.entity.server.Ban) BoostLevel(org.javacord.api.entity.server.BoostLevel) RoleImpl(org.javacord.core.entity.permission.RoleImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerTextChannelImpl(org.javacord.core.entity.channel.ServerTextChannelImpl) Locale(java.util.Locale) Map(java.util.Map) RestRequest(org.javacord.core.util.rest.RestRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) VanityUrlCodeImpl(org.javacord.core.entity.VanityUrlCodeImpl) AuditLog(org.javacord.api.entity.auditlog.AuditLog) Webhook(org.javacord.api.entity.webhook.Webhook) NsfwLevel(org.javacord.api.entity.server.NsfwLevel) Activity(org.javacord.api.entity.activity.Activity) InternalServerAttachableListenerManager(org.javacord.core.listener.server.InternalServerAttachableListenerManager) InviteImpl(org.javacord.core.entity.server.invite.InviteImpl) UserImpl(org.javacord.core.entity.user.UserImpl) Collection(java.util.Collection) ChannelCategoryImpl(org.javacord.core.entity.channel.ChannelCategoryImpl) WebhookImpl(org.javacord.core.entity.webhook.WebhookImpl) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) SlashCommand(org.javacord.api.interaction.SlashCommand) MultiFactorAuthenticationLevel(org.javacord.api.entity.server.MultiFactorAuthenticationLevel) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) VanityUrlCode(org.javacord.api.entity.VanityUrlCode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ServerVoiceChannel(org.javacord.api.entity.channel.ServerVoiceChannel) Objects(java.util.Objects) ExplicitContentFilterLevel(org.javacord.api.entity.server.ExplicitContentFilterLevel) RestEndpoint(org.javacord.core.util.rest.RestEndpoint) Sticker(org.javacord.api.entity.sticker.Sticker) List(java.util.List) Logger(org.apache.logging.log4j.Logger) AudioConnectionImpl(org.javacord.core.audio.AudioConnectionImpl) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Role(org.javacord.api.entity.permission.Role) Optional(java.util.Optional) IncomingWebhookImpl(org.javacord.core.entity.webhook.IncomingWebhookImpl) AuditLogActionType(org.javacord.api.entity.auditlog.AuditLogActionType) ActiveThreads(org.javacord.api.entity.server.ActiveThreads) RichInvite(org.javacord.api.entity.server.invite.RichInvite) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector) ChannelType(org.javacord.api.entity.channel.ChannelType) KnownCustomEmoji(org.javacord.api.entity.emoji.KnownCustomEmoji) ActivityImpl(org.javacord.core.entity.activity.ActivityImpl) ServerThreadChannelImpl(org.javacord.core.entity.channel.ServerThreadChannelImpl) Channel(org.javacord.api.entity.channel.Channel) DiscordClient(org.javacord.api.entity.DiscordClient) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) IncomingWebhook(org.javacord.api.entity.webhook.IncomingWebhook) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DiscordEntity(org.javacord.api.entity.DiscordEntity) Region(org.javacord.api.entity.Region) Icon(org.javacord.api.entity.Icon) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) Cleanupable(org.javacord.core.util.Cleanupable) DefaultMessageNotificationLevel(org.javacord.api.entity.server.DefaultMessageNotificationLevel) ReentrantLock(java.util.concurrent.locks.ReentrantLock) MalformedURLException(java.net.MalformedURLException) MemberImpl(org.javacord.core.entity.user.MemberImpl) DiscordApiImpl(org.javacord.core.DiscordApiImpl) AuditLogEntry(org.javacord.api.entity.auditlog.AuditLogEntry) Javacord(org.javacord.api.Javacord) Consumer(java.util.function.Consumer) LoggerUtil(org.javacord.core.util.logging.LoggerUtil) ServerFeature(org.javacord.api.entity.server.ServerFeature) ChannelCategory(org.javacord.api.entity.channel.ChannelCategory) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) ServerStageVoiceChannel(org.javacord.api.entity.channel.ServerStageVoiceChannel) Intent(org.javacord.api.entity.intent.Intent) User(org.javacord.api.entity.user.User) RestRequestResult(org.javacord.core.util.rest.RestRequestResult) VerificationLevel(org.javacord.api.entity.server.VerificationLevel) UserStatus(org.javacord.api.entity.user.UserStatus) DiscordApi(org.javacord.api.DiscordApi) Server(org.javacord.api.entity.server.Server) Comparator(java.util.Comparator) Collections(java.util.Collections) RegularServerChannel(org.javacord.api.entity.channel.RegularServerChannel) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) Optional(java.util.Optional) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ServerChannel(org.javacord.api.entity.channel.ServerChannel) ServerThreadChannel(org.javacord.api.entity.channel.ServerThreadChannel) ServerVoiceChannel(org.javacord.api.entity.channel.ServerVoiceChannel) Channel(org.javacord.api.entity.channel.Channel) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) ServerStageVoiceChannel(org.javacord.api.entity.channel.ServerStageVoiceChannel) RegularServerChannel(org.javacord.api.entity.channel.RegularServerChannel) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ServerChannel(org.javacord.api.entity.channel.ServerChannel) RegularServerChannel(org.javacord.api.entity.channel.RegularServerChannel)

Example 5 with ServerThreadChannel

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

the class ServerThreadChannelBuilderDelegateImpl method create.

@Override
public CompletableFuture<ServerThreadChannel> create() {
    final ObjectNode body = JsonNodeFactory.instance.objectNode();
    prepareBody(body);
    if (message != null) {
        return new RestRequest<ServerThreadChannel>(message.getApi(), RestMethod.POST, RestEndpoint.START_THREAD_WITH_MESSAGE).setUrlParameters(message.getChannel().getIdAsString(), message.getIdAsString()).setBody(body).execute(result -> ((ServerImpl) message.getServer().get()).getOrCreateServerThreadChannel(result.getJsonBody()));
    } else {
        return new RestRequest<ServerThreadChannel>(serverTextChannel.getApi(), RestMethod.POST, RestEndpoint.START_THREAD_WITHOUT_MESSAGE).setUrlParameters(serverTextChannel.getIdAsString()).setBody(body).execute(result -> ((ServerImpl) serverTextChannel.getServer()).getOrCreateServerThreadChannel(result.getJsonBody()));
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ServerThreadChannel(org.javacord.api.entity.channel.ServerThreadChannel)

Aggregations

ServerThreadChannel (org.javacord.api.entity.channel.ServerThreadChannel)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ArrayList (java.util.ArrayList)2 ServerTextChannel (org.javacord.api.entity.channel.ServerTextChannel)2 UserImpl (org.javacord.core.entity.user.UserImpl)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Instant (java.time.Instant)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 Map (java.util.Map)1 Objects (java.util.Objects)1