Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide old presence data on update #2852

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -33,7 +33,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 @@ -395,11 +395,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 @@ -426,6 +428,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 @@ -322,9 +322,10 @@ fn update_cache_with_event(
}
},
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 @@ -310,11 +310,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
Loading