Search in sources :

Example 1 with ThreadChannel

use of net.dv8tion.jda.api.entities.ThreadChannel in project JDA by DV8FromTheWorld.

the class ThreadListSyncHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    long guildId = content.getLong("guild_id");
    if (api.getGuildSetupController().isLocked(guildId))
        return guildId;
    EntityBuilder entityBuilder = api.getEntityBuilder();
    DataArray threadsArrayJson = content.getArray("threads");
    for (int i = 0; i < threadsArrayJson.length(); i++) {
        DataObject threadJson = threadsArrayJson.getObject(i);
        ThreadChannel thread = entityBuilder.createThreadChannel(threadJson, guildId);
        api.handleEvent(new ThreadRevealedEvent(api, responseNumber, thread));
    }
    return null;
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) ThreadRevealedEvent(net.dv8tion.jda.api.events.thread.ThreadRevealedEvent) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) ThreadChannel(net.dv8tion.jda.api.entities.ThreadChannel) DataArray(net.dv8tion.jda.api.utils.data.DataArray)

Example 2 with ThreadChannel

use of net.dv8tion.jda.api.entities.ThreadChannel in project JDA by DV8FromTheWorld.

the class ThreadDeleteHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    long guildId = content.getLong("guild_id");
    if (getJDA().getGuildSetupController().isLocked(guildId))
        return guildId;
    GuildImpl guild = (GuildImpl) getJDA().getGuildById(guildId);
    final long threadId = content.getLong("id");
    ThreadChannel thread = getJDA().getThreadChannelsView().remove(threadId);
    if (thread == null || guild == null) {
        WebSocketClient.LOG.debug("THREAD_DELETE attempted to delete a thread that is not yet cached. JSON: {}", content);
        return null;
    }
    guild.getThreadChannelsView().remove(threadId);
    getJDA().handleEvent(new ChannelDeleteEvent(getJDA(), responseNumber, thread));
    getJDA().getEventCache().clear(EventCache.Type.CHANNEL, threadId);
    return null;
}
Also used : GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) ChannelDeleteEvent(net.dv8tion.jda.api.events.channel.ChannelDeleteEvent) ThreadChannel(net.dv8tion.jda.api.entities.ThreadChannel)

Example 3 with ThreadChannel

use of net.dv8tion.jda.api.entities.ThreadChannel in project JDA by DV8FromTheWorld.

the class ThreadChannelPaginationActionImpl method handleSuccess.

@Override
protected void handleSuccess(Response response, Request<List<ThreadChannel>> request) {
    DataObject obj = response.getObject();
    DataArray selfThreadMembers = obj.getArray("members");
    DataArray threads = obj.getArray("threads");
    List<ThreadChannel> list = new ArrayList<>(threads.length());
    EntityBuilder builder = api.getEntityBuilder();
    TLongObjectMap<DataObject> selfThreadMemberMap = new TLongObjectHashMap<>();
    for (int i = 0; i < selfThreadMembers.length(); i++) {
        DataObject selfThreadMember = selfThreadMembers.getObject(i);
        // Store the thread member based on the "id" which is the _thread's_ id, not the member's id (which would be our id)
        selfThreadMemberMap.put(selfThreadMember.getLong("id"), selfThreadMember);
    }
    for (int i = 0; i < threads.length(); i++) {
        try {
            DataObject threadObj = threads.getObject(i);
            DataObject selfThreadMemberObj = selfThreadMemberMap.get(threadObj.getLong("id", 0));
            if (selfThreadMemberObj != null) {
                // Combine the thread and self thread-member into a single object to model what we get from
                // thread payloads (like from Gateway, etc)
                threadObj.put("member", selfThreadMemberObj);
            }
            ThreadChannel thread = builder.createThreadChannel(threadObj, getGuild().getIdLong());
            list.add(thread);
            if (this.useCache)
                this.cached.add(thread);
            this.last = thread;
            this.lastKey = last.getIdLong();
        } catch (ParsingException | NullPointerException e) {
            LOG.warn("Encountered exception in ThreadChannelPagination", e);
        }
    }
    request.onSuccess(list);
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) ParsingException(net.dv8tion.jda.api.exceptions.ParsingException) ArrayList(java.util.ArrayList) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) ThreadChannel(net.dv8tion.jda.api.entities.ThreadChannel) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) DataArray(net.dv8tion.jda.api.utils.data.DataArray)

Example 4 with ThreadChannel

use of net.dv8tion.jda.api.entities.ThreadChannel in project JDA by DV8FromTheWorld.

the class ThreadCreateHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    long guildId = content.getLong("guild_id");
    if (api.getGuildSetupController().isLocked(guildId))
        return guildId;
    ThreadChannel thread = api.getEntityBuilder().createThreadChannel(content, guildId);
    api.handleEvent(new ChannelCreateEvent(api, responseNumber, thread));
    return null;
}
Also used : ChannelCreateEvent(net.dv8tion.jda.api.events.channel.ChannelCreateEvent) ThreadChannel(net.dv8tion.jda.api.entities.ThreadChannel)

Example 5 with ThreadChannel

