use of net.dv8tion.jda.api.events.user.update.UserUpdateOnlineStatusEvent in project JDA by DV8FromTheWorld.
the class PresenceUpdateHandler method handleInternally.
@Override
protected Long handleInternally(DataObject content) {
// Ignore events for relationships, presences are guild only to us
if (content.isNull("guild_id")) {
log.debug("Received PRESENCE_UPDATE without guild_id. Ignoring event.");
return null;
}
if (api.getCacheFlags().stream().noneMatch(CacheFlag::isPresence))
return null;
// Do a pre-check to see if this is for a Guild, and if it is, if the guild is currently locked or not cached.
final long guildId = content.getUnsignedLong("guild_id");
if (getJDA().getGuildSetupController().isLocked(guildId))
return guildId;
GuildImpl guild = (GuildImpl) getJDA().getGuildById(guildId);
if (guild == null) {
getJDA().getEventCache().cache(EventCache.Type.GUILD, guildId, responseNumber, allContent, this::handle);
EventCache.LOG.debug("Received a PRESENCE_UPDATE for a guild that is not yet cached! GuildId:{} UserId: {}", guildId, content.getObject("user").get("id"));
return null;
}
CacheView.SimpleCacheView<MemberPresenceImpl> presences = guild.getPresenceView();
if (presences == null)
// technically this should be impossible
return null;
DataObject jsonUser = content.getObject("user");
final long userId = jsonUser.getUnsignedLong("id");
MemberImpl member = (MemberImpl) guild.getMemberById(userId);
MemberPresenceImpl presence = presences.get(userId);
OnlineStatus status = OnlineStatus.fromKey(content.getString("status"));
if (status == OnlineStatus.OFFLINE)
presences.remove(userId);
if (presence == null) {
presence = new MemberPresenceImpl();
if (status != OnlineStatus.OFFLINE) {
try (UnlockHook lock = presences.writeLock()) {
presences.getMap().put(userId, presence);
}
}
}
// Now that we've update the User's info, lets see if we need to set the specific Presence information.
// This is stored in the Member objects.
// We set the activities to null to prevent parsing if the cache was disabled
final DataArray activityArray = !getJDA().isCacheFlagSet(CacheFlag.ACTIVITY) || content.isNull("activities") ? null : content.getArray("activities");
List<Activity> newActivities = new ArrayList<>();
boolean parsedActivity = parseActivities(userId, activityArray, newActivities);
if (getJDA().isCacheFlagSet(CacheFlag.CLIENT_STATUS) && !content.isNull("client_status"))
handleClientStatus(content, presence);
// Check if activities changed
if (parsedActivity)
handleActivities(newActivities, member, presence);
if (presence.getOnlineStatus() != status) {
OnlineStatus oldStatus = presence.getOnlineStatus();
presence.setOnlineStatus(status);
if (member != null) {
getJDA().getEntityBuilder().updateMemberCache(member);
getJDA().handleEvent(new UserUpdateOnlineStatusEvent(getJDA(), responseNumber, member, oldStatus));
}
}
return null;
}
Aggregations