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 }