Skip to content

Commit

Permalink
Provide old presence data from the cache on presence update (#2852)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbt365 authored and GnomedDev committed Aug 16, 2024
1 parent 1ca61fe commit 9a1bdbb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
7 changes: 6 additions & 1 deletion examples/e10_gateway_intents/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ impl EventHandler for Handler {

// As the intents set in this example, this event shall never be dispatched.
// Try it by changing your status.
async fn presence_update(&self, _ctx: Context, _new_data: Presence) {
async fn presence_update(
&self,
_ctx: Context,
_old_data: Option<Presence>,
_new_data: Presence,
) {
println!("Presence Update");
}

Expand Down
10 changes: 7 additions & 3 deletions src/cache/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::model::event::{
VoiceChannelStatusUpdateEvent,
VoiceStateUpdateEvent,
};
use crate::model::gateway::ShardInfo;
use crate::model::gateway::{Presence, ShardInfo};
use crate::model::guild::{Guild, GuildMemberFlags, Member, MemberGeneratedFlags, Role};
use crate::model::id::ShardId;
use crate::model::user::{CurrentUser, OnlineStatus};
Expand Down Expand Up @@ -394,11 +394,13 @@ impl CacheUpdate for MessageUpdateEvent {
}

impl CacheUpdate for PresenceUpdateEvent {
type Output = ();
type Output = Presence;

fn update(&mut self, cache: &Cache) -> Option<()> {
fn update(&mut self, cache: &Cache) -> Option<Presence> {
if let Some(guild_id) = self.presence.guild_id {
if let Some(mut guild) = cache.guilds.get_mut(&guild_id) {
let old = guild.presences.get(&self.presence.user.id).cloned();

// If the member went offline, remove them from the presence list.
if self.presence.status == OnlineStatus::Offline {
guild.presences.remove(&self.presence.user.id);
Expand All @@ -425,6 +427,8 @@ impl CacheUpdate for PresenceUpdateEvent {
});
}
}

return old;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/client/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,10 @@ fn update_cache_with_event(
presences: event.presences.into_vec(),
},
Event::PresenceUpdate(mut event) => {
update_cache!(cache, event);
let old_data = if_cache!(event.update(cache));

FullEvent::PresenceUpdate {
old_data,
new_data: event.presence,
}
},
Expand Down
5 changes: 3 additions & 2 deletions src/client/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,12 @@ event_handler! {

/// Dispatched when a user's presence is updated (e.g off -> on).
///
/// Provides the presence's new data.
/// Provides the presence's new data, as well as the old presence data if the
/// cache feature is enabled and the data is available.
///
/// Note: This event will not trigger unless the "guild presences" privileged intent is enabled
/// on the bot application page.
PresenceUpdate { new_data: Presence } => async fn presence_update(&self, ctx: Context);
PresenceUpdate { old_data: Option<Presence>, new_data: Presence } => async fn presence_update(&self, ctx: Context);

/// Dispatched upon startup.
///
Expand Down

0 comments on commit 9a1bdbb

Please sign in to comment.