Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add role connections #219

Merged
merged 6 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 30 additions & 28 deletions discord/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,28 @@ import (
)

type Application struct {
ID snowflake.ID `json:"id"`
Name string `json:"name"`
Icon *string `json:"icon,omitempty"`
Description string `json:"description"`
RPCOrigins []string `json:"rpc_origins"`
BotPublic bool `json:"bot_public"`
BotRequireCodeGrant bool `json:"bot_require_code_grant"`
TermsOfServiceURL *string `json:"terms_of_service_url,omitempty"`
PrivacyPolicyURL *string `json:"privacy_policy_url,omitempty"`
CustomInstallURL *string `json:"custom_install_url,omitempty"`
InstallParams *InstallParams `json:"install_params"`
Tags []string `json:"tags"`
Owner *User `json:"owner,omitempty"`
Summary string `json:"summary"`
VerifyKey string `json:"verify_key"`
Team *Team `json:"team,omitempty"`
GuildID *snowflake.ID `json:"guild_id,omitempty"`
PrimarySkuID *snowflake.ID `json:"primary_sku_id,omitempty"`
Slug *string `json:"slug,omitempty"`
CoverImage *string `json:"cover_image,omitempty"`
Flags ApplicationFlags `json:"flags,omitempty"`
ID snowflake.ID `json:"id"`
Name string `json:"name"`
Icon *string `json:"icon,omitempty"`
Description string `json:"description"`
RPCOrigins []string `json:"rpc_origins"`
BotPublic bool `json:"bot_public"`
BotRequireCodeGrant bool `json:"bot_require_code_grant"`
TermsOfServiceURL *string `json:"terms_of_service_url,omitempty"`
PrivacyPolicyURL *string `json:"privacy_policy_url,omitempty"`
CustomInstallURL *string `json:"custom_install_url,omitempty"`
RoleConnectionsVerificationURL *string `json:"role_connections_verification_url"`
InstallParams *InstallParams `json:"install_params"`
Tags []string `json:"tags"`
Owner *User `json:"owner,omitempty"`
Summary string `json:"summary"`
VerifyKey string `json:"verify_key"`
Team *Team `json:"team,omitempty"`
GuildID *snowflake.ID `json:"guild_id,omitempty"`
PrimarySkuID *snowflake.ID `json:"primary_sku_id,omitempty"`
Slug *string `json:"slug,omitempty"`
CoverImage *string `json:"cover_image,omitempty"`
Flags ApplicationFlags `json:"flags,omitempty"`
}

func (a Application) IconURL(opts ...CDNOpt) *string {
Expand Down Expand Up @@ -100,13 +101,14 @@ const (
OAuth2ScopeGuildsMembersRead OAuth2Scope = "guilds.members.read"
OAuth2ScopeGDMJoin OAuth2Scope = "gdm.join"

OAuth2ScopeRelationshipsRead OAuth2Scope = "relationships.read"
OAuth2ScopeIdentify OAuth2Scope = "identify"
OAuth2ScopeEmail OAuth2Scope = "email"
OAuth2ScopeConnections OAuth2Scope = "connections"
OAuth2ScopeBot OAuth2Scope = "bot"
OAuth2ScopeMessagesRead OAuth2Scope = "messages.read"
OAuth2ScopeWebhookIncoming OAuth2Scope = "webhook.incoming"
OAuth2ScopeRelationshipsRead OAuth2Scope = "relationships.read"
OAuth2ScopeRoleConnectionsWrite OAuth2Scope = "role_connections.write"
OAuth2ScopeIdentify OAuth2Scope = "identify"
OAuth2ScopeEmail OAuth2Scope = "email"
OAuth2ScopeConnections OAuth2Scope = "connections"
OAuth2ScopeBot OAuth2Scope = "bot"
OAuth2ScopeMessagesRead OAuth2Scope = "messages.read"
OAuth2ScopeWebhookIncoming OAuth2Scope = "webhook.incoming"
)

func (s OAuth2Scope) String() string {
Expand Down
23 changes: 23 additions & 0 deletions discord/application_role_connection_metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package discord

type ApplicationRoleConnectionMetadata struct {
Type ApplicationRoleConnectionMetadataType `json:"type"`
Key string `json:"key"`
Name string `json:"name"`
NameLocalizations map[Locale]string `json:"name_localizations,omitempty"`
Description string `json:"description"`
DescriptionLocalizations map[Locale]string `json:"description_localizations,omitempty"`
}

type ApplicationRoleConnectionMetadataType int

const (
IntegerLessThanOrEqual ApplicationRoleConnectionMetadataType = iota + 1
sebm253 marked this conversation as resolved.
Show resolved Hide resolved
IntegerGreaterThanOrEqual
IntegerEqual
IntegerNotEqual
DateTimeLessThanOrEqual
DateTimeGreaterThanOrEqual
BooleanEqual
BooleanNotEqual
)
12 changes: 12 additions & 0 deletions discord/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,15 @@ type SelfUserUpdate struct {
Username string `json:"username,omitempty"`
Avatar *json.Nullable[Icon] `json:"avatar,omitempty"`
}

type ApplicationRoleConnection struct {
PlatformName *string `json:"platform_name"`
PlatformUsername *string `json:"platform_username"`
Metadata ApplicationRoleConnectionMetadata `json:"metadata"`
}

type ApplicationRoleConnectionUpdate struct {
PlatformName *string `json:"platform_name,omitempty"`
PlatformUsername *string `json:"platform_username,omitempty"`
Metadata *ApplicationRoleConnectionMetadata `json:"metadata,omitempty"`
}
13 changes: 13 additions & 0 deletions rest/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ type Applications interface {

GetGuildCommandsPermissions(applicationID snowflake.ID, guildID snowflake.ID, opts ...RequestOpt) ([]discord.ApplicationCommandPermissions, error)
GetGuildCommandPermissions(applicationID snowflake.ID, guildID snowflake.ID, commandID snowflake.ID, opts ...RequestOpt) (*discord.ApplicationCommandPermissions, error)

GetApplicationRoleConnectionMetadataRecords(applicationID snowflake.ID, opts ...RequestOpt) ([]discord.ApplicationRoleConnectionMetadata, error)
sebm253 marked this conversation as resolved.
Show resolved Hide resolved
UpdateApplicationRoleConnectionMetadataRecords(applicationID snowflake.ID, newRecords []discord.ApplicationRoleConnectionMetadata, opts ...RequestOpt) ([]discord.ApplicationRoleConnectionMetadata, error)
}

type applicationsImpl struct {
Expand Down Expand Up @@ -142,6 +145,16 @@ func (s *applicationsImpl) GetGuildCommandPermissions(applicationID snowflake.ID
return
}

func (s *applicationsImpl) GetApplicationRoleConnectionMetadataRecords(applicationID snowflake.ID, opts ...RequestOpt) (metadata []discord.ApplicationRoleConnectionMetadata, err error) {
err = s.client.Do(GetApplicationRoleConnectionMetadataRecords.Compile(nil, applicationID), nil, &metadata, opts...)
return
}

func (s *applicationsImpl) UpdateApplicationRoleConnectionMetadataRecords(applicationID snowflake.ID, newRecords []discord.ApplicationRoleConnectionMetadata, opts ...RequestOpt) (metadata []discord.ApplicationRoleConnectionMetadata, err error) {
err = s.client.Do(UpdateApplicationRoleConnectionMetadataRecords.Compile(nil, applicationID), newRecords, &metadata, opts...)
return
}

func unmarshalApplicationCommandsToApplicationCommands(unmarshalCommands []discord.UnmarshalApplicationCommand) []discord.ApplicationCommand {
commands := make([]discord.ApplicationCommand, len(unmarshalCommands))
for i := range unmarshalCommands {
Expand Down
13 changes: 13 additions & 0 deletions rest/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ type OAuth2 interface {

SetGuildCommandPermissions(bearerToken string, applicationID snowflake.ID, guildID snowflake.ID, commandID snowflake.ID, commandPermissions []discord.ApplicationCommandPermission, opts ...RequestOpt) (*discord.ApplicationCommandPermissions, error)

GetCurrentUserApplicationRoleConnection(applicationID snowflake.ID, opts ...RequestOpt) (discord.ApplicationRoleConnection, error)
UpdateCurrentUserApplicationRoleConnection(applicationID snowflake.ID, connectionUpdate discord.ApplicationRoleConnectionUpdate, opts ...RequestOpt) (discord.ApplicationRoleConnection, error)

GetAccessToken(clientID snowflake.ID, clientSecret string, code string, redirectURI string, opts ...RequestOpt) (*discord.AccessTokenResponse, error)
RefreshAccessToken(clientID snowflake.ID, clientSecret string, refreshToken string, opts ...RequestOpt) (*discord.AccessTokenResponse, error)
}
Expand Down Expand Up @@ -97,6 +100,16 @@ func (s *oAuth2Impl) SetGuildCommandPermissions(bearerToken string, applicationI
return
}

func (s *oAuth2Impl) GetCurrentUserApplicationRoleConnection(applicationID snowflake.ID, opts ...RequestOpt) (connection discord.ApplicationRoleConnection, err error) {
err = s.client.Do(GetCurrentUserApplicationRoleConnection.Compile(nil, applicationID), nil, &connection, opts...)
return
}

func (s *oAuth2Impl) UpdateCurrentUserApplicationRoleConnection(applicationID snowflake.ID, connectionUpdate discord.ApplicationRoleConnectionUpdate, opts ...RequestOpt) (connection discord.ApplicationRoleConnection, err error) {
err = s.client.Do(UpdateCurrentUserApplicationRoleConnection.Compile(nil, applicationID), connectionUpdate, &connection, opts...)
return
}

func (s *oAuth2Impl) exchangeAccessToken(clientID snowflake.ID, clientSecret string, grantType discord.GrantType, codeOrRefreshToken string, redirectURI string, opts ...RequestOpt) (exchange *discord.AccessTokenResponse, err error) {
values := url.Values{
"client_id": []string{clientID.String()},
Expand Down
25 changes: 15 additions & 10 deletions rest/rest_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ var (

// 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")
GetCurrentUserConnections = NewNoBotAuthEndpoint(http.MethodGet, "/users/@me/connections")
GetCurrentUserGuilds = NewNoBotAuthEndpoint(http.MethodGet, "/users/@me/guilds")
LeaveGuild = NewEndpoint(http.MethodDelete, "/users/@me/guilds/{guild.id}")
GetDMChannels = NewEndpoint(http.MethodGet, "/users/@me/channels")
CreateDMChannel = NewEndpoint(http.MethodPost, "/users/@me/channels")
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")
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}")
GetDMChannels = NewEndpoint(http.MethodGet, "/users/@me/channels")
CreateDMChannel = NewEndpoint(http.MethodPost, "/users/@me/channels")
)

// Guilds
Expand Down Expand Up @@ -249,7 +251,7 @@ var (
GetChannelInvites = NewEndpoint(http.MethodGet, "/channels/{channel.id}/invites")
)

// Interactions
// Applications
var (
GetGlobalCommands = NewEndpoint(http.MethodGet, "/applications/{application.id}/commands")
GetGlobalCommand = NewEndpoint(http.MethodGet, "/applications/{application.id}/command/{command.id}")
Expand Down Expand Up @@ -278,6 +280,9 @@ var (
CreateFollowupMessage = NewNoBotAuthEndpoint(http.MethodPost, "/webhooks/{application.id}/{interaction.token}")
UpdateFollowupMessage = NewNoBotAuthEndpoint(http.MethodPatch, "/webhooks/{application.id}/{interaction.token}/messages/{message.id}")
DeleteFollowupMessage = NewNoBotAuthEndpoint(http.MethodDelete, "/webhooks/{application.id}/{interaction.token}/messages/{message.id}")

GetApplicationRoleConnectionMetadataRecords = NewEndpoint(http.MethodGet, "/applications/{application.id}/role-connections/metadata")
UpdateApplicationRoleConnectionMetadataRecords = NewEndpoint(http.MethodPut, "/applications/{application.id}/role-connections/metadata")
)

// NewEndpoint returns a new Endpoint which requires bot auth with the given http method & route.
Expand Down