Skip to content

Commit

Permalink
Modals fully working
Browse files Browse the repository at this point in the history
  • Loading branch information
szerookii committed Apr 25, 2024
1 parent 1b7c660 commit 43c5060
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
21 changes: 13 additions & 8 deletions goscord/discord/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,27 +323,32 @@ func (ModalSubmitData) Type() InteractionType {
}

func (d *ModalSubmitData) UnmarshalJSON(data []byte) error {
type modalSubmitData ModalSubmitData

var v struct {
modalSubmitData
Components []unmarshalableMessageComponent `json:"components"`
CustomId string `json:"custom_id"`
Components json.RawMessage `json:"components"`
}

err := sonic.Unmarshal(data, &v)
if err != nil {
return err
}

*d = ModalSubmitData(v.modalSubmitData)
d.CustomId = v.CustomId

d.Components = make([]MessageComponent, len(v.Components))
var tmp []unmarshalableMessageComponent

for i, v := range v.Components {
err = sonic.Unmarshal(v.Components, &tmp)
if err != nil {
return err
}

d.Components = make([]MessageComponent, len(tmp))

for i, v := range tmp {
d.Components[i] = v.MessageComponent
}

return err
return nil
}

type ResolvedData struct {
Expand Down
17 changes: 7 additions & 10 deletions goscord/discord/message_components.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package discord
import (
"encoding/json"
"fmt"

"github.com/bytedance/sonic"
)

Expand Down Expand Up @@ -54,18 +55,14 @@ func (u *unmarshalableMessageComponent) UnmarshalJSON(data []byte) error {
switch v.Type {
case ComponentTypeActionRow:
u.MessageComponent = &ActionRows{}

case ComponentTypeButton:
u.MessageComponent = &Button{}

case ComponentTypeSelectMenu:
u.MessageComponent = &SelectMenu{}

case ComponentTypeTextInput:
u.MessageComponent = &TextInput{}

default:
return fmt.Errorf("unknown component type: %d", v.Type)
return fmt.Errorf("unknown component type %d", v.Type)
}

return sonic.Unmarshal(data, u.MessageComponent)
Expand All @@ -88,23 +85,23 @@ func (r ActionRows) MarshalJSON() ([]byte, error) {
})
}

func (r ActionRows) UnmarshalJSON(data []byte) error {
func (r *ActionRows) UnmarshalJSON(data []byte) error {
var v struct {
Components []unmarshalableMessageComponent `json:"components"`
}

err := sonic.Unmarshal(data, &v)

if err != nil {
return err
}

r.Components = make([]MessageComponent, len(v.Components))

for i, v := range v.Components {
r.Components[i] = v.MessageComponent
for i, c := range v.Components {
r.Components[i] = c.MessageComponent
}

return err
return nil
}

type Button struct {
Expand Down

0 comments on commit 43c5060

Please sign in to comment.