From 7f344fbe5e3b19b2c52d789b2ecee5199876cb13 Mon Sep 17 00:00:00 2001 From: aping Date: Mon, 17 Mar 2025 17:46:31 +0800 Subject: [PATCH] feat: implement Subscription events (#1591) --- eventhandlers.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ events.go | 12 ++++++++++++ structs.go | 3 +++ 3 files changed, 63 insertions(+) diff --git a/eventhandlers.go b/eventhandlers.go index 06df984..44869f5 100644 --- a/eventhandlers.go +++ b/eventhandlers.go @@ -62,6 +62,8 @@ const ( rateLimitEventType = "__RATE_LIMIT__" readyEventType = "READY" resumedEventType = "RESUMED" + subscriptionCreateEventType = "SUBSCRIPTION_CREATE" + subscriptionUpdateEventType = "SUBSCRIPTION_UPDATE" stageInstanceEventCreateEventType = "STAGE_INSTANCE_EVENT_CREATE" stageInstanceEventDeleteEventType = "STAGE_INSTANCE_EVENT_DELETE" stageInstanceEventUpdateEventType = "STAGE_INSTANCE_EVENT_UPDATE" @@ -1158,6 +1160,46 @@ func (eh resumedEventHandler) Handle(s *Session, i interface{}) { } } +// subscriptionCreateEventHandler is an event handler for SubscriptionCreate events. +type subscriptionCreateEventHandler func(*Session, *SubscriptionCreate) + +// Type returns the event type for SubscriptionCreate events. +func (eh subscriptionCreateEventHandler) Type() string { + return subscriptionCreateEventType +} + +// New returns a new instance of SubscriptionCreate. +func (eh subscriptionCreateEventHandler) New() interface{} { + return &SubscriptionCreate{} +} + +// Handle is the handler for SubscriptionCreate events. +func (eh subscriptionCreateEventHandler) Handle(s *Session, i interface{}) { + if t, ok := i.(*SubscriptionCreate); ok { + eh(s, t) + } +} + +// subscriptionUpdateEventHandler is an event handler for SubscriptionUpdate events. +type subscriptionUpdateEventHandler func(*Session, *SubscriptionUpdate) + +// Type returns the event type for SubscriptionCreate events. +func (eh subscriptionUpdateEventHandler) Type() string { + return subscriptionUpdateEventType +} + +// New returns a new instance of SubscriptionUpdate. +func (eh subscriptionUpdateEventHandler) New() interface{} { + return &SubscriptionUpdate{} +} + +// Handle is the handler for SubscriptionUpdate events. +func (eh subscriptionUpdateEventHandler) Handle(s *Session, i interface{}) { + if t, ok := i.(*SubscriptionUpdate); ok { + eh(s, t) + } +} + // stageInstanceEventCreateEventHandler is an event handler for StageInstanceEventCreate events. type stageInstanceEventCreateEventHandler func(*Session, *StageInstanceEventCreate) @@ -1552,6 +1594,10 @@ func handlerForInterface(handler interface{}) EventHandler { return readyEventHandler(v) case func(*Session, *Resumed): return resumedEventHandler(v) + case func(*Session, *SubscriptionCreate): + return subscriptionCreateEventHandler(v) + case func(*Session, *SubscriptionUpdate): + return subscriptionUpdateEventHandler(v) case func(*Session, *StageInstanceEventCreate): return stageInstanceEventCreateEventHandler(v) case func(*Session, *StageInstanceEventDelete): @@ -1637,6 +1683,8 @@ func init() { registerInterfaceProvider(presencesReplaceEventHandler(nil)) registerInterfaceProvider(readyEventHandler(nil)) registerInterfaceProvider(resumedEventHandler(nil)) + registerInterfaceProvider(subscriptionCreateEventHandler(nil)) + registerInterfaceProvider(subscriptionUpdateEventHandler(nil)) registerInterfaceProvider(stageInstanceEventCreateEventHandler(nil)) registerInterfaceProvider(stageInstanceEventDeleteEventHandler(nil)) registerInterfaceProvider(stageInstanceEventUpdateEventHandler(nil)) diff --git a/events.go b/events.go index 6a410fe..3d36458 100644 --- a/events.go +++ b/events.go @@ -461,3 +461,15 @@ type EntitlementUpdate struct { type EntitlementDelete struct { *Entitlement } + +// SubscriptionCreate is the data for an SubscriptionCreate event. +// https://discord.com/developers/docs/monetization/implementing-app-subscriptions#using-subscription-events-for-the-subscription-lifecycle +type SubscriptionCreate struct { + *Subscription +} + +// SubscriptionUpdate is the data for an SubscriptionUpdate event. +// https://discord.com/developers/docs/monetization/implementing-app-subscriptions#using-subscription-events-for-the-subscription-lifecycle +type SubscriptionUpdate struct { + *Subscription +} diff --git a/structs.go b/structs.go index b87d566..3350d61 100644 --- a/structs.go +++ b/structs.go @@ -2476,6 +2476,9 @@ type Subscription struct { // List of entitlements granted for this subscription EntitlementIDs []string `json:"entitlement_ids"` + // List of SKUs that this user will be subscribed to at renewal + RenewalSKUIDs []string `json:"renewal_sku_ids,omitempty"` + // Start of the current subscription period CurrentPeriodStart time.Time `json:"current_period_start"`