use of net.dv8tion.jda.api.entities.ThreadChannel in project JDA by DV8FromTheWorld.

the class ThreadUpdateHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    long guildId = content.getLong("guild_id");
    if (api.getGuildSetupController().isLocked(guildId))
        return guildId;
    final long threadId = content.getLong("id");
    ThreadChannelImpl thread = (ThreadChannelImpl) getJDA().getThreadChannelById(threadId);
    // Refer to the documentation for more info: https://discord.com/developers/docs/topics/threads#unarchiving-a-thread
    if (thread == null) {
        // Technically, when the ThreadChannel is unarchived the archive_timestamp (getTimeArchiveInfoLastModified) changes
        // as well, but we don't have the original value because we didn't have the thread in memory, so we can't
        // provide an entirely accurate ChannelUpdateArchiveTimestampEvent. Not sure how much that'll matter.
        thread = (ThreadChannelImpl) api.getEntityBuilder().createThreadChannel(content, guildId);
        api.handleEvent(new ChannelUpdateArchivedEvent(api, responseNumber, thread, true, false));
        return null;
    }
    final DataObject threadMetadata = content.getObject("thread_metadata");
    final String name = content.getString("name");
    final ThreadChannel.AutoArchiveDuration autoArchiveDuration = ThreadChannel.AutoArchiveDuration.fromKey(threadMetadata.getInt("auto_archive_duration"));
    final boolean locked = threadMetadata.getBoolean("locked");
    final boolean archived = threadMetadata.getBoolean("archived");
    final boolean invitable = threadMetadata.getBoolean("invitable");
    final long archiveTimestamp = Helpers.toTimestamp(threadMetadata.getString("archive_timestamp"));
    final int slowmode = content.getInt("rate_limit_per_user", 0);
    final String oldName = thread.getName();
    final ThreadChannel.AutoArchiveDuration oldAutoArchiveDuration = thread.getAutoArchiveDuration();
    final boolean oldLocked = thread.isLocked();
    final boolean oldArchived = thread.isArchived();
    final boolean oldInvitable = !thread.isPublic() && thread.isInvitable();
    final long oldArchiveTimestamp = thread.getArchiveTimestamp();
    final int oldSlowmode = thread.getSlowmode();
    // TODO should these be Thread specific events?
    if (!Objects.equals(oldName, name)) {
        thread.setName(name);
        api.handleEvent(new ChannelUpdateNameEvent(getJDA(), responseNumber, thread, oldName, name));
    }
    if (oldSlowmode != slowmode) {
        thread.setSlowmode(slowmode);
        api.handleEvent(new ChannelUpdateSlowmodeEvent(api, responseNumber, thread, oldSlowmode, slowmode));
    }
    if (oldAutoArchiveDuration != autoArchiveDuration) {
        thread.setAutoArchiveDuration(autoArchiveDuration);
        api.handleEvent(new ChannelUpdateAutoArchiveDurationEvent(api, responseNumber, thread, oldAutoArchiveDuration, autoArchiveDuration));
    }
    if (oldLocked != locked) {
        thread.setLocked(locked);
        api.handleEvent(new ChannelUpdateLockedEvent(api, responseNumber, thread, oldLocked, locked));
    }
    if (oldArchived != archived) {
        thread.setArchived(archived);
        api.handleEvent(new ChannelUpdateArchivedEvent(api, responseNumber, thread, oldArchived, archived));
    }
    if (oldArchiveTimestamp != archiveTimestamp) {
        thread.setArchiveTimestamp(archiveTimestamp);
        api.handleEvent(new ChannelUpdateArchiveTimestampEvent(api, responseNumber, thread, oldArchiveTimestamp, archiveTimestamp));
    }
    if (oldInvitable != invitable) {
        thread.setInvitable(invitable);
        api.handleEvent(new ChannelUpdateInvitableEvent(api, responseNumber, thread, oldInvitable, invitable));
    }
    return null;
}
Also used : ThreadChannelImpl(net.dv8tion.jda.internal.entities.ThreadChannelImpl) DataObject(net.dv8tion.jda.api.utils.data.DataObject) ThreadChannel(net.dv8tion.jda.api.entities.ThreadChannel)

Aggregations

ThreadChannel (net.dv8tion.jda.api.entities.ThreadChannel)6 DataObject (net.dv8tion.jda.api.utils.data.DataObject)3 DataArray (net.dv8tion.jda.api.utils.data.DataArray)2 EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)2 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)1 ArrayList (java.util.ArrayList)1 ChannelCreateEvent (net.dv8tion.jda.api.events.channel.ChannelCreateEvent)1 ChannelDeleteEvent (net.dv8tion.jda.api.events.channel.ChannelDeleteEvent)1 ThreadRevealedEvent (net.dv8tion.jda.api.events.thread.ThreadRevealedEvent)1 ParsingException (net.dv8tion.jda.api.exceptions.ParsingException)1 GuildImpl (net.dv8tion.jda.internal.entities.GuildImpl)1 ThreadChannelImpl (net.dv8tion.jda.internal.entities.ThreadChannelImpl)1