Skip to content

Commit

Permalink
Merge branch 'master' into selects-component
Browse files Browse the repository at this point in the history
  • Loading branch information
FedorLap2006 committed Jun 18, 2021
2 parents ea6ac6f + adc5b70 commit 9b358e5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
39 changes: 35 additions & 4 deletions components.go
Expand Up @@ -20,21 +20,52 @@ type MessageComponent interface {
Type() ComponentType
}

type UnmarshableMessageComponent struct {
MessageComponent
}

func (umc *UnmarshableMessageComponent) UnmarshalJSON(src []byte) (err error) {
var v struct {
Type ComponentType `json:"type"`
}
err = json.Unmarshal(src, &v)
if err != nil {
return
}

var data MessageComponent
switch v.Type {
case ActionsRowComponent:
v := ActionsRow{}
err = json.Unmarshal(src, &v)
data = v
case ButtonComponent:
v := Button{}
err = json.Unmarshal(src, &v)
data = v
}
if err != nil {
return
}
umc.MessageComponent = data
return
}

// ActionsRow is a container for components within one row.
type ActionsRow struct {
Components []MessageComponent `json:"components"`
}

// MarshalJSON is a method for marshaling ActionsRow to a JSON object.
func (r ActionsRow) MarshalJSON() ([]byte, error) {
type actionRow ActionsRow
type actionsRow ActionsRow

return json.Marshal(struct {
actionRow
actionsRow
Type ComponentType `json:"type"`
}{
actionRow: actionRow(r),
Type: r.Type(),
actionsRow: actionsRow(r),
Type: r.Type(),
})
}

Expand Down
2 changes: 1 addition & 1 deletion message.go
Expand Up @@ -81,7 +81,7 @@ type Message struct {
Attachments []*MessageAttachment `json:"attachments"`

// A list of components attached to the message.
Components []MessageComponent `json:"components"`
Components []UnmarshableMessageComponent `json:"components"`

// A list of embeds present in the message. Multiple
// embeds can currently only be sent by webhooks.
Expand Down

0 comments on commit 9b358e5

Please sign in to comment.