Skip to content

Commit 3fc5bed

Browse files
authored
Merge pull request #124 from DisgoOrg/feature/getter-for-modal-interaction-components
introduce InputComponent interface and CustomID=>InputComponent mapping
2 parents a25742e + 4bf1913 commit 3fc5bed

File tree

3 files changed

+69
-14
lines changed

3 files changed

+69
-14
lines changed

core/entity_builder.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,24 @@ func (b *entityBuilderImpl) CreateInteraction(interaction discord.Interaction, c
300300

301301
case discord.ModalSubmitInteraction:
302302
baseInteraction := b.baseInteraction(i.BaseInteraction, c, updateCache)
303+
304+
componentsMap := ModalComponentsMap{}
305+
306+
for j := range i.Data.Components {
307+
for k := range i.Data.Components[j].Components() {
308+
component := i.Data.Components[j].Components()[k]
309+
if inputComponent, ok := component.(discord.InputComponent); ok {
310+
componentsMap[inputComponent.ID()] = inputComponent
311+
}
312+
}
313+
}
314+
303315
modalSubmitInteraction := &ModalSubmitInteraction{
304316
CreateInteraction: CreateInteraction{BaseInteraction: baseInteraction},
305-
Data: i.Data,
317+
Data: ModalSubmitInteractionData{
318+
ModalSubmitInteractionData: i.Data,
319+
Components: componentsMap,
320+
},
306321
}
307322

308323
return modalSubmitInteraction

core/modal_submit_interaction.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var _ Interaction = (*ModalSubmitInteraction)(nil)
1111

1212
type ModalSubmitInteraction struct {
1313
CreateInteraction
14-
Data discord.ModalSubmitInteractionData
14+
Data ModalSubmitInteractionData
1515
}
1616

1717
func (i ModalSubmitInteraction) interaction() {}
@@ -26,3 +26,36 @@ func (i ModalSubmitInteraction) UpdateMessage(messageUpdate discord.MessageUpdat
2626
func (i ModalSubmitInteraction) DeferUpdateMessage(opts ...rest.RequestOpt) error {
2727
return i.Respond(discord.InteractionCallbackTypeDeferredUpdateMessage, nil, opts...)
2828
}
29+
30+
type ModalSubmitInteractionData struct {
31+
discord.ModalSubmitInteractionData
32+
Components ModalComponentsMap
33+
}
34+
35+
type ModalComponentsMap map[discord.CustomID]discord.InputComponent
36+
37+
func (m ModalComponentsMap) Get(customID discord.CustomID) discord.InputComponent {
38+
if component, ok := m[customID]; ok {
39+
return component
40+
}
41+
return nil
42+
}
43+
44+
func (m ModalComponentsMap) TextComponent(customID discord.CustomID) *discord.TextInputComponent {
45+
component := m.Get(customID)
46+
if component == nil {
47+
return nil
48+
}
49+
if cmp, ok := component.(discord.TextInputComponent); ok {
50+
return &cmp
51+
}
52+
return nil
53+
}
54+
55+
func (m ModalComponentsMap) Text(customID discord.CustomID) *string {
56+
component := m.TextComponent(customID)
57+
if component == nil {
58+
return nil
59+
}
60+
return &component.Value
61+
}

discord/component.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ type InteractiveComponent interface {
4444
interactiveComponent()
4545
}
4646

47+
type InputComponent interface {
48+
InteractiveComponent
49+
inputComponent()
50+
}
51+
4752
type UnmarshalComponent struct {
4853
Component
4954
}
@@ -141,12 +146,12 @@ func (c *ActionRowComponent) UnmarshalJSON(data []byte) error {
141146
return nil
142147
}
143148

144-
func (c ActionRowComponent) Type() ComponentType {
149+
func (ActionRowComponent) Type() ComponentType {
145150
return ComponentTypeActionRow
146151
}
147152

148-
func (c ActionRowComponent) component() {}
149-
func (c ActionRowComponent) containerComponent() {}
153+
func (ActionRowComponent) component() {}
154+
func (ActionRowComponent) containerComponent() {}
150155

151156
func (c ActionRowComponent) Components() []InteractiveComponent {
152157
return c
@@ -276,7 +281,7 @@ func (c ButtonComponent) MarshalJSON() ([]byte, error) {
276281
})
277282
}
278283

279-
func (c ButtonComponent) Type() ComponentType {
284+
func (ButtonComponent) Type() ComponentType {
280285
return ComponentTypeButton
281286
}
282287

@@ -289,8 +294,8 @@ func (c ButtonComponent) SetID(id CustomID) InteractiveComponent {
289294
return c
290295
}
291296

292-
func (c ButtonComponent) component() {}
293-
func (c ButtonComponent) interactiveComponent() {}
297+
func (ButtonComponent) component() {}
298+
func (ButtonComponent) interactiveComponent() {}
294299

295300
// WithStyle returns a new ButtonComponent with the provided style
296301
func (c ButtonComponent) WithStyle(style ButtonStyle) ButtonComponent {
@@ -375,7 +380,7 @@ func (c SelectMenuComponent) MarshalJSON() ([]byte, error) {
375380
})
376381
}
377382

378-
func (c SelectMenuComponent) Type() ComponentType {
383+
func (SelectMenuComponent) Type() ComponentType {
379384
return ComponentTypeSelectMenu
380385
}
381386

@@ -388,8 +393,8 @@ func (c SelectMenuComponent) SetID(id CustomID) InteractiveComponent {
388393
return c
389394
}
390395

391-
func (c SelectMenuComponent) component() {}
392-
func (c SelectMenuComponent) interactiveComponent() {}
396+
func (SelectMenuComponent) component() {}
397+
func (SelectMenuComponent) interactiveComponent() {}
393398

394399
// WithCustomID returns a new SelectMenuComponent with the provided customID
395400
func (c SelectMenuComponent) WithCustomID(customID CustomID) SelectMenuComponent {
@@ -515,6 +520,7 @@ func (o SelectMenuOption) WithDefault(defaultOption bool) SelectMenuOption {
515520
var (
516521
_ Component = (*TextInputComponent)(nil)
517522
_ InteractiveComponent = (*TextInputComponent)(nil)
523+
_ InputComponent = (*TextInputComponent)(nil)
518524
)
519525

520526
//goland:noinspection GoUnusedExportedFunction
@@ -558,7 +564,7 @@ func (c TextInputComponent) MarshalJSON() ([]byte, error) {
558564
})
559565
}
560566

561-
func (c TextInputComponent) Type() ComponentType {
567+
func (TextInputComponent) Type() ComponentType {
562568
return ComponentTypeTextInput
563569
}
564570

@@ -571,8 +577,9 @@ func (c TextInputComponent) SetID(id CustomID) InteractiveComponent {
571577
return c
572578
}
573579

574-
func (c TextInputComponent) component() {}
575-
func (c TextInputComponent) interactiveComponent() {}
580+
func (TextInputComponent) component() {}
581+
func (TextInputComponent) interactiveComponent() {}
582+
func (TextInputComponent) inputComponent() {}
576583

577584
// WithCustomID returns a new SelectMenuComponent with the provided customID
578585
func (c TextInputComponent) WithCustomID(customID CustomID) TextInputComponent {

0 commit comments

Comments
 (0)