Skip to content

Commit

Permalink
add endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoffi committed Aug 2, 2024
1 parent 87c3490 commit 9620435
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
10 changes: 10 additions & 0 deletions endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
70 changes: 70 additions & 0 deletions restapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
19 changes: 19 additions & 0 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 2453 in structs.go

View workflow job for this annotation

GitHub Actions / lint

exported const EntitlementOwnerTypeGuildSubscription should have comment (or a comment on this block) or be unexported (golint)
EntitlementOwnerTypeUserSubscription EntitlementOwnerType = 2
)

type EntitlementTest struct {

Check failure on line 2457 in structs.go

View workflow job for this annotation

GitHub Actions / lint

exported type `EntitlementTest` should have comment or be unexported (golint)
// 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
Expand Down

0 comments on commit 9620435

Please sign in to comment.