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));
}
}
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)));
}
}
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;
}
Aggregations