void HandlePresenceUpdateEvent(DiscordApiData data) { Snowflake guildId = data.GetSnowflake("guild_id").Value; // Update user DiscordApiData userData = data.Get("user"); Snowflake userId = userData.GetSnowflake("id").Value; if (cache.Users.TryGetValue(userId, out MutableUser mutableUser)) { mutableUser.PartialUpdate(userData); } else { // Don't throw exception since we can still update everything else... log.LogError($"[PRESENCE_UPDATE] Failed to update user {userId}, they were not in the cache!"); } // Update presence DiscordUserPresence presence = new DiscordUserPresence(userId, data); cache.GuildPresences[guildId, userId] = presence; // Update member if (cache.GuildMembers.TryGetValue(guildId, userId, out MutableGuildMember mutableMember)) { mutableMember.PartialUpdate(data); // Fire event OnPresenceUpdated?.Invoke(this, new PresenceEventArgs(shard, guildId, mutableMember.ImmutableEntity, presence)); } // It is technically valid for the member to not exist here, especially if the guild is considered large. }
internal PresenceEventArgs(Shard shard, Snowflake guildId, DiscordGuildMember member, DiscordUserPresence presence) : base(shard, guildId, member) { Presence = presence; }