feat: implement Subscription events (#1591)

This commit is contained in:
aping 2025-03-17 17:46:31 +08:00 committed by GitHub
parent d4827e8d77
commit 7f344fbe5e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 63 additions and 0 deletions

View file

@ -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))

View file

@ -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
}

View file

@ -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"`