From 5a9ed6695be52e20a45a2c0aec21d3d8dde064e1 Mon Sep 17 00:00:00 2001 From: cane Date: Sat, 14 Oct 2023 11:10:36 +0200 Subject: [PATCH 1/4] Update subscription objects to match the docs (#322) --- discord/entitlement.go | 22 +++++++++------------- discord/sku.go | 7 ++++++- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/discord/entitlement.go b/discord/entitlement.go index fb0e763e..756aad76 100644 --- a/discord/entitlement.go +++ b/discord/entitlement.go @@ -7,19 +7,15 @@ import ( ) type Entitlement struct { - ID snowflake.ID `json:"id"` - SkuID snowflake.ID `json:"sku_id"` - UserID *snowflake.ID `json:"user_id"` - GuildID *snowflake.ID `json:"guild_id"` - ApplicationID snowflake.ID `json:"application_id"` - Type EntitlementType `json:"type"` - Consumed bool `json:"consumed"` - StartsAt *time.Time `json:"starts_at"` - EndsAt *time.Time `json:"ends_at"` - PromotionID *snowflake.ID `json:"promotion_id"` - Deleted bool `json:"deleted"` - GiftCodeFlags int `json:"gift_code_flags"` - SubscriptionID *snowflake.ID `json:"subscription_id"` + 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"` } type EntitlementType int diff --git a/discord/sku.go b/discord/sku.go index 20366a45..6e3858e6 100644 --- a/discord/sku.go +++ b/discord/sku.go @@ -31,6 +31,11 @@ const ( type SKUFlags int const ( - SKUFlagGuildSubscription SKUFlags = 1 << (iota + 7) + SKUFlagAvailable SKUFlags = 1 << (iota + 2) + _ + _ + _ + _ + SKUFlagGuildSubscription SKUFlagUserSubscription ) From c50b92ff0ac42076d3ad44a3a07d86743291c2d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?To=CF=80?= Date: Wed, 1 Nov 2023 00:33:40 +0100 Subject: [PATCH 2/4] fix incorrect expiration in oauth sessions --- oauth2/client_impl.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oauth2/client_impl.go b/oauth2/client_impl.go index 080968bf..f3dadff5 100644 --- a/oauth2/client_impl.go +++ b/oauth2/client_impl.go @@ -158,6 +158,6 @@ func newSession(accessToken discord.AccessTokenResponse) Session { RefreshToken: accessToken.RefreshToken, Scopes: accessToken.Scope, TokenType: accessToken.TokenType, - Expiration: time.Now().Add(accessToken.ExpiresIn * time.Second), + Expiration: time.Now().Add(accessToken.ExpiresIn), } } From 631c26135bb5b79fe773e49d39f09006fa8a4a8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?To=CF=80?= Date: Thu, 2 Nov 2023 00:37:02 +0100 Subject: [PATCH 3/4] fix problem with oauth2 endpoints (#323) --- discord/user.go | 4 ++-- rest/oauth2.go | 28 ++++++++++++++++++++++++++++ rest/rest_endpoints.go | 10 +++++----- rest/users.go | 24 +++--------------------- 4 files changed, 38 insertions(+), 28 deletions(-) diff --git a/discord/user.go b/discord/user.go index caa63b8d..c0fe7535 100644 --- a/discord/user.go +++ b/discord/user.go @@ -181,8 +181,8 @@ const ( PremiumTypeNitroBasic ) -// SelfUserUpdate is the payload used to update the OAuth2User -type SelfUserUpdate struct { +// UserUpdate is the payload used to update the OAuth2User +type UserUpdate struct { Username string `json:"username,omitempty"` Avatar *json.Nullable[Icon] `json:"avatar,omitempty"` } diff --git a/rest/oauth2.go b/rest/oauth2.go index a0948430..8843edf5 100644 --- a/rest/oauth2.go +++ b/rest/oauth2.go @@ -1,6 +1,7 @@ package rest import ( + "errors" "net/url" "github.com/disgoorg/snowflake/v2" @@ -8,6 +9,9 @@ import ( "github.com/disgoorg/disgo/discord" ) +// ErrMissingBearerToken is returned when a bearer token is missing for a request which requires it. +var ErrMissingBearerToken = errors.New("missing bearer token") + var _ OAuth2 = (*oAuth2Impl)(nil) func NewOAuth2(client Client) OAuth2 { @@ -18,9 +22,15 @@ type OAuth2 interface { GetBotApplicationInfo(opts ...RequestOpt) (*discord.Application, error) GetCurrentAuthorizationInfo(bearerToken string, opts ...RequestOpt) (*discord.AuthorizationInformation, error) + // GetCurrentUser returns the current user + // Leave bearerToken empty to use the bot token. GetCurrentUser(bearerToken string, opts ...RequestOpt) (*discord.OAuth2User, error) GetCurrentMember(bearerToken string, guildID snowflake.ID, opts ...RequestOpt) (*discord.Member, error) + // GetCurrentUserGuilds returns a list of guilds the current user is a member of. Requires the discord.OAuth2ScopeGuilds scope. + // Leave bearerToken empty to use the bot token. GetCurrentUserGuilds(bearerToken string, before snowflake.ID, after snowflake.ID, limit int, withCounts bool, opts ...RequestOpt) ([]discord.OAuth2Guild, error) + // GetCurrentUserGuildsPage returns a Page of guilds the current user is a member of. Requires the discord.OAuth2ScopeGuilds scope. + // Leave bearerToken empty to use the bot token. GetCurrentUserGuildsPage(bearerToken string, startID snowflake.ID, limit int, withCounts bool, opts ...RequestOpt) Page[discord.OAuth2Guild] GetCurrentUserConnections(bearerToken string, opts ...RequestOpt) ([]discord.Connection, error) @@ -50,6 +60,9 @@ func (s *oAuth2Impl) GetBotApplicationInfo(opts ...RequestOpt) (application *dis } func (s *oAuth2Impl) GetCurrentAuthorizationInfo(bearerToken string, opts ...RequestOpt) (info *discord.AuthorizationInformation, err error) { + if bearerToken == "" { + return nil, ErrMissingBearerToken + } err = s.client.Do(GetAuthorizationInfo.Compile(nil), nil, &info, withBearerToken(bearerToken, opts)...) return } @@ -60,6 +73,9 @@ func (s *oAuth2Impl) GetCurrentUser(bearerToken string, opts ...RequestOpt) (use } func (s *oAuth2Impl) GetCurrentMember(bearerToken string, guildID snowflake.ID, opts ...RequestOpt) (member *discord.Member, err error) { + if bearerToken == "" { + return nil, ErrMissingBearerToken + } err = s.client.Do(GetCurrentMember.Compile(nil, guildID), nil, &member, withBearerToken(bearerToken, opts)...) return } @@ -94,21 +110,33 @@ func (s *oAuth2Impl) GetCurrentUserGuildsPage(bearerToken string, startID snowfl } func (s *oAuth2Impl) GetCurrentUserConnections(bearerToken string, opts ...RequestOpt) (connections []discord.Connection, err error) { + if bearerToken == "" { + return nil, ErrMissingBearerToken + } err = s.client.Do(GetCurrentUserConnections.Compile(nil), nil, &connections, withBearerToken(bearerToken, opts)...) return } func (s *oAuth2Impl) SetGuildCommandPermissions(bearerToken string, applicationID snowflake.ID, guildID snowflake.ID, commandID snowflake.ID, commandPermissions []discord.ApplicationCommandPermission, opts ...RequestOpt) (commandPerms *discord.ApplicationCommandPermissions, err error) { + if bearerToken == "" { + return nil, ErrMissingBearerToken + } err = s.client.Do(SetGuildCommandPermissions.Compile(nil, applicationID, guildID, commandID), discord.ApplicationCommandPermissionsSet{Permissions: commandPermissions}, &commandPerms, withBearerToken(bearerToken, opts)...) return } func (s *oAuth2Impl) GetCurrentUserApplicationRoleConnection(bearerToken string, applicationID snowflake.ID, opts ...RequestOpt) (connection *discord.ApplicationRoleConnection, err error) { + if bearerToken == "" { + return nil, ErrMissingBearerToken + } err = s.client.Do(GetCurrentUserApplicationRoleConnection.Compile(nil, applicationID), nil, &connection, withBearerToken(bearerToken, opts)...) return } func (s *oAuth2Impl) UpdateCurrentUserApplicationRoleConnection(bearerToken string, applicationID snowflake.ID, connectionUpdate discord.ApplicationRoleConnectionUpdate, opts ...RequestOpt) (connection *discord.ApplicationRoleConnection, err error) { + if bearerToken == "" { + return nil, ErrMissingBearerToken + } err = s.client.Do(UpdateCurrentUserApplicationRoleConnection.Compile(nil, applicationID), connectionUpdate, &connection, withBearerToken(bearerToken, opts)...) return } diff --git a/rest/rest_endpoints.go b/rest/rest_endpoints.go index e06bd0e5..fb1d689a 100644 --- a/rest/rest_endpoints.go +++ b/rest/rest_endpoints.go @@ -29,7 +29,7 @@ var ( // OAuth2 var ( GetBotApplicationInfo = NewEndpoint(http.MethodGet, "/oauth2/applications/@me") - GetAuthorizationInfo = NewEndpoint(http.MethodGet, "/oauth2/@me") + GetAuthorizationInfo = NewNoBotAuthEndpoint(http.MethodGet, "/oauth2/@me") Token = NewEndpoint(http.MethodPost, "/oauth2/token") ) @@ -37,10 +37,10 @@ var ( var ( GetUser = NewEndpoint(http.MethodGet, "/users/{user.id}") GetCurrentUser = NewEndpoint(http.MethodGet, "/users/@me") - GetCurrentMember = NewEndpoint(http.MethodGet, "/users/@me/guilds/{guild.id}/member") - UpdateSelfUser = NewEndpoint(http.MethodPatch, "/users/@me") + UpdateCurrentUser = NewEndpoint(http.MethodPatch, "/users/@me") + GetCurrentUserGuilds = NewEndpoint(http.MethodGet, "/users/@me/guilds") + GetCurrentMember = NewNoBotAuthEndpoint(http.MethodGet, "/users/@me/guilds/{guild.id}/member") GetCurrentUserConnections = NewNoBotAuthEndpoint(http.MethodGet, "/users/@me/connections") - GetCurrentUserGuilds = NewNoBotAuthEndpoint(http.MethodGet, "/users/@me/guilds") GetCurrentUserApplicationRoleConnection = NewNoBotAuthEndpoint(http.MethodGet, "/users/@me/applications/{application.id}/role-connection") UpdateCurrentUserApplicationRoleConnection = NewNoBotAuthEndpoint(http.MethodPut, "/users/@me/applications/{application.id}/role-connection") LeaveGuild = NewEndpoint(http.MethodDelete, "/users/@me/guilds/{guild.id}") @@ -278,7 +278,7 @@ var ( GetGuildCommandsPermissions = NewEndpoint(http.MethodGet, "/applications/{application.id}/guilds/{guild.id}/commands/permissions") GetGuildCommandPermissions = NewEndpoint(http.MethodGet, "/applications/{application.id}/guilds/{guild.id}/commands/{command.id}/permissions") - SetGuildCommandPermissions = NewEndpoint(http.MethodPut, "/applications/{application.id}/guilds/{guild.id}/commands/{command.id}/permissions") + SetGuildCommandPermissions = NewNoBotAuthEndpoint(http.MethodPut, "/applications/{application.id}/guilds/{guild.id}/commands/{command.id}/permissions") GetInteractionResponse = NewNoBotAuthEndpoint(http.MethodGet, "/webhooks/{application.id}/{interaction.token}/messages/@original") CreateInteractionResponse = NewNoBotAuthEndpoint(http.MethodPost, "/interactions/{interaction.id}/{interaction.token}/callback") diff --git a/rest/users.go b/rest/users.go index ac15783d..18850132 100644 --- a/rest/users.go +++ b/rest/users.go @@ -14,8 +14,7 @@ func NewUsers(client Client) Users { type Users interface { GetUser(userID snowflake.ID, opts ...RequestOpt) (*discord.User, error) - UpdateSelfUser(selfUserUpdate discord.SelfUserUpdate, opts ...RequestOpt) (*discord.OAuth2User, error) - GetGuilds(before int, after int, limit int, withCounts bool, opts ...RequestOpt) ([]discord.OAuth2Guild, error) + UpdateCurrentUser(userUpdate discord.UserUpdate, opts ...RequestOpt) (*discord.OAuth2User, error) LeaveGuild(guildID snowflake.ID, opts ...RequestOpt) error GetDMChannels(opts ...RequestOpt) ([]discord.Channel, error) CreateDMChannel(userID snowflake.ID, opts ...RequestOpt) (*discord.DMChannel, error) @@ -30,25 +29,8 @@ func (s *userImpl) GetUser(userID snowflake.ID, opts ...RequestOpt) (user *disco return } -func (s *userImpl) UpdateSelfUser(updateSelfUser discord.SelfUserUpdate, opts ...RequestOpt) (selfUser *discord.OAuth2User, err error) { - err = s.client.Do(UpdateSelfUser.Compile(nil), updateSelfUser, &selfUser, opts...) - return -} - -func (s *userImpl) GetGuilds(before int, after int, limit int, withCounts bool, opts ...RequestOpt) (guilds []discord.OAuth2Guild, err error) { - queryParams := discord.QueryValues{ - "with_counts": withCounts, - } - if before > 0 { - queryParams["before"] = before - } - if after > 0 { - queryParams["after"] = after - } - if limit > 0 { - queryParams["limit"] = limit - } - err = s.client.Do(GetCurrentUserGuilds.Compile(queryParams), nil, &guilds, opts...) +func (s *userImpl) UpdateCurrentUser(userUpdate discord.UserUpdate, opts ...RequestOpt) (selfUser *discord.OAuth2User, err error) { + err = s.client.Do(UpdateCurrentUser.Compile(nil), userUpdate, &selfUser, opts...) return } From 0162c707df18af369a795b85ae7b95530b605475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?To=CF=80?= Date: Tue, 7 Nov 2023 22:04:39 +0100 Subject: [PATCH 4/4] fix missnamed creator json tag in emoji --- discord/emoji.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/emoji.go b/discord/emoji.go index d91b59fe..94d7fcef 100644 --- a/discord/emoji.go +++ b/discord/emoji.go @@ -15,7 +15,7 @@ type Emoji struct { GuildID snowflake.ID `json:"guild_id,omitempty"` // not present in the API but we need it Name string `json:"name,omitempty"` // may be empty for deleted emojis Roles []snowflake.ID `json:"roles,omitempty"` - Creator *User `json:"creator,omitempty"` + Creator *User `json:"user,omitempty"` RequireColons bool `json:"require_colons,omitempty"` Managed bool `json:"managed,omitempty"` Animated bool `json:"animated,omitempty"`