Skip to content

Commit

Permalink
Add GetEntitlement and new GetEntitlements param (#402)
Browse files Browse the repository at this point in the history
* Add GetEntitlement and new GetEntitlements param

* move params into a struct

* move QueryParamsGetEntitlements

* shorten struct name

* rename struct again because ton cant decide
  • Loading branch information
sebm253 authored Dec 2, 2024
1 parent 7266716 commit 4c19fca
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 32 deletions.
23 changes: 13 additions & 10 deletions discord/entitlement.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ import (
)

type Entitlement struct {
ID snowflake.ID `json:"id"`
SkuID snowflake.ID `json:"sku_id"`
ApplicationID snowflake.ID `json:"application_id"`
UserID *snowflake.ID `json:"user_id"`
Type EntitlementType `json:"type"`
Deleted bool `json:"deleted"`
StartsAt *time.Time `json:"starts_at"`
EndsAt *time.Time `json:"ends_at"`
GuildID *snowflake.ID `json:"guild_id"`
Consumed *bool `json:"consumed"`
ID snowflake.ID `json:"id"`
SkuID snowflake.ID `json:"sku_id"`
ApplicationID snowflake.ID `json:"application_id"`
UserID *snowflake.ID `json:"user_id"`
PromotionID *snowflake.ID `json:"promotion_id"`
Type EntitlementType `json:"type"`
Deleted bool `json:"deleted"`
GiftCodeFlags int `json:"gift_code_flags"`
Consumed *bool `json:"consumed"`
StartsAt *time.Time `json:"starts_at"`
EndsAt *time.Time `json:"ends_at"`
GuildID *snowflake.ID `json:"guild_id"`
SubscriptionID *snowflake.ID `json:"subscription_id"`
}

type EntitlementType int
Expand Down
67 changes: 45 additions & 22 deletions rest/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ type Applications interface {
GetApplicationRoleConnectionMetadata(applicationID snowflake.ID, opts ...RequestOpt) ([]discord.ApplicationRoleConnectionMetadata, error)
UpdateApplicationRoleConnectionMetadata(applicationID snowflake.ID, newRecords []discord.ApplicationRoleConnectionMetadata, opts ...RequestOpt) ([]discord.ApplicationRoleConnectionMetadata, error)

GetEntitlements(applicationID snowflake.ID, userID snowflake.ID, guildID snowflake.ID, before snowflake.ID, after snowflake.ID, limit int, excludeEnded bool, skuIDs []snowflake.ID, opts ...RequestOpt) ([]discord.Entitlement, error)
GetEntitlements(applicationID snowflake.ID, params GetEntitlementsParams, opts ...RequestOpt) ([]discord.Entitlement, error)
GetEntitlement(applicationID snowflake.ID, entitlementID snowflake.ID, opts ...RequestOpt) (*discord.Entitlement, error)
CreateTestEntitlement(applicationID snowflake.ID, entitlementCreate discord.TestEntitlementCreate, opts ...RequestOpt) (*discord.Entitlement, error)
DeleteTestEntitlement(applicationID snowflake.ID, entitlementID snowflake.ID, opts ...RequestOpt) error
ConsumeEntitlement(applicationID snowflake.ID, entitlementID snowflake.ID, opts ...RequestOpt) error
Expand All @@ -51,6 +52,42 @@ type Applications interface {
GetActivityInstance(applicationID snowflake.ID, instanceID string, opts ...RequestOpt) (*discord.ActivityInstance, error)
}

// GetEntitlementsParams holds query parameters for Applications.GetEntitlements (https://discord.com/developers/docs/resources/entitlement#list-entitlements)
type GetEntitlementsParams struct {
UserID snowflake.ID
SkuIDs []snowflake.ID
Before int
After int
Limit int
GuildID snowflake.ID
ExcludeEnded bool
ExcludeDeleted bool
}

func (p GetEntitlementsParams) ToQueryValues() discord.QueryValues {
queryValues := discord.QueryValues{
"exclude_ended": p.ExcludeEnded,
"exclude_deleted": p.ExcludeDeleted,
"sku_ids": slicehelper.JoinSnowflakes(p.SkuIDs),
}
if p.UserID != 0 {
queryValues["user_id"] = p.UserID
}
if p.Before != 0 {
queryValues["before"] = p.Before
}
if p.After != 0 {
queryValues["after"] = p.After
}
if p.Limit != 0 {
queryValues["limit"] = p.Limit
}
if p.GuildID != 0 {
queryValues["guild_id"] = p.GuildID
}
return queryValues
}

type applicationsImpl struct {
client Client
}
Expand Down Expand Up @@ -183,27 +220,13 @@ func (s *applicationsImpl) UpdateApplicationRoleConnectionMetadata(applicationID
return
}

func (s *applicationsImpl) GetEntitlements(applicationID snowflake.ID, userID snowflake.ID, guildID snowflake.ID, before snowflake.ID, after snowflake.ID, limit int, excludeEnded bool, skuIDs []snowflake.ID, opts ...RequestOpt) (entitlements []discord.Entitlement, err error) {
queryValues := discord.QueryValues{
"exclude_ended": excludeEnded,
"sku_ids": slicehelper.JoinSnowflakes(skuIDs),
}
if userID != 0 {
queryValues["user_id"] = userID
}
if guildID != 0 {
queryValues["guild_id"] = guildID
}
if before != 0 {
queryValues["before"] = before
}
if after != 0 {
queryValues["after"] = after
}
if limit != 0 {
queryValues["limit"] = limit
}
err = s.client.Do(GetEntitlements.Compile(queryValues, applicationID), nil, &entitlements, opts...)
func (s *applicationsImpl) GetEntitlements(applicationID snowflake.ID, params GetEntitlementsParams, opts ...RequestOpt) (entitlements []discord.Entitlement, err error) {
err = s.client.Do(GetEntitlements.Compile(params.ToQueryValues(), applicationID), nil, &entitlements, opts...)
return
}

func (s *applicationsImpl) GetEntitlement(applicationID snowflake.ID, entitlementID snowflake.ID, opts ...RequestOpt) (entitlement *discord.Entitlement, err error) {
err = s.client.Do(GetEntitlement.Compile(nil, applicationID, entitlementID), nil, &entitlement, opts...)
return
}

Expand Down
1 change: 1 addition & 0 deletions rest/rest_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ var (
UpdateApplicationRoleConnectionMetadata = NewEndpoint(http.MethodPut, "/applications/{application.id}/role-connections/metadata")

GetEntitlements = NewEndpoint(http.MethodGet, "/applications/{application.id}/entitlements")
GetEntitlement = NewEndpoint(http.MethodGet, "/applications/{application.id}/entitlements/{entitlement.id}")
CreateTestEntitlement = NewEndpoint(http.MethodPost, "/applications/{application.id}/entitlements")
DeleteTestEntitlement = NewEndpoint(http.MethodDelete, "/applications/{application.id}/entitlements/{entitlement.id}")
ConsumeEntitlement = NewEndpoint(http.MethodPost, "/applications/{application.id}/entitlements/{entitlement.id}/consume")
Expand Down

0 comments on commit 4c19fca

Please sign in to comment.