Skip to content

Commit

Permalink
Add subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
sebm253 committed Aug 28, 2024
1 parent a979bbb commit fcbf484
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 9 deletions.
27 changes: 27 additions & 0 deletions discord/subscription.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package discord

import (
"time"

"github.com/disgoorg/snowflake/v2"
)

type Subscription struct {
ID snowflake.ID `json:"id"`
UserID snowflake.ID `json:"user_id"`
SkuIDs []snowflake.ID `json:"sku_ids"`
EntitlementIDs []snowflake.ID `json:"entitlement_ids"`
CurrentPeriodStart time.Time `json:"current_period_start"`
CurrentPeriodEnd time.Time `json:"current_period_end"`
Status SubscriptionStatus `json:"status"`
CanceledAt *time.Time `json:"canceled_at"`
Country *string `json:"country"`
}

type SubscriptionStatus int

const (
SubscriptionStatusActive SubscriptionStatus = iota + 1
SubscriptionStatusEnding
SubscriptionStatusInactive
)
19 changes: 19 additions & 0 deletions events/listener_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ type ListenerAdapter struct {
OnEntitlementUpdate func(event *EntitlementUpdate)
OnEntitlementDelete func(event *EntitlementDelete)

// Subscription Events
OnSubscriptionCreate func(event *SubscriptionCreate)
OnSubscriptionUpdate func(event *SubscriptionUpdate)
OnSubscriptionDelete func(event *SubscriptionDelete)

// Sticker Events
OnStickersUpdate func(event *StickersUpdate)
OnStickerCreate func(event *StickerCreate)
Expand Down Expand Up @@ -353,6 +358,20 @@ func (l *ListenerAdapter) OnEvent(event bot.Event) {
listener(e)
}

// Subscription Events
case *SubscriptionCreate:
if listener := l.OnSubscriptionCreate; listener != nil {
listener(e)
}
case *SubscriptionUpdate:
if listener := l.OnSubscriptionUpdate; listener != nil {
listener(e)
}
case *SubscriptionDelete:
if listener := l.OnSubscriptionDelete; listener != nil {
listener(e)
}

// Sticker Events
case *StickersUpdate:
if listener := l.OnStickersUpdate; listener != nil {
Expand Down
20 changes: 20 additions & 0 deletions events/subscription_events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package events

import "github.com/disgoorg/disgo/discord"

type GenericSubscriptionEvent struct {
*GenericEvent
discord.Subscription
}

type SubscriptionCreate struct {
*GenericSubscriptionEvent
}

type SubscriptionUpdate struct {
*GenericSubscriptionEvent
}

type SubscriptionDelete struct {
*GenericSubscriptionEvent
}
3 changes: 3 additions & 0 deletions gateway/gateway_event_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ const (
EventTypeStageInstanceCreate EventType = "STAGE_INSTANCE_CREATE"
EventTypeStageInstanceDelete EventType = "STAGE_INSTANCE_DELETE"
EventTypeStageInstanceUpdate EventType = "STAGE_INSTANCE_UPDATE"
EventTypeSubscriptionCreate EventType = "SUBSCRIPTION_CREATE"
EventTypeSubscriptionUpdate EventType = "SUBSCRIPTION_UPDATE"
EventTypeSubscriptionDelete EventType = "SUBSCRIPTION_DELETE"
EventTypeTypingStart EventType = "TYPING_START"
EventTypeUserUpdate EventType = "USER_UPDATE"
EventTypeVoiceChannelEffectSend EventType = "VOICE_CHANNEL_EFFECT_SEND"
Expand Down
21 changes: 21 additions & 0 deletions gateway/gateway_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,3 +796,24 @@ type EventEntitlementDelete struct {

func (EventEntitlementDelete) messageData() {}
func (EventEntitlementDelete) eventData() {}

type EventSubscriptionCreate struct {
discord.Subscription
}

func (EventSubscriptionCreate) messageData() {}
func (EventSubscriptionCreate) eventData() {}

type EventSubscriptionUpdate struct {
discord.Subscription
}

func (EventSubscriptionUpdate) messageData() {}
func (EventSubscriptionUpdate) eventData() {}

type EventSubscriptionDelete struct {
discord.Subscription
}

func (EventSubscriptionDelete) messageData() {}
func (EventSubscriptionDelete) eventData() {}
15 changes: 15 additions & 0 deletions gateway/gateway_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,21 @@ func UnmarshalEventData(data []byte, eventType EventType) (EventData, error) {
err = json.Unmarshal(data, &d)
eventData = d

case EventTypeSubscriptionCreate:
var d EventSubscriptionCreate
err = json.Unmarshal(data, &d)
eventData = d

case EventTypeSubscriptionUpdate:
var d EventSubscriptionUpdate
err = json.Unmarshal(data, &d)
eventData = d

case EventTypeSubscriptionDelete:
var d EventSubscriptionDelete
err = json.Unmarshal(data, &d)
eventData = d

case EventTypeTypingStart:
var d EventTypingStart
err = json.Unmarshal(data, &d)
Expand Down
4 changes: 4 additions & 0 deletions handlers/all_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ var allEventHandlers = []bot.GatewayEventHandler{
bot.NewGatewayEventHandler(gateway.EventTypeStageInstanceUpdate, gatewayHandlerStageInstanceUpdate),
bot.NewGatewayEventHandler(gateway.EventTypeStageInstanceDelete, gatewayHandlerStageInstanceDelete),

bot.NewGatewayEventHandler(gateway.EventTypeSubscriptionCreate, gatewayHandlerSubscriptionCreate),
bot.NewGatewayEventHandler(gateway.EventTypeSubscriptionUpdate, gatewayHandlerSubscriptionUpdate),
bot.NewGatewayEventHandler(gateway.EventTypeSubscriptionDelete, gatewayHandlerSubscriptionDelete),

bot.NewGatewayEventHandler(gateway.EventTypeTypingStart, gatewayHandlerTypingStart),
bot.NewGatewayEventHandler(gateway.EventTypeUserUpdate, gatewayHandlerUserUpdate),

Expand Down
34 changes: 34 additions & 0 deletions handlers/subscription_handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package handlers

import (
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/gateway"
)

func gatewayHandlerSubscriptionCreate(client bot.Client, sequenceNumber int, shardID int, event gateway.EventSubscriptionCreate) {
client.EventManager().DispatchEvent(&events.SubscriptionCreate{
GenericSubscriptionEvent: &events.GenericSubscriptionEvent{
GenericEvent: events.NewGenericEvent(client, sequenceNumber, shardID),
Subscription: event.Subscription,
},
})
}

func gatewayHandlerSubscriptionUpdate(client bot.Client, sequenceNumber int, shardID int, event gateway.EventSubscriptionUpdate) {
client.EventManager().DispatchEvent(&events.SubscriptionUpdate{
GenericSubscriptionEvent: &events.GenericSubscriptionEvent{
GenericEvent: events.NewGenericEvent(client, sequenceNumber, shardID),
Subscription: event.Subscription,
},
})
}

func gatewayHandlerSubscriptionDelete(client bot.Client, sequenceNumber int, shardID int, event gateway.EventSubscriptionDelete) {
client.EventManager().DispatchEvent(&events.SubscriptionDelete{
GenericSubscriptionEvent: &events.GenericSubscriptionEvent{
GenericEvent: events.NewGenericEvent(client, sequenceNumber, shardID),
Subscription: event.Subscription,
},
})
}
7 changes: 0 additions & 7 deletions rest/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ type Applications interface {
DeleteTestEntitlement(applicationID snowflake.ID, entitlementID snowflake.ID, opts ...RequestOpt) error
ConsumeEntitlement(applicationID snowflake.ID, entitlementID snowflake.ID, opts ...RequestOpt) error

GetSKUs(applicationID snowflake.ID, opts ...RequestOpt) ([]discord.SKU, error)

GetApplicationEmojis(applicationID snowflake.ID, opts ...RequestOpt) ([]discord.Emoji, error)
GetApplicationEmoji(applicationID snowflake.ID, emojiID snowflake.ID, opts ...RequestOpt) (*discord.Emoji, error)
CreateApplicationEmoji(applicationID snowflake.ID, emojiCreate discord.EmojiCreate, opts ...RequestOpt) (*discord.Emoji, error)
Expand Down Expand Up @@ -222,11 +220,6 @@ func (s *applicationsImpl) ConsumeEntitlement(applicationID snowflake.ID, entitl
return s.client.Do(ConsumeEntitlement.Compile(nil, applicationID, entitlementID), nil, nil, opts...)
}

func (s *applicationsImpl) GetSKUs(applicationID snowflake.ID, opts ...RequestOpt) (skus []discord.SKU, err error) {
err = s.client.Do(GetSKUs.Compile(nil, applicationID), nil, &skus, opts...)
return
}

func (s *applicationsImpl) GetApplicationEmojis(applicationID snowflake.ID, opts ...RequestOpt) (emojis []discord.Emoji, err error) {
var rs emojisResponse
err = s.client.Do(GetApplicationEmojis.Compile(nil, applicationID), nil, &rs, opts...)
Expand Down
3 changes: 3 additions & 0 deletions rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Rest interface {
StageInstances
Emojis
Stickers
SKUs
GuildScheduledEvents
}

Expand All @@ -47,6 +48,7 @@ func New(client Client) Rest {
StageInstances: NewStageInstances(client),
Emojis: NewEmojis(client),
Stickers: NewStickers(client),
SKUs: NewSKUs(client),
GuildScheduledEvents: NewGuildScheduledEvents(client),
}
}
Expand All @@ -71,5 +73,6 @@ type restImpl struct {
StageInstances
Emojis
Stickers
SKUs
GuildScheduledEvents
}
10 changes: 8 additions & 2 deletions rest/rest_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,6 @@ var (
DeleteTestEntitlement = NewEndpoint(http.MethodDelete, "/applications/{application.id}/entitlements/{entitlement.id}")
ConsumeEntitlement = NewEndpoint(http.MethodPost, "/applications/{application.id}/entitlements/{entitlement.id}/consume")

GetSKUs = NewEndpoint(http.MethodGet, "/applications/{application.id}/skus")

GetApplicationEmojis = NewEndpoint(http.MethodGet, "/applications/{application.id}/emojis")
GetApplicationEmoji = NewEndpoint(http.MethodGet, "/applications/{application.id}/emojis/{emoji.id}")
CreateApplicationEmoji = NewEndpoint(http.MethodPost, "/applications/{application.id}/emojis")
Expand All @@ -317,6 +315,14 @@ var (
GetActivityInstance = NewEndpoint(http.MethodGet, "/applications/{application.id}/activity-instances/{instance.id}")
)

// SKUs
var (
GetSKUs = NewEndpoint(http.MethodGet, "/applications/{application.id}/skus")

GetSKUSubscriptions = NewEndpoint(http.MethodGet, "/skus/{sku.id}/subscriptions")
GetSKUSubscription = NewEndpoint(http.MethodGet, "/skus/{sku.id}/subscriptions/{subscription.id}")
)

// NewEndpoint returns a new Endpoint which requires bot auth with the given http method & route.
func NewEndpoint(method string, route string) *Endpoint {
return &Endpoint{
Expand Down
64 changes: 64 additions & 0 deletions rest/skus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package rest

import (
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/snowflake/v2"
)

var _ SKUs = (*skusImpl)(nil)

func NewSKUs(client Client) SKUs {
return &skusImpl{client: client}
}

type SKUs interface {
GetSKUs(applicationID snowflake.ID, opts ...RequestOpt) ([]discord.SKU, error)

GetSKUSubscriptions(skuID snowflake.ID, before snowflake.ID, after snowflake.ID, limit int, userID snowflake.ID, opts ...RequestOpt) ([]discord.Subscription, error)
GetSKUSubscriptionsPage(skuID snowflake.ID, userID snowflake.ID, startID snowflake.ID, limit int, opts ...RequestOpt) Page[discord.Subscription]
GetSKUSubscription(skuID snowflake.ID, subscriptionID snowflake.ID, opts ...RequestOpt) (*discord.Subscription, error)
}

type skusImpl struct {
client Client
}

func (s *skusImpl) GetSKUs(applicationID snowflake.ID, opts ...RequestOpt) (skus []discord.SKU, err error) {
err = s.client.Do(GetSKUs.Compile(nil, applicationID), nil, &skus, opts...)
return
}

func (s *skusImpl) GetSKUSubscriptions(skuID snowflake.ID, before snowflake.ID, after snowflake.ID, limit int, userID snowflake.ID, opts ...RequestOpt) (subscriptions []discord.Subscription, err error) {
values := discord.QueryValues{}
if before != 0 {
values["before"] = before
}
if after != 0 {
values["after"] = after
}
if limit != 0 {
values["limit"] = limit
}
if userID != 0 {
values["user_id"] = userID
}
err = s.client.Do(GetSKUSubscriptions.Compile(values, skuID), nil, &subscriptions, opts...)
return
}

func (s *skusImpl) GetSKUSubscriptionsPage(skuID snowflake.ID, userID snowflake.ID, startID snowflake.ID, limit int, opts ...RequestOpt) Page[discord.Subscription] {
return Page[discord.Subscription]{
getItemsFunc: func(before snowflake.ID, after snowflake.ID) ([]discord.Subscription, error) {
return s.GetSKUSubscriptions(skuID, before, after, limit, userID, opts...)
},
getIDFunc: func(subscription discord.Subscription) snowflake.ID {
return subscription.ID
},
ID: startID,
}
}

func (s *skusImpl) GetSKUSubscription(skuID snowflake.ID, subscriptionID snowflake.ID, opts ...RequestOpt) (subscription *discord.Subscription, err error) {
err = s.client.Do(GetSKUSubscription.Compile(nil, skuID, subscriptionID), nil, &subscription, opts...)
return
}

0 comments on commit fcbf484

Please sign in to comment.