Search in sources :

Example 1 with ThreadMember

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

the class ThreadMembersUpdateHandler method handleAddedThreadMembers.

private void handleAddedThreadMembers(ThreadChannelImpl thread, DataArray addedMembersJson) {
    EntityBuilder entityBuilder = api.getEntityBuilder();
    CacheView.SimpleCacheView<ThreadMember> view = thread.getThreadMemberView();
    List<ThreadMember> addedThreadMembers = new ArrayList<>();
    for (int i = 0; i < addedMembersJson.length(); i++) {
        DataObject threadMemberJson = addedMembersJson.getObject(i);
        ThreadMember threadMember = entityBuilder.createThreadMember((GuildImpl) thread.getGuild(), thread, threadMemberJson);
        addedThreadMembers.add(threadMember);
    }
    // want to cache ThreadMembers if the Members they're based on aren't being cached.
    try (UnlockHook lock = view.writeLock()) {
        for (ThreadMember threadMember : addedThreadMembers) {
            view.getMap().put(threadMember.getIdLong(), threadMember);
        }
    }
    // Emit the events from outside the writeLock
    for (ThreadMember threadMember : addedThreadMembers) {
        api.handleEvent(new ThreadMemberJoinEvent(api, responseNumber, thread, threadMember));
    }
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) CacheView(net.dv8tion.jda.api.utils.cache.CacheView) UnlockHook(net.dv8tion.jda.internal.utils.UnlockHook) ArrayList(java.util.ArrayList) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) ThreadMember(net.dv8tion.jda.api.entities.ThreadMember) ThreadMemberJoinEvent(net.dv8tion.jda.api.events.thread.member.ThreadMemberJoinEvent)

Example 2 with ThreadMember

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

the class ThreadMembersUpdateHandler method handleRemovedThreadMembers.

private void handleRemovedThreadMembers(ThreadChannelImpl thread, List<Long> removedMemberIds) {
    CacheView.SimpleCacheView<ThreadMember> view = thread.getThreadMemberView();
    // Store the removed threads into a map so that we can provide them in the events later.
    // We don't want to dispatch the events from inside the writeLock
    TLongObjectMap<ThreadMember> removedThreadMembers = new TLongObjectHashMap<>();
    try (UnlockHook lock = view.writeLock()) {
        for (long threadMemberId : removedMemberIds) {
            ThreadMember threadMember = view.getMap().remove(threadMemberId);
            removedThreadMembers.put(threadMemberId, threadMember);
        }
    }
    for (long threadMemberId : removedMemberIds) {
        api.handleEvent(new ThreadMemberLeaveEvent(api, responseNumber, thread, threadMemberId, removedThreadMembers.remove(threadMemberId)));
    }
}
Also used : CacheView(net.dv8tion.jda.api.utils.cache.CacheView) UnlockHook(net.dv8tion.jda.internal.utils.UnlockHook) ThreadMemberLeaveEvent(net.dv8tion.jda.api.events.thread.member.ThreadMemberLeaveEvent) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) ThreadMember(net.dv8tion.jda.api.entities.ThreadMember)

Example 3 with ThreadMember

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

the class ThreadMemberUpdateHandler 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);
    if (thread == null) {
        getJDA().getEventCache().cache(EventCache.Type.CHANNEL, threadId, responseNumber, allContent, this::handle);
        EventCache.LOG.debug("THREAD_MEMBER_UPDATE attempted to update a thread that does not exist. JSON: {}", content);
        return null;
    }
    // Based on the docs it is expected that we will only ever receive THREAD_MEMBER_UPDATE when Discord needs to inform
    // us that we are a member of a ThreadChannels that we might not have in memory. Currently this only happens
    // for ThreadChannels that get unarchived.
    // Details available at: https://discord.com/developers/docs/topics/threads#unarchiving-a-thread
    long userId = content.getLong("user_id");
    if (userId != getJDA().getSelfUser().getIdLong()) {
        JDAImpl.LOG.warn("Received a THREAD_MEMBER_UPDATE for a user that isn't the current bot user. " + "This validates assumptions that THREAD_MEMBER_UPDATE would ONLY be for the current bot user. " + "Skipping this dispatch for now. This should be reported as a bug." + "\nDetails: {}", content);
        return null;
    }
    CacheView.SimpleCacheView<ThreadMember> view = thread.getThreadMemberView();
    try (UnlockHook lock = view.writeLock()) {
        // We might have still had the ThreadChannel in memory, so our ThreadMember might still exist. Do an existence check.
        ThreadMember threadMember = view.getMap().get(userId);
        if (threadMember == null) {
            threadMember = api.getEntityBuilder().createThreadMember(thread, thread.getGuild().getSelfMember(), content);
            view.getMap().put(threadMember.getIdLong(), threadMember);
        }
    }
    return null;
}
Also used : ThreadChannelImpl(net.dv8tion.jda.internal.entities.ThreadChannelImpl) CacheView(net.dv8tion.jda.api.utils.cache.CacheView) UnlockHook(net.dv8tion.jda.internal.utils.UnlockHook) ThreadMember(net.dv8tion.jda.api.entities.ThreadMember)

Aggregations

ThreadMember (net.dv8tion.jda.api.entities.ThreadMember)3 CacheView (net.dv8tion.jda.api.utils.cache.CacheView)3 UnlockHook (net.dv8tion.jda.internal.utils.UnlockHook)3 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)1 ArrayList (java.util.ArrayList)1 ThreadMemberJoinEvent (net.dv8tion.jda.api.events.thread.member.ThreadMemberJoinEvent)1 ThreadMemberLeaveEvent (net.dv8tion.jda.api.events.thread.member.ThreadMemberLeaveEvent)1 DataObject (net.dv8tion.jda.api.utils.data.DataObject)1 EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)1 ThreadChannelImpl (net.dv8tion.jda.internal.entities.ThreadChannelImpl)1