Skip to content

Commit

Permalink
Merge branch 'master' into patch/slog
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Nov 10, 2023
2 parents 1403066 + 0162c70 commit 566a604
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 44 deletions.
2 changes: 1 addition & 1 deletion discord/emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
22 changes: 9 additions & 13 deletions discord/entitlement.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion discord/sku.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ const (
type SKUFlags int

const (
SKUFlagGuildSubscription SKUFlags = 1 << (iota + 7)
SKUFlagAvailable SKUFlags = 1 << (iota + 2)
_
_
_
_
SKUFlagGuildSubscription
SKUFlagUserSubscription
)
4 changes: 2 additions & 2 deletions discord/user.go
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion oauth2/client_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
28 changes: 28 additions & 0 deletions rest/oauth2.go
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 566a604

Please sign in to comment.