Skip to content

Commit 4d28ba8

Browse files
committed
Refactor component interfaces to include GetID and GetCustomID methods
1 parent d19c3d9 commit 4d28ba8

File tree

3 files changed

+210
-47
lines changed

3 files changed

+210
-47
lines changed

discord/component.go

Lines changed: 162 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,32 @@ const (
3232
type Component interface {
3333
json.Marshaler
3434
Type() ComponentType
35+
GetID() int
36+
component()
3537
}
3638

39+
type InteractiveComponent interface {
40+
Component
41+
GetCustomID() string
42+
interactiveComponent()
43+
}
44+
45+
// LayoutComponent is an interface for all components that can be present as a top level component in a [Message].
46+
// [ActionRowComponent]
47+
// [SectionComponent]
48+
// [TextDisplayComponent]
49+
// [MediaGalleryComponent]
50+
// [FileComponent]
51+
// [SeparatorComponent]
52+
// [ContainerComponent]
3753
type LayoutComponent interface {
3854
Component
39-
Components() []Component
40-
containerComponent()
55+
layoutComponent()
4156
}
4257

43-
type InteractiveComponent interface {
58+
type MessageComponent interface {
4459
Component
45-
ComponentCustomID() string
46-
interactiveComponent()
60+
messageComponent()
4761
}
4862

4963
type UnmarshalComponent struct {
@@ -187,6 +201,17 @@ func (ActionRowComponent) Type() ComponentType {
187201
return ComponentTypeActionRow
188202
}
189203

204+
func (c ActionRowComponent) GetID() int {
205+
return c.ID
206+
}
207+
208+
func (c ActionRowComponent) GetComponents() []Component {
209+
return c.Components
210+
}
211+
212+
func (ActionRowComponent) component() {}
213+
func (ActionRowComponent) layoutComponent() {}
214+
190215
// Buttons returns all ButtonComponent(s) in the ActionRowComponent
191216
func (c ActionRowComponent) Buttons() []ButtonComponent {
192217
var buttons []ButtonComponent
@@ -353,6 +378,17 @@ func (ButtonComponent) Type() ComponentType {
353378
return ComponentTypeButton
354379
}
355380

381+
func (c ButtonComponent) GetID() int {
382+
return c.ID
383+
}
384+
385+
func (c ButtonComponent) GetCustomID() string {
386+
return c.CustomID
387+
}
388+
389+
func (ButtonComponent) component() {}
390+
func (ButtonComponent) interactiveComponent() {}
391+
356392
// WithStyle returns a new ButtonComponent with the provided style
357393
func (c ButtonComponent) WithStyle(style ButtonStyle) ButtonComponent {
358394
c.Style = style
@@ -454,6 +490,17 @@ func (TextInputComponent) Type() ComponentType {
454490
return ComponentTypeTextInput
455491
}
456492

493+
func (c TextInputComponent) GetID() int {
494+
return c.ID
495+
}
496+
497+
func (c TextInputComponent) GetCustomID() string {
498+
return c.CustomID
499+
}
500+
501+
func (TextInputComponent) component() {}
502+
func (TextInputComponent) interactiveComponent() {}
503+
457504
// WithCustomID returns a new SelectMenuComponent with the provided customID
458505
func (c TextInputComponent) WithCustomID(customID string) TextInputComponent {
459506
c.CustomID = customID
@@ -509,10 +556,12 @@ type UnfurledMediaItem struct {
509556
}
510557

511558
var (
512-
_ Component = (*SectionComponent)(nil)
559+
_ Component = (*SectionComponent)(nil)
560+
_ LayoutComponent = (*SectionComponent)(nil)
513561
)
514562

515563
type SectionComponent struct {
564+
ID int `json:"id,omitempty"`
516565
Components []Component `json:"components"`
517566
Accessory Component `json:"accessory"`
518567
}
@@ -532,11 +581,23 @@ func (SectionComponent) Type() ComponentType {
532581
return ComponentTypeSection
533582
}
534583

584+
func (c SectionComponent) GetID() int {
585+
return c.ID
586+
}
587+
588+
func (c SectionComponent) GetComponents() []Component {
589+
return c.Components
590+
}
591+
592+
func (SectionComponent) component() {}
593+
func (SectionComponent) layoutComponent() {}
594+
535595
var (
536596
_ Component = (*TextDisplayComponent)(nil)
537597
)
538598

539599
type TextDisplayComponent struct {
600+
ID int `json:"id,omitempty"`
540601
Content string `json:"content"`
541602
}
542603

@@ -555,11 +616,18 @@ func (TextDisplayComponent) Type() ComponentType {
555616
return ComponentTypeTextDisplay
556617
}
557618

619+
func (c TextDisplayComponent) GetID() int {
620+
return c.ID
621+
}
622+
623+
func (TextDisplayComponent) component() {}
624+
558625
var (
559626
_ Component = (*ThumbnailComponent)(nil)
560627
)
561628

562629
type ThumbnailComponent struct {
630+
ID int `json:"id,omitempty"`
563631
Media UnfurledMediaItem `json:"media"`
564632
Description string `json:"description,omitempty"`
565633
Spoiler bool `json:"spoiler,omitempty"`
@@ -580,6 +648,12 @@ func (ThumbnailComponent) Type() ComponentType {
580648
return ComponentTypeThumbnail
581649
}
582650

651+
func (c ThumbnailComponent) GetID() int {
652+
return c.ID
653+
}
654+
655+
func (ThumbnailComponent) component() {}
656+
583657
type MediaGalleryItem struct {
584658
Media UnfurledMediaItem `json:"media"`
585659
Description string `json:"description,omitempty"`
@@ -591,6 +665,7 @@ var (
591665
)
592666

593667
type MediaGalleryComponent struct {
668+
ID int `json:"id,omitempty"`
594669
Items []MediaGalleryItem `json:"items"`
595670
}
596671

@@ -609,6 +684,12 @@ func (MediaGalleryComponent) Type() ComponentType {
609684
return ComponentTypeMediaGallery
610685
}
611686

687+
func (c MediaGalleryComponent) GetID() int {
688+
return c.ID
689+
}
690+
691+
func (MediaGalleryComponent) component() {}
692+
612693
type SeparatorSpacingSize int
613694

614695
const (
@@ -622,6 +703,7 @@ var (
622703
)
623704

624705
type SeparatorComponent struct {
706+
ID int `json:"id,omitempty"`
625707
Divider bool `json:"divider,omitempty"`
626708
Spacing SeparatorSpacingSize `json:"spacing,omitempty"`
627709
}
@@ -641,11 +723,18 @@ func (SeparatorComponent) Type() ComponentType {
641723
return ComponentTypeSeparator
642724
}
643725

726+
func (c SeparatorComponent) GetID() int {
727+
return c.ID
728+
}
729+
730+
func (SeparatorComponent) component() {}
731+
644732
var (
645733
_ Component = (*FileComponent)(nil)
646734
)
647735

648736
type FileComponent struct {
737+
ID int `json:"id,omitempty"`
649738
// File only supports attachment://<filename> references
650739
File UnfurledMediaItem `json:"file"`
651740
Spoiler bool `json:"spoiler,omitempty"`
@@ -666,11 +755,19 @@ func (FileComponent) Type() ComponentType {
666755
return ComponentTypeFile
667756
}
668757

758+
func (c FileComponent) GetID() int {
759+
return c.ID
760+
}
761+
762+
func (FileComponent) component() {}
763+
669764
var (
670-
_ Component = (*ContainerComponent)(nil)
765+
_ Component = (*ContainerComponent)(nil)
766+
_ LayoutComponent = (*ContainerComponent)(nil)
671767
)
672768

673769
type ContainerComponent struct {
770+
ID int `json:"id,omitempty"`
674771
AccentColor *int `json:"accent_color,omitempty"`
675772
Spoiler bool `json:"spoiler,omitempty"`
676773
Components []Component `json:"components"`
@@ -691,23 +788,37 @@ func (ContainerComponent) Type() ComponentType {
691788
return ComponentTypeContainer
692789
}
693790

791+
func (c ContainerComponent) GetID() int {
792+
return c.ID
793+
}
794+
795+
func (c ContainerComponent) GetComponents() []Component {
796+
return c.Components
797+
}
798+
799+
func (ContainerComponent) component() {}
800+
func (ContainerComponent) layoutComponent() {}
801+
694802
var (
695-
_ Component = (*UnknownComponent)(nil)
803+
_ Component = (*UnknownComponent)(nil)
804+
_ InteractiveComponent = (*UnknownComponent)(nil)
805+
_ LayoutComponent = (*UnknownComponent)(nil)
806+
_ SelectMenuComponent = (*UnknownComponent)(nil)
696807
)
697808

698809
type UnknownComponent struct {
699-
type_ ComponentType
700-
id int
701-
Data json.RawMessage
810+
ComponentType ComponentType
811+
ID int
812+
Data json.RawMessage
702813
}
703814

704815
func (c UnknownComponent) MarshalJSON() ([]byte, error) {
705816
data, err := json.Marshal(struct {
706817
Type ComponentType `json:"type"`
707818
ID int `json:"id,omitempty"`
708819
}{
709-
Type: c.type_,
710-
ID: c.id,
820+
Type: c.ComponentType,
821+
ID: c.ID,
711822
})
712823
if err != nil {
713824
return nil, err
@@ -725,12 +836,47 @@ func (c *UnknownComponent) UnmarshalJSON(data []byte) error {
725836
return err
726837
}
727838

728-
c.type_ = unknownComponent.Type
729-
c.id = unknownComponent.ID
839+
c.ComponentType = unknownComponent.Type
840+
c.ID = unknownComponent.ID
730841
c.Data = data
731842
return nil
732843
}
733844

734845
func (c UnknownComponent) Type() ComponentType {
735-
return c.type_
846+
return c.ComponentType
847+
}
848+
849+
func (c UnknownComponent) GetID() int {
850+
return c.ID
851+
}
852+
853+
func (c UnknownComponent) GetCustomID() string {
854+
var data struct {
855+
CustomID string `json:"custom_id"`
856+
}
857+
if err := json.Unmarshal(c.Data, &data); err != nil {
858+
return ""
859+
}
860+
861+
return data.CustomID
736862
}
863+
864+
func (c UnknownComponent) GetComponents() []Component {
865+
var data struct {
866+
Components []UnmarshalComponent `json:"components"`
867+
}
868+
if err := json.Unmarshal(c.Data, &data); err != nil {
869+
return nil
870+
}
871+
872+
var components []Component
873+
for _, component := range data.Components {
874+
components = append(components, component.Component)
875+
}
876+
return components
877+
}
878+
879+
func (UnknownComponent) component() {}
880+
func (UnknownComponent) interactiveComponent() {}
881+
func (UnknownComponent) layoutComponent() {}
882+
func (UnknownComponent) selectMenuComponent() {}

discord/message.go

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,13 @@ func MessageURL(guildID snowflake.ID, channelID snowflake.ID, messageID snowflak
9595

9696
// Message is a struct for messages sent in discord text-based channels
9797
type Message struct {
98-
ID snowflake.ID `json:"id"`
99-
GuildID *snowflake.ID `json:"guild_id"`
100-
Reactions []MessageReaction `json:"reactions"`
101-
Attachments []Attachment `json:"attachments"`
102-
TTS bool `json:"tts"`
103-
Embeds []Embed `json:"embeds,omitempty"`
104-
// Components can be any of:
105-
// ActionRowComponent,
106-
// SectionComponent,
107-
// TextDisplayComponent,
108-
// MediaGalleryComponent,
109-
// SeparatorComponent,
110-
// FileComponent,
111-
// ContainerComponent,
112-
// UnknownComponent
113-
Components []Component `json:"components,omitempty"`
98+
ID snowflake.ID `json:"id"`
99+
GuildID *snowflake.ID `json:"guild_id"`
100+
Reactions []MessageReaction `json:"reactions"`
101+
Attachments []Attachment `json:"attachments"`
102+
TTS bool `json:"tts"`
103+
Embeds []Embed `json:"embeds,omitempty"`
104+
Components []LayoutComponent `json:"components,omitempty"`
114105
CreatedAt time.Time `json:"timestamp"`
115106
Mentions []User `json:"mentions"`
116107
MentionEveryone bool `json:"mention_everyone"`
@@ -570,12 +561,12 @@ type MessageCall struct {
570561
EndedTimestamp *time.Time `json:"ended_timestamp"`
571562
}
572563

573-
func unmarshalComponents(components []UnmarshalComponent) []Component {
574-
containerComponents := make([]Component, len(components))
564+
func unmarshalComponents(components []UnmarshalComponent) []LayoutComponent {
565+
c := make([]LayoutComponent, len(components))
575566
for i := range components {
576-
containerComponents[i] = components[i].Component
567+
c[i] = components[i].Component.(LayoutComponent)
577568
}
578-
return containerComponents
569+
return c
579570
}
580571

581572
// Nonce is a string or int used when sending a message to discord.

0 commit comments

Comments
 (0)