Skip to content

Commit

Permalink
fix problem with oauth2 endpoints (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Nov 1, 2023
1 parent c50b92f commit 631c261
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 28 deletions.
4 changes: 2 additions & 2 deletions discord/user.go
Expand Up @@ -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"`
}
Expand Down
28 changes: 28 additions & 0 deletions rest/oauth2.go
@@ -1,13 +1,17 @@
package rest

import (
"errors"
"net/url"

"github.com/disgoorg/snowflake/v2"

"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 {
Expand All @@ -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)

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions rest/rest_endpoints.go
Expand Up @@ -29,18 +29,18 @@ 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")
)

// Users
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}")
Expand Down Expand Up @@ -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")
Expand Down
24 changes: 3 additions & 21 deletions rest/users.go
Expand Up @@ -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)
Expand All @@ -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
}

Expand Down

0 comments on commit 631c261

Please sign in to comment.