From 962043529b4172a6adcd1486250bad42c3479c4d Mon Sep 17 00:00:00 2001 From: Jonas Date: Sat, 3 Aug 2024 00:41:54 +0200 Subject: [PATCH] add endpoints --- endpoints.go | 10 ++++++++ restapi.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ structs.go | 19 ++++++++++++++ 3 files changed, 99 insertions(+) diff --git a/endpoints.go b/endpoints.go index 29189af69..1a4731819 100644 --- a/endpoints.go +++ b/endpoints.go @@ -172,6 +172,16 @@ var ( return EndpointPoll(cID, mID) + "/expire" } + EndpointEntitlements = func(aID string) string { + return EndpointApplication(aID) + "/entitlements" + } + EndpointEntitlement = func(aID, eID string) string { + return EndpointEntitlements(aID) + "/" + eID + } + EndpointEntitlementConsume = func(aID, eID string) string { + return EndpointEntitlement(aID, eID) + "/consume" + } + EndpointApplicationGlobalCommands = func(aID string) string { return EndpointApplication(aID) + "/commands" } diff --git a/restapi.go b/restapi.go index ffb8e3022..ae53debcd 100644 --- a/restapi.go +++ b/restapi.go @@ -3500,3 +3500,73 @@ func (s *Session) PollExpire(channelID, messageID string) (msg *Message, err err err = unmarshal(body, &msg) return } + +// ---------------------------------------------------------------------- +// Functions specific to entitlements +// ---------------------------------------------------------------------- + +// Entitlements returns all antitlements for a given app, active and expired. +// appID : The ID of the application. +// userID : Optional user ID to look up for. +// skuIDs : Optional array of SKU IDs to check for. +// before : Optional timestamp to retrieve Entitlements before this time. +// after : Optional timestamp to retrieve Entitlements after this time. +// limit : Optional maximum number of entitlements to return (1-100, default 100). +// guildID : Optional guild ID to look up for. +// exclude_ended : Optional whether or not ended entitlements should be omitted. +func (s *Session) Entitlements(appID, userID string, skuIDs *[]string, beforeID, afterID string, limit int, guildID string, excludeEnded bool, options ...RequestOption) (entitlements []*Entitlement, err error) { + endpoint := EndpointEntitlements(appID) + + queryParams := url.Values{} + if userID != "" { + queryParams.Set("user_id", userID) + } + if skuIDs != nil && len(*skuIDs) > 0 { + queryParams.Set("sku_ids", strings.Join(*skuIDs, ",")) + } + if beforeID != "" { + queryParams.Set("before", beforeID) + } + if afterID != "" { + queryParams.Set("after", afterID) + } + if limit > 0 { + queryParams.Set("limit", strconv.Itoa(limit)) + } + if guildID != "" { + queryParams.Set("guild_id", guildID) + } + if excludeEnded { + queryParams.Set("exclude_ended", "true") + } + + body, err := s.RequestWithBucketID("GET", endpoint+"?"+queryParams.Encode(), nil, endpoint, options...) + if err != nil { + return + } + + err = unmarshal(body, &entitlements) + return +} + +// EntitlementConsume marks a given One-Time Purchase for the user as consumed. +func (s *Session) EntitlementConsume(appID, entitlementID string, options ...RequestOption) (err error) { + _, err = s.RequestWithBucketID("POST", EndpointEntitlementConsume(appID, entitlementID), nil, EndpointEntitlementConsume(appID, ""), options...) + return +} + +// EntitlementTestCreate creates a test entitlement to a given SKU for a given guild or user. +// Discord will act as though that user or guild has entitlement to your premium offering. +func (s *Session) EntitlementTestCreate(appID string, data *EntitlementTest, options ...RequestOption) (err error) { + endpoint := EndpointEntitlements(appID) + + _, err = s.RequestWithBucketID("POST", endpoint, data, endpoint, options...) + return +} + +// EntitlementTestDelete deletes a currently-active test entitlement. Discord will act as though +// that user or guild no longer has entitlement to your premium offering. +func (s *Session) EntitlementTestDelete(appID, entitlementID string, options ...RequestOption) (err error) { + _, err = s.RequestWithBucketID("DELETE", EndpointEntitlement(appID, entitlementID), nil, EndpointEntitlement(appID, ""), options...) + return +} diff --git a/structs.go b/structs.go index 9b2cc57ac..5b57d0022 100644 --- a/structs.go +++ b/structs.go @@ -2446,6 +2446,25 @@ type Entitlement struct { SubscriptionID string `json:"subscription_id,omitempty"` } +// EntitlementOwnerType is the type of entitlement +type EntitlementOwnerType int + +const ( + EntitlementOwnerTypeGuildSubscription EntitlementOwnerType = 1 + EntitlementOwnerTypeUserSubscription EntitlementOwnerType = 2 +) + +type EntitlementTest struct { + // The ID of the SKU to grant the entitlement to + SkuID string `json:"sku_id"` + + // The ID of the guild or user to grant the entitlement to + OwnerID string `json:"owner_id"` + + // OwnerType is the type of which the entitlement should be created + OwnerType EntitlementOwnerType `json:"owner_type"` +} + // Constants for the different bit offsets of text channel permissions const ( // Deprecated: PermissionReadMessages has been replaced with PermissionViewChannel for text and voice channels