Skip to content

Commit 814a7e3

Browse files
authored
Add entry point commands + callback updates (#390)
* Add interaction callback response * Add entry point commands + callback updates * add missing newlines * handle entry point command interactions * add LaunchActivity to InteractionEvent * fix bad copypaste * make Handler optional when creating * make Handler a bointer * adjust func so it makes more sense * adjust comment * add newline back :-) * address review * remove 🅱️ointers
1 parent 47dd91b commit 814a7e3

15 files changed

+418
-0
lines changed

discord/application_command.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const (
1414
ApplicationCommandTypeSlash ApplicationCommandType = iota + 1
1515
ApplicationCommandTypeUser
1616
ApplicationCommandTypeMessage
17+
ApplicationCommandTypePrimaryEntryPoint
1718
)
1819

1920
type ApplicationCommand interface {
@@ -69,6 +70,11 @@ func (u *UnmarshalApplicationCommand) UnmarshalJSON(data []byte) error {
6970
err = json.Unmarshal(data, &v)
7071
applicationCommand = v
7172

73+
case ApplicationCommandTypePrimaryEntryPoint:
74+
var v EntryPointCommand
75+
err = json.Unmarshal(data, &v)
76+
applicationCommand = v
77+
7278
default:
7379
err = fmt.Errorf("unknown application command with type %d received", cType.Type)
7480
}
@@ -180,6 +186,7 @@ func (c SlashCommand) NameLocalized() string {
180186
func (c SlashCommand) DefaultMemberPermissions() Permissions {
181187
return c.defaultMemberPermissions
182188
}
189+
183190
func (c SlashCommand) DMPermission() bool {
184191
return c.dmPermission
185192
}
@@ -297,6 +304,7 @@ func (c UserCommand) NameLocalized() string {
297304
func (c UserCommand) DefaultMemberPermissions() Permissions {
298305
return c.defaultMemberPermissions
299306
}
307+
300308
func (c UserCommand) DMPermission() bool {
301309
return c.dmPermission
302310
}
@@ -410,6 +418,7 @@ func (c MessageCommand) NameLocalized() string {
410418
func (c MessageCommand) DefaultMemberPermissions() Permissions {
411419
return c.defaultMemberPermissions
412420
}
421+
413422
func (c MessageCommand) DMPermission() bool {
414423
return c.dmPermission
415424
}
@@ -435,3 +444,127 @@ func (c MessageCommand) CreatedAt() time.Time {
435444
}
436445

437446
func (MessageCommand) applicationCommand() {}
447+
448+
var _ ApplicationCommand = (*EntryPointCommand)(nil)
449+
450+
type EntryPointCommand struct {
451+
id snowflake.ID
452+
applicationID snowflake.ID
453+
guildID *snowflake.ID
454+
name string
455+
nameLocalizations map[Locale]string
456+
nameLocalized string
457+
defaultMemberPermissions Permissions
458+
dmPermission bool
459+
nsfw bool
460+
integrationTypes []ApplicationIntegrationType
461+
contexts []InteractionContextType
462+
version snowflake.ID
463+
Handler EntryPointCommandHandlerType
464+
}
465+
466+
func (c *EntryPointCommand) UnmarshalJSON(data []byte) error {
467+
var v rawEntryPointCommand
468+
if err := json.Unmarshal(data, &v); err != nil {
469+
return err
470+
}
471+
472+
c.id = v.ID
473+
c.applicationID = v.ApplicationID
474+
c.guildID = v.GuildID
475+
c.name = v.Name
476+
c.nameLocalizations = v.NameLocalizations
477+
c.nameLocalized = v.NameLocalized
478+
c.defaultMemberPermissions = v.DefaultMemberPermissions
479+
c.dmPermission = v.DMPermission
480+
c.nsfw = v.NSFW
481+
c.integrationTypes = v.IntegrationTypes
482+
c.contexts = v.Contexts
483+
c.version = v.Version
484+
c.Handler = v.Handler
485+
return nil
486+
}
487+
488+
func (c EntryPointCommand) MarshalJSON() ([]byte, error) {
489+
return json.Marshal(rawEntryPointCommand{
490+
ID: c.id,
491+
Type: c.Type(),
492+
ApplicationID: c.applicationID,
493+
GuildID: c.guildID,
494+
Name: c.name,
495+
NameLocalizations: c.nameLocalizations,
496+
NameLocalized: c.nameLocalized,
497+
DefaultMemberPermissions: c.defaultMemberPermissions,
498+
DMPermission: c.dmPermission,
499+
NSFW: c.nsfw,
500+
IntegrationTypes: c.integrationTypes,
501+
Contexts: c.contexts,
502+
Version: c.version,
503+
Handler: c.Handler,
504+
})
505+
}
506+
507+
func (c EntryPointCommand) ID() snowflake.ID {
508+
return c.id
509+
}
510+
511+
func (EntryPointCommand) Type() ApplicationCommandType {
512+
return ApplicationCommandTypePrimaryEntryPoint
513+
}
514+
515+
func (c EntryPointCommand) ApplicationID() snowflake.ID {
516+
return c.applicationID
517+
}
518+
519+
func (c EntryPointCommand) GuildID() *snowflake.ID {
520+
return c.guildID
521+
}
522+
523+
func (c EntryPointCommand) Name() string {
524+
return c.name
525+
}
526+
527+
func (c EntryPointCommand) NameLocalizations() map[Locale]string {
528+
return c.nameLocalizations
529+
}
530+
531+
func (c EntryPointCommand) NameLocalized() string {
532+
return c.nameLocalized
533+
}
534+
535+
func (c EntryPointCommand) DefaultMemberPermissions() Permissions {
536+
return c.defaultMemberPermissions
537+
}
538+
539+
func (c EntryPointCommand) DMPermission() bool {
540+
return c.dmPermission
541+
}
542+
543+
func (c EntryPointCommand) NSFW() bool {
544+
return c.nsfw
545+
}
546+
547+
func (c EntryPointCommand) IntegrationTypes() []ApplicationIntegrationType {
548+
return c.integrationTypes
549+
}
550+
551+
func (c EntryPointCommand) Contexts() []InteractionContextType {
552+
return c.contexts
553+
}
554+
555+
func (c EntryPointCommand) Version() snowflake.ID {
556+
return c.version
557+
}
558+
559+
func (c EntryPointCommand) CreatedAt() time.Time {
560+
return c.id.Time()
561+
}
562+
563+
func (EntryPointCommand) applicationCommand() {}
564+
565+
type EntryPointCommandHandlerType int
566+
567+
const (
568+
EntryPointCommandHandlerTypeAppHandler EntryPointCommandHandlerType = iota + 1
569+
EntryPointCommandHandlerTypeDiscordLaunchActivity
570+
)

discord/application_command_create.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,36 @@ func (c MessageCommandCreate) CommandName() string {
107107
}
108108

109109
func (MessageCommandCreate) applicationCommandCreate() {}
110+
111+
type EntryPointCommandCreate struct {
112+
Name string `json:"name"`
113+
NameLocalizations map[Locale]string `json:"name_localizations,omitempty"`
114+
DefaultMemberPermissions *json.Nullable[Permissions] `json:"default_member_permissions,omitempty"`
115+
// Deprecated: Use Contexts instead
116+
DMPermission *bool `json:"dm_permission,omitempty"`
117+
IntegrationTypes []ApplicationIntegrationType `json:"integration_types,omitempty"`
118+
Contexts []InteractionContextType `json:"contexts,omitempty"`
119+
NSFW *bool `json:"nsfw,omitempty"`
120+
Handler EntryPointCommandHandlerType `json:"handler,omitempty"`
121+
}
122+
123+
func (c EntryPointCommandCreate) MarshalJSON() ([]byte, error) {
124+
type entryPointCommandCreate EntryPointCommandCreate
125+
return json.Marshal(struct {
126+
Type ApplicationCommandType `json:"type"`
127+
entryPointCommandCreate
128+
}{
129+
Type: c.Type(),
130+
entryPointCommandCreate: entryPointCommandCreate(c),
131+
})
132+
}
133+
134+
func (EntryPointCommandCreate) Type() ApplicationCommandType {
135+
return ApplicationCommandTypePrimaryEntryPoint
136+
}
137+
138+
func (c EntryPointCommandCreate) CommandName() string {
139+
return c.Name
140+
}
141+
142+
func (EntryPointCommandCreate) applicationCommandCreate() {}

discord/application_command_raw.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,20 @@ type rawContextCommand struct {
6262
Contexts []InteractionContextType `json:"contexts"`
6363
Version snowflake.ID `json:"version"`
6464
}
65+
66+
type rawEntryPointCommand struct {
67+
ID snowflake.ID `json:"id"`
68+
Type ApplicationCommandType `json:"type"`
69+
ApplicationID snowflake.ID `json:"application_id"`
70+
GuildID *snowflake.ID `json:"guild_id,omitempty"`
71+
Name string `json:"name"`
72+
NameLocalizations map[Locale]string `json:"name_localizations,omitempty"`
73+
NameLocalized string `json:"name_localized,omitempty"`
74+
DefaultMemberPermissions Permissions `json:"default_member_permissions"`
75+
DMPermission bool `json:"dm_permission"`
76+
NSFW bool `json:"nsfw"`
77+
IntegrationTypes []ApplicationIntegrationType `json:"integration_types"`
78+
Contexts []InteractionContextType `json:"contexts"`
79+
Version snowflake.ID `json:"version"`
80+
Handler EntryPointCommandHandlerType `json:"handler"`
81+
}

discord/application_command_update.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,36 @@ func (c MessageCommandUpdate) CommandName() *string {
107107
}
108108

109109
func (MessageCommandUpdate) applicationCommandUpdate() {}
110+
111+
type EntryPointCommandUpdate struct {
112+
Name *string `json:"name,omitempty"`
113+
NameLocalizations *map[Locale]string `json:"name_localizations,omitempty"`
114+
DefaultMemberPermissions *json.Nullable[Permissions] `json:"default_member_permissions,omitempty"`
115+
// Deprecated: Use Contexts instead
116+
DMPermission *bool `json:"dm_permission,omitempty"`
117+
IntegrationTypes *[]ApplicationIntegrationType `json:"integration_types,omitempty"`
118+
Contexts *[]InteractionContextType `json:"contexts,omitempty"`
119+
NSFW *bool `json:"nsfw,omitempty"`
120+
Handler *EntryPointCommandHandlerType `json:"handler,omitempty"`
121+
}
122+
123+
func (c EntryPointCommandUpdate) MarshalJSON() ([]byte, error) {
124+
type entryPointCommandUpdate EntryPointCommandUpdate
125+
return json.Marshal(struct {
126+
Type ApplicationCommandType `json:"type"`
127+
entryPointCommandUpdate
128+
}{
129+
Type: c.Type(),
130+
entryPointCommandUpdate: entryPointCommandUpdate(c),
131+
})
132+
}
133+
134+
func (EntryPointCommandUpdate) Type() ApplicationCommandType {
135+
return ApplicationCommandTypePrimaryEntryPoint
136+
}
137+
138+
func (c EntryPointCommandUpdate) CommandName() *string {
139+
return c.Name
140+
}
141+
142+
func (EntryPointCommandUpdate) applicationCommandUpdate() {}

discord/interaction_application_command.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ func (i *ApplicationCommandInteraction) UnmarshalJSON(data []byte) error {
5858
v.Resolved.Messages[id] = msg
5959
}
6060
}
61+
case ApplicationCommandTypePrimaryEntryPoint:
62+
v := EntryPointCommandInteractionData{}
63+
err = json.Unmarshal(interaction.Data, &v)
64+
interactionData = v
6165

6266
default:
6367
return fmt.Errorf("unknown application rawInteraction data with type %d received", cType.Type)
@@ -131,6 +135,10 @@ func (i ApplicationCommandInteraction) MessageCommandInteractionData() MessageCo
131135
return i.Data.(MessageCommandInteractionData)
132136
}
133137

138+
func (i ApplicationCommandInteraction) EntryPointCommandInteractionData() EntryPointCommandInteractionData {
139+
return i.Data.(EntryPointCommandInteractionData)
140+
}
141+
134142
func (ApplicationCommandInteraction) interaction() {}
135143

136144
type ApplicationCommandInteractionData interface {
@@ -687,3 +695,54 @@ func (MessageCommandInteractionData) contextCommandInteractionData() {}
687695
type MessageCommandResolved struct {
688696
Messages map[snowflake.ID]Message `json:"messages,omitempty"`
689697
}
698+
699+
var (
700+
_ ApplicationCommandInteractionData = (*EntryPointCommandInteractionData)(nil)
701+
)
702+
703+
type rawEntryPointCommandInteractionData struct {
704+
ID snowflake.ID `json:"id"`
705+
Name string `json:"name"`
706+
Type ApplicationCommandType `json:"type"`
707+
}
708+
709+
type EntryPointCommandInteractionData struct {
710+
id snowflake.ID
711+
name string
712+
}
713+
714+
func (d *EntryPointCommandInteractionData) UnmarshalJSON(data []byte) error {
715+
var iData rawEntryPointCommandInteractionData
716+
if err := json.Unmarshal(data, &iData); err != nil {
717+
return err
718+
}
719+
d.id = iData.ID
720+
d.name = iData.Name
721+
return nil
722+
}
723+
724+
func (d *EntryPointCommandInteractionData) MarshalJSON() ([]byte, error) {
725+
return json.Marshal(rawEntryPointCommandInteractionData{
726+
ID: d.id,
727+
Name: d.name,
728+
Type: d.Type(),
729+
})
730+
}
731+
732+
func (EntryPointCommandInteractionData) Type() ApplicationCommandType {
733+
return ApplicationCommandTypePrimaryEntryPoint
734+
}
735+
736+
func (d EntryPointCommandInteractionData) CommandID() snowflake.ID {
737+
return d.id
738+
}
739+
740+
func (d EntryPointCommandInteractionData) CommandName() string {
741+
return d.name
742+
}
743+
744+
func (d EntryPointCommandInteractionData) GuildID() *snowflake.ID {
745+
return nil
746+
}
747+
748+
func (EntryPointCommandInteractionData) applicationCommandInteractionData() {}

discord/interaction_callback.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package discord
2+
3+
import "github.com/disgoorg/snowflake/v2"
4+
5+
type InteractionCallbackResponse struct {
6+
Interaction InteractionCallback `json:"interaction"`
7+
Resource *InteractionCallbackResource `json:"resource"`
8+
}
9+
10+
type InteractionCallback struct {
11+
ID snowflake.ID `json:"id"`
12+
Type InteractionType `json:"type"`
13+
ActivityInstanceID string `json:"activity_instance_id"`
14+
ResponseMessageID snowflake.ID `json:"response_message_id"`
15+
ResponseMessageLoading bool `json:"response_message_loading"`
16+
ResponseMessageEphemeral bool `json:"response_message_ephemeral"`
17+
}
18+
19+
type InteractionCallbackResource struct {
20+
Type InteractionResponseType `json:"type"`
21+
ActivityInstance *InteractionCallbackActivityInstance `json:"activity_instance"`
22+
Message *Message `json:"message"`
23+
}
24+
25+
type InteractionCallbackActivityInstance struct {
26+
ID string `json:"id"`
27+
}

discord/interaction_response.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const (
1515
InteractionResponseTypeAutocompleteResult
1616
InteractionResponseTypeModal
1717
InteractionResponseTypePremiumRequired
18+
_
19+
InteractionResponseTypeLaunchActivity
1820
)
1921

2022
// InteractionResponse is how you answer interactions. If an answer is not sent within 3 seconds of receiving it, the interaction is failed, and you will be unable to respond to it.

0 commit comments

Comments
 (0)