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 support for user apps #341

Merged
merged 11 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion _examples/oauth2/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func handleRoot(w http.ResponseWriter, r *http.Request) {
}

func handleLogin(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, client.GenerateAuthorizationURL(baseURL+"/trylogin", discord.PermissionsNone, 0, false, discord.OAuth2ScopeIdentify, discord.OAuth2ScopeGuilds, discord.OAuth2ScopeEmail, discord.OAuth2ScopeConnections, discord.OAuth2ScopeWebhookIncoming), http.StatusSeeOther)
http.Redirect(w, r, client.GenerateAuthorizationURL(baseURL+"/trylogin", discord.PermissionsNone, 0, false, 0, discord.OAuth2ScopeIdentify, discord.OAuth2ScopeGuilds, discord.OAuth2ScopeEmail, discord.OAuth2ScopeConnections, discord.OAuth2ScopeWebhookIncoming), http.StatusSeeOther)
}

func handleTryLogin(w http.ResponseWriter, r *http.Request) {
Expand Down
2 changes: 1 addition & 1 deletion _examples/verified_roles/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func main() {
}

func handleVerify(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, oAuth2Client.GenerateAuthorizationURL(baseURL+"/callback", discord.PermissionsNone, 0, false, discord.OAuth2ScopeIdentify, discord.OAuth2ScopeRoleConnectionsWrite), http.StatusTemporaryRedirect)
http.Redirect(w, r, oAuth2Client.GenerateAuthorizationURL(baseURL+"/callback", discord.PermissionsNone, 0, false, 0, discord.OAuth2ScopeIdentify, discord.OAuth2ScopeRoleConnectionsWrite), http.StatusTemporaryRedirect)
}

func handleCallback(w http.ResponseWriter, r *http.Request) {
Expand Down
86 changes: 51 additions & 35 deletions discord/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,34 @@ 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"`
Bot *User `json:"bot,omitempty"`
TermsOfServiceURL *string `json:"terms_of_service_url,omitempty"`
PrivacyPolicyURL *string `json:"privacy_policy_url,omitempty"`
CustomInstallURL *string `json:"custom_install_url,omitempty"`
InteractionsEndpointURL *string `json:"interactions_endpoint_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"`
Guild *Guild `json:"guild,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"`
ApproximateGuildCount *int `json:"approximate_guild_count,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"`
Bot *User `json:"bot,omitempty"`
TermsOfServiceURL *string `json:"terms_of_service_url,omitempty"`
PrivacyPolicyURL *string `json:"privacy_policy_url,omitempty"`
CustomInstallURL *string `json:"custom_install_url,omitempty"`
InteractionsEndpointURL *string `json:"interactions_endpoint_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"`
Guild *Guild `json:"guild,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"`
ApproximateGuildCount *int `json:"approximate_guild_count,omitempty"`
IntegrationTypes []ApplicationIntegrationType `json:"integration_types"`
IntegrationTypesConfig ApplicationIntegrationTypesConfig `json:"integration_types_config"`
}

func (a Application) IconURL(opts ...CDNOpt) *string {
Expand All @@ -61,15 +63,16 @@ func (a Application) CreatedAt() time.Time {
}

type ApplicationUpdate struct {
CustomInstallURL *string `json:"custom_install_url,omitempty"`
Description *string `json:"description,omitempty"`
RoleConnectionsVerificationURL *string `json:"role_connections_verification_url,omitempty"`
InstallParams *InstallParams `json:"install_params,omitempty"`
Flags *ApplicationFlags `json:"flags,omitempty"`
Icon *json.Nullable[Icon] `json:"icon,omitempty"`
CoverImage *json.Nullable[Icon] `json:"cover_image,omitempty"`
InteractionsEndpointURL *string `json:"interactions_endpoint_url,omitempty"`
Tags []string `json:"tags,omitempty"`
CustomInstallURL *string `json:"custom_install_url,omitempty"`
Description *string `json:"description,omitempty"`
RoleConnectionsVerificationURL *string `json:"role_connections_verification_url,omitempty"`
InstallParams *InstallParams `json:"install_params,omitempty"`
Flags *ApplicationFlags `json:"flags,omitempty"`
Icon *json.Nullable[Icon] `json:"icon,omitempty"`
CoverImage *json.Nullable[Icon] `json:"cover_image,omitempty"`
InteractionsEndpointURL *string `json:"interactions_endpoint_url,omitempty"`
Tags []string `json:"tags,omitempty"`
IntegrationTypesConfig *ApplicationIntegrationTypesConfig `json:"integration_types_config,omitempty"`
}

type PartialApplication struct {
Expand Down Expand Up @@ -261,3 +264,16 @@ const (
TeamRoleDeveloper TeamRole = "developer"
TeamRoleReadOnly TeamRole = "read_only"
)

type ApplicationIntegrationType int

const (
ApplicationIntegrationTypeGuildInstall ApplicationIntegrationType = iota
ApplicationIntegrationTypeUserInstall
)

type ApplicationIntegrationTypesConfig map[ApplicationIntegrationType]ApplicationIntegrationTypeConfiguration

type ApplicationIntegrationTypeConfiguration struct {
OAuth2InstallParams *InstallParams `json:"oauth2_install_params"`
}
44 changes: 44 additions & 0 deletions discord/application_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type ApplicationCommand interface {
Version() snowflake.ID
CreatedAt() time.Time
NSFW() bool
IntegrationTypes() []ApplicationIntegrationType
Contexts() []InteractionContextType
applicationCommand()
}

Expand Down Expand Up @@ -95,6 +97,8 @@ type SlashCommand struct {
defaultMemberPermissions Permissions
dmPermission bool
nsfw bool
integrationTypes []ApplicationIntegrationType
contexts []InteractionContextType
version snowflake.ID
}

Expand All @@ -117,6 +121,8 @@ func (c *SlashCommand) UnmarshalJSON(data []byte) error {
c.defaultMemberPermissions = v.DefaultMemberPermissions
c.dmPermission = v.DMPermission
c.nsfw = v.NSFW
c.integrationTypes = v.IntegrationTypes
c.contexts = v.Contexts
c.version = v.Version
return nil
}
Expand All @@ -137,6 +143,8 @@ func (c SlashCommand) MarshalJSON() ([]byte, error) {
DefaultMemberPermissions: c.defaultMemberPermissions,
DMPermission: c.dmPermission,
NSFW: c.nsfw,
IntegrationTypes: c.integrationTypes,
Contexts: c.contexts,
Version: c.version,
})
}
Expand Down Expand Up @@ -180,6 +188,14 @@ func (c SlashCommand) NSFW() bool {
return c.nsfw
}

func (c SlashCommand) IntegrationTypes() []ApplicationIntegrationType {
return c.integrationTypes
}

func (c SlashCommand) Contexts() []InteractionContextType {
return c.contexts
}

func (c SlashCommand) Version() snowflake.ID {
return c.version
}
Expand All @@ -206,6 +222,8 @@ type UserCommand struct {
defaultMemberPermissions Permissions
dmPermission bool
nsfw bool
integrationTypes []ApplicationIntegrationType
contexts []InteractionContextType
version snowflake.ID
}

Expand All @@ -224,6 +242,8 @@ func (c *UserCommand) UnmarshalJSON(data []byte) error {
c.defaultMemberPermissions = v.DefaultMemberPermissions
c.dmPermission = v.DMPermission
c.nsfw = v.NSFW
c.integrationTypes = v.IntegrationTypes
c.contexts = v.Contexts
c.version = v.Version
return nil
}
Expand All @@ -240,6 +260,8 @@ func (c UserCommand) MarshalJSON() ([]byte, error) {
DefaultMemberPermissions: c.defaultMemberPermissions,
DMPermission: c.dmPermission,
NSFW: c.nsfw,
IntegrationTypes: c.integrationTypes,
Contexts: c.contexts,
Version: c.version,
})
}
Expand Down Expand Up @@ -283,6 +305,14 @@ func (c UserCommand) NSFW() bool {
return c.nsfw
}

func (c UserCommand) IntegrationTypes() []ApplicationIntegrationType {
return c.integrationTypes
}

func (c UserCommand) Contexts() []InteractionContextType {
return c.contexts
}

func (c UserCommand) Version() snowflake.ID {
return c.version
}
Expand All @@ -305,6 +335,8 @@ type MessageCommand struct {
defaultMemberPermissions Permissions
dmPermission bool
nsfw bool
integrationTypes []ApplicationIntegrationType
contexts []InteractionContextType
version snowflake.ID
}

Expand All @@ -323,6 +355,8 @@ func (c *MessageCommand) UnmarshalJSON(data []byte) error {
c.defaultMemberPermissions = v.DefaultMemberPermissions
c.dmPermission = v.DMPermission
c.nsfw = v.NSFW
c.integrationTypes = v.IntegrationTypes
c.contexts = v.Contexts
c.version = v.Version
return nil
}
Expand All @@ -339,6 +373,8 @@ func (c MessageCommand) MarshalJSON() ([]byte, error) {
DefaultMemberPermissions: c.defaultMemberPermissions,
DMPermission: c.dmPermission,
NSFW: c.nsfw,
IntegrationTypes: c.integrationTypes,
Contexts: c.contexts,
Version: c.version,
})
}
Expand Down Expand Up @@ -382,6 +418,14 @@ func (c MessageCommand) NSFW() bool {
return c.nsfw
}

func (c MessageCommand) IntegrationTypes() []ApplicationIntegrationType {
return c.integrationTypes
}

func (c MessageCommand) Contexts() []InteractionContextType {
return c.contexts
}

func (c MessageCommand) Version() snowflake.ID {
return c.version
}
Expand Down
21 changes: 15 additions & 6 deletions discord/application_command_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ type SlashCommandCreate struct {
DescriptionLocalizations map[Locale]string `json:"description_localizations,omitempty"`
Options []ApplicationCommandOption `json:"options,omitempty"`
DefaultMemberPermissions *json.Nullable[Permissions] `json:"default_member_permissions,omitempty"` // different behavior for 0 and null, optional
DMPermission *bool `json:"dm_permission,omitempty"`
NSFW *bool `json:"nsfw,omitempty"`
// Deprecated: Use Contexts instead
DMPermission *bool `json:"dm_permission,omitempty"`
IntegrationTypes []ApplicationIntegrationType `json:"integration_types,omitempty"`
Contexts []InteractionContextType `json:"contexts,omitempty"`
NSFW *bool `json:"nsfw,omitempty"`
}

func (c SlashCommandCreate) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -45,8 +48,11 @@ type UserCommandCreate struct {
Name string `json:"name"`
NameLocalizations map[Locale]string `json:"name_localizations,omitempty"`
DefaultMemberPermissions *json.Nullable[Permissions] `json:"default_member_permissions,omitempty"`
DMPermission *bool `json:"dm_permission,omitempty"`
NSFW *bool `json:"nsfw,omitempty"`
// Deprecated: Use Contexts instead
DMPermission *bool `json:"dm_permission,omitempty"`
IntegrationTypes []ApplicationIntegrationType `json:"integration_types,omitempty"`
Contexts []InteractionContextType `json:"contexts,omitempty"`
NSFW *bool `json:"nsfw,omitempty"`
}

func (c UserCommandCreate) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -74,8 +80,11 @@ type MessageCommandCreate struct {
Name string `json:"name"`
NameLocalizations map[Locale]string `json:"name_localizations,omitempty"`
DefaultMemberPermissions *json.Nullable[Permissions] `json:"default_member_permissions,omitempty"`
DMPermission *bool `json:"dm_permission,omitempty"`
NSFW *bool `json:"nsfw,omitempty"`
// Deprecated: Use Contexts instead
DMPermission *bool `json:"dm_permission,omitempty"`
IntegrationTypes []ApplicationIntegrationType `json:"integration_types,omitempty"`
Contexts []InteractionContextType `json:"contexts,omitempty"`
NSFW *bool `json:"nsfw,omitempty"`
}

func (c MessageCommandCreate) MarshalJSON() ([]byte, error) {
Expand Down
56 changes: 30 additions & 26 deletions discord/application_command_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ import (
)

type rawSlashCommand struct {
ID snowflake.ID `json:"id"`
Type ApplicationCommandType `json:"type"`
ApplicationID snowflake.ID `json:"application_id"`
GuildID *snowflake.ID `json:"guild_id,omitempty"`
Name string `json:"name"`
NameLocalizations map[Locale]string `json:"name_localizations,omitempty"`
NameLocalized string `json:"name_localized,omitempty"`
Description string `json:"description,omitempty"`
DescriptionLocalizations map[Locale]string `json:"description_localizations,omitempty"`
DescriptionLocalized string `json:"description_localized,omitempty"`
Options []ApplicationCommandOption `json:"options,omitempty"`
DefaultMemberPermissions Permissions `json:"default_member_permissions"`
DMPermission bool `json:"dm_permission"`
NSFW bool `json:"nsfw"`
Version snowflake.ID `json:"version"`
ID snowflake.ID `json:"id"`
Type ApplicationCommandType `json:"type"`
ApplicationID snowflake.ID `json:"application_id"`
GuildID *snowflake.ID `json:"guild_id,omitempty"`
Name string `json:"name"`
NameLocalizations map[Locale]string `json:"name_localizations,omitempty"`
NameLocalized string `json:"name_localized,omitempty"`
Description string `json:"description,omitempty"`
DescriptionLocalizations map[Locale]string `json:"description_localizations,omitempty"`
DescriptionLocalized string `json:"description_localized,omitempty"`
Options []ApplicationCommandOption `json:"options,omitempty"`
DefaultMemberPermissions Permissions `json:"default_member_permissions"`
DMPermission bool `json:"dm_permission"`
NSFW bool `json:"nsfw"`
IntegrationTypes []ApplicationIntegrationType `json:"integration_types"`
Contexts []InteractionContextType `json:"contexts"`
Version snowflake.ID `json:"version"`
}

func (c *rawSlashCommand) UnmarshalJSON(data []byte) error {
Expand All @@ -46,15 +48,17 @@ func (c *rawSlashCommand) UnmarshalJSON(data []byte) error {
}

type rawContextCommand struct {
ID snowflake.ID `json:"id"`
Type ApplicationCommandType `json:"type"`
ApplicationID snowflake.ID `json:"application_id"`
GuildID *snowflake.ID `json:"guild_id,omitempty"`
Name string `json:"name"`
NameLocalizations map[Locale]string `json:"name_localizations,omitempty"`
NameLocalized string `json:"name_localized,omitempty"`
DefaultMemberPermissions Permissions `json:"default_member_permissions"`
DMPermission bool `json:"dm_permission"`
NSFW bool `json:"nsfw"`
Version snowflake.ID `json:"version"`
ID snowflake.ID `json:"id"`
Type ApplicationCommandType `json:"type"`
ApplicationID snowflake.ID `json:"application_id"`
GuildID *snowflake.ID `json:"guild_id,omitempty"`
Name string `json:"name"`
NameLocalizations map[Locale]string `json:"name_localizations,omitempty"`
NameLocalized string `json:"name_localized,omitempty"`
DefaultMemberPermissions Permissions `json:"default_member_permissions"`
DMPermission bool `json:"dm_permission"`
NSFW bool `json:"nsfw"`
IntegrationTypes []ApplicationIntegrationType `json:"integration_types"`
Contexts []InteractionContextType `json:"contexts"`
Version snowflake.ID `json:"version"`
}
Loading
Loading