Skip to content

Commit

Permalink
Add entitlement gateway events
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrasnitski committed Oct 10, 2023
1 parent b3f087c commit 4256861
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/client/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,18 @@ fn update_cache_with_event(ctx: Context, event: Event) -> Option<(FullEvent, Opt
ctx,
unsubscribed: event,
},
Event::EntitlementCreate(event) => FullEvent::EntitlementCreate {
ctx,
entitlement: event.entitlement,
},
Event::EntitlementUpdate(event) => FullEvent::EntitlementUpdate {
ctx,
entitlement: event.entitlement,
},
Event::EntitlementDelete(event) => FullEvent::EntitlementDelete {
ctx,
entitlement: event.entitlement,
},
};

Some((event, extra_event))
Expand Down
21 changes: 21 additions & 0 deletions src/client/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,27 @@ event_handler! {
/// Provides data about the cancelled subscription.
async fn guild_scheduled_event_user_remove(&self, GuildScheduledEventUserRemove { ctx: Context, unsubscribed: GuildScheduledEventUserRemoveEvent });

/// Dispatched when a user subscribes to a SKU.
///
/// Provides data about the subscription.
async fn entitlement_create(&self, EntitlementCreate { ctx: Context, entitlement: Entitlement });

/// Dispatched when a user's subscription to a SKU renews for the next billing period.
///
/// Provides data abut the updated subscription. Specifically, the [`Entitlement::ends_at`]
/// field will have changed.
async fn entitlement_update(&self, EntitlementUpdate { ctx: Context, entitlement: Entitlement });

/// Dispatched when a user's entitlement has been deleted. This happens rarely, but can occur
/// if a subscription is refunded or otherwise deleted by Discord.
///
/// Provides data about the subscription. Specifically, the [`Entitlement::deleted`] field will
/// be set.
///
/// Note that expired subscriptions do not result in deletion of their corresponding
/// [`Entitlement`] and will not trigger this event upon expiring.
async fn entitlement_delete(&self, EntitlementDelete { ctx: Context, entitlement: Entitlement });

/// Dispatched when an HTTP rate limit is hit
async fn ratelimit(&self, Ratelimit { data: RatelimitInfo });
}
Expand Down
36 changes: 36 additions & 0 deletions src/model/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,36 @@ pub struct GuildScheduledEventUserRemoveEvent {
pub guild_id: GuildId,
}

/// Requires no gateway intents.
///
/// [Discord docs](https://discord.com/developers/docs/monetization/entitlements#new-entitlement)
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(transparent)]
#[non_exhaustive]
pub struct EntitlementCreateEvent {
pub entitlement: Entitlement,
}

/// Requires no gateway intents.
///
/// [Discord docs](https://discord.com/developers/docs/monetization/entitlements#new-entitlement)
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(transparent)]
#[non_exhaustive]
pub struct EntitlementUpdateEvent {
pub entitlement: Entitlement,
}

/// Requires no gateway intents.
///
/// [Discord docs](https://discord.com/developers/docs/monetization/entitlements#new-entitlement)
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(transparent)]
#[non_exhaustive]
pub struct EntitlementDeleteEvent {
pub entitlement: Entitlement,
}

/// [Discord docs](https://discord.com/developers/docs/topics/gateway-events#payload-structure).
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, Serialize)]
Expand Down Expand Up @@ -1188,6 +1218,12 @@ pub enum Event {
GuildScheduledEventUserAdd(GuildScheduledEventUserAddEvent),
/// A guild member has unsubscribed from a scheduled event.
GuildScheduledEventUserRemove(GuildScheduledEventUserRemoveEvent),
/// A user subscribed to a SKU.
EntitlementCreate(EntitlementCreateEvent),
/// A user's subscription to a SKU renewed for the next billing period.
EntitlementUpdate(EntitlementUpdateEvent),
/// A user's entitlement was deleted by Discord, or refunded.
EntitlementDelete(EntitlementDeleteEvent),
/// An event type not covered by the above
#[serde(other, deserialize_with = "ignore_input")]
Unknown,
Expand Down

0 comments on commit 4256861

Please sign in to comment.