Skip to content
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
17 changes: 16 additions & 1 deletion core/entity_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,24 @@ func (b *entityBuilderImpl) CreateInteraction(interaction discord.Interaction, c

case discord.ModalSubmitInteraction:
baseInteraction := b.baseInteraction(i.BaseInteraction, c, updateCache)

componentsMap := ModalComponentsMap{}

for j := range i.Data.Components {
for k := range i.Data.Components[j].Components() {
component := i.Data.Components[j].Components()[k]
if inputComponent, ok := component.(discord.InputComponent); ok {
componentsMap[inputComponent.ID()] = inputComponent
}
}
}

modalSubmitInteraction := &ModalSubmitInteraction{
CreateInteraction: CreateInteraction{BaseInteraction: baseInteraction},
Data: i.Data,
Data: ModalSubmitInteractionData{
ModalSubmitInteractionData: i.Data,
Components: componentsMap,
},
}

return modalSubmitInteraction
Expand Down
35 changes: 34 additions & 1 deletion core/modal_submit_interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var _ Interaction = (*ModalSubmitInteraction)(nil)

type ModalSubmitInteraction struct {
CreateInteraction
Data discord.ModalSubmitInteractionData
Data ModalSubmitInteractionData
}

func (i ModalSubmitInteraction) interaction() {}
Expand All @@ -26,3 +26,36 @@ func (i ModalSubmitInteraction) UpdateMessage(messageUpdate discord.MessageUpdat
func (i ModalSubmitInteraction) DeferUpdateMessage(opts ...rest.RequestOpt) error {
return i.Respond(discord.InteractionCallbackTypeDeferredUpdateMessage, nil, opts...)
}

type ModalSubmitInteractionData struct {
discord.ModalSubmitInteractionData
Components ModalComponentsMap
}

type ModalComponentsMap map[discord.CustomID]discord.InputComponent

func (m ModalComponentsMap) Get(customID discord.CustomID) discord.InputComponent {
if component, ok := m[customID]; ok {
return component
}
return nil
}

func (m ModalComponentsMap) TextComponent(customID discord.CustomID) *discord.TextInputComponent {
component := m.Get(customID)
if component == nil {
return nil
}
if cmp, ok := component.(discord.TextInputComponent); ok {
return &cmp
}
return nil
}

func (m ModalComponentsMap) Text(customID discord.CustomID) *string {
component := m.TextComponent(customID)
if component == nil {
return nil
}
return &component.Value
}
31 changes: 19 additions & 12 deletions discord/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ type InteractiveComponent interface {
interactiveComponent()
}

type InputComponent interface {
InteractiveComponent
inputComponent()
}

type UnmarshalComponent struct {
Component
}
Expand Down Expand Up @@ -141,12 +146,12 @@ func (c *ActionRowComponent) UnmarshalJSON(data []byte) error {
return nil
}

func (c ActionRowComponent) Type() ComponentType {
func (ActionRowComponent) Type() ComponentType {
return ComponentTypeActionRow
}

func (c ActionRowComponent) component() {}
func (c ActionRowComponent) containerComponent() {}
func (ActionRowComponent) component() {}
func (ActionRowComponent) containerComponent() {}

func (c ActionRowComponent) Components() []InteractiveComponent {
return c
Expand Down Expand Up @@ -276,7 +281,7 @@ func (c ButtonComponent) MarshalJSON() ([]byte, error) {
})
}

func (c ButtonComponent) Type() ComponentType {
func (ButtonComponent) Type() ComponentType {
return ComponentTypeButton
}

Expand All @@ -289,8 +294,8 @@ func (c ButtonComponent) SetID(id CustomID) InteractiveComponent {
return c
}

func (c ButtonComponent) component() {}
func (c ButtonComponent) interactiveComponent() {}
func (ButtonComponent) component() {}
func (ButtonComponent) interactiveComponent() {}

// WithStyle returns a new ButtonComponent with the provided style
func (c ButtonComponent) WithStyle(style ButtonStyle) ButtonComponent {
Expand Down Expand Up @@ -375,7 +380,7 @@ func (c SelectMenuComponent) MarshalJSON() ([]byte, error) {
})
}

func (c SelectMenuComponent) Type() ComponentType {
func (SelectMenuComponent) Type() ComponentType {
return ComponentTypeSelectMenu
}

Expand All @@ -388,8 +393,8 @@ func (c SelectMenuComponent) SetID(id CustomID) InteractiveComponent {
return c
}

func (c SelectMenuComponent) component() {}
func (c SelectMenuComponent) interactiveComponent() {}
func (SelectMenuComponent) component() {}
func (SelectMenuComponent) interactiveComponent() {}

// WithCustomID returns a new SelectMenuComponent with the provided customID
func (c SelectMenuComponent) WithCustomID(customID CustomID) SelectMenuComponent {
Expand Down Expand Up @@ -515,6 +520,7 @@ func (o SelectMenuOption) WithDefault(defaultOption bool) SelectMenuOption {
var (
_ Component = (*TextInputComponent)(nil)
_ InteractiveComponent = (*TextInputComponent)(nil)
_ InputComponent = (*TextInputComponent)(nil)
)

//goland:noinspection GoUnusedExportedFunction
Expand Down Expand Up @@ -558,7 +564,7 @@ func (c TextInputComponent) MarshalJSON() ([]byte, error) {
})
}

func (c TextInputComponent) Type() ComponentType {
func (TextInputComponent) Type() ComponentType {
return ComponentTypeTextInput
}

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

func (c TextInputComponent) component() {}
func (c TextInputComponent) interactiveComponent() {}
func (TextInputComponent) component() {}
func (TextInputComponent) interactiveComponent() {}
func (TextInputComponent) inputComponent() {}

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