diff --git a/components.go b/components.go index cca5e0613..a998dd39b 100644 --- a/components.go +++ b/components.go @@ -20,6 +20,37 @@ 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"` @@ -27,14 +58,14 @@ type ActionsRow struct { // 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(), }) } diff --git a/message.go b/message.go index 25fad0460..8fabdf1f4 100644 --- a/message.go +++ b/message.go @@ -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.