forked from pothtonswer/discordmuffin
Merge branch 'componentsv2' of https://github.com/bwmarrin/discordgo into feature/builder
This commit is contained in:
commit
83a8bfa438
1 changed files with 52 additions and 64 deletions
116
components.go
116
components.go
|
@ -24,7 +24,6 @@ const (
|
|||
MediaGalleryComponent ComponentType = 12
|
||||
FileComponentType ComponentType = 13
|
||||
SeparatorComponent ComponentType = 14
|
||||
ContentInventoryEntryComponent ComponentType = 16
|
||||
ContainerComponent ComponentType = 17
|
||||
)
|
||||
|
||||
|
@ -112,13 +111,17 @@ func (r ActionsRow) MarshalJSON() ([]byte, error) {
|
|||
|
||||
// UnmarshalJSON is a helper function to unmarshal Actions Row.
|
||||
func (r *ActionsRow) UnmarshalJSON(data []byte) error {
|
||||
type actionsRow ActionsRow
|
||||
var v struct {
|
||||
actionsRow
|
||||
RawComponents []unmarshalableMessageComponent `json:"components"`
|
||||
}
|
||||
err := json.Unmarshal(data, &v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*r = ActionsRow(v.actionsRow)
|
||||
|
||||
r.Components = make([]MessageComponent, len(v.RawComponents))
|
||||
for i, v := range v.RawComponents {
|
||||
r.Components[i] = v.MessageComponent
|
||||
|
@ -336,6 +339,31 @@ type Section struct {
|
|||
Accessory MessageComponent `json:"accessory"`
|
||||
}
|
||||
|
||||
// UnmarshalJSON is a method for unmarshaling Section from JSON
|
||||
func (s *Section) UnmarshalJSON(data []byte) error {
|
||||
type section Section
|
||||
|
||||
var v struct {
|
||||
section
|
||||
RawComponents []unmarshalableMessageComponent `json:"components"`
|
||||
RawAccessory unmarshalableMessageComponent `json:"accessory"`
|
||||
}
|
||||
|
||||
err := json.Unmarshal(data, &v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*s = Section(v.section)
|
||||
s.Accessory = v.RawAccessory.MessageComponent
|
||||
s.Components = make([]MessageComponent, len(v.RawComponents))
|
||||
for i, v := range v.RawComponents {
|
||||
s.Components[i] = v.MessageComponent
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Type is a method to get the type of a component.
|
||||
func (Section) Type() ComponentType {
|
||||
return SectionComponent
|
||||
|
@ -354,42 +382,6 @@ func (s Section) MarshalJSON() ([]byte, error) {
|
|||
})
|
||||
}
|
||||
|
||||
// UnmarshalJSON is a method for unmarshaling Section from JSON.
|
||||
// origin https://github.com/bwmarrin/discordgo/pull/1616
|
||||
func (s *Section) UnmarshalJSON(data []byte) error {
|
||||
type sectionAlias Section
|
||||
aux := struct {
|
||||
sectionAlias
|
||||
RawComponents []json.RawMessage `json:"components"`
|
||||
RawAccessory json.RawMessage `json:"accessory"`
|
||||
}{}
|
||||
|
||||
if err := json.Unmarshal(data, &aux); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*s = Section(aux.sectionAlias)
|
||||
|
||||
s.Components = make([]MessageComponent, len(aux.RawComponents))
|
||||
for i, raw := range aux.RawComponents {
|
||||
component, err := MessageComponentFromJSON(raw)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to unmarshal section component %d: %w", i, err)
|
||||
}
|
||||
s.Components[i] = component
|
||||
}
|
||||
|
||||
if len(aux.RawAccessory) > 0 && string(aux.RawAccessory) != "null" {
|
||||
accessory, err := MessageComponentFromJSON(aux.RawAccessory)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to unmarshal accessory: %w", err)
|
||||
}
|
||||
s.Accessory = accessory
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// TextDisplay is a top-level component that allows you to add markdown-formatted text to the message.
|
||||
type TextDisplay struct {
|
||||
Content string `json:"content"`
|
||||
|
@ -550,6 +542,29 @@ func (Container) Type() ComponentType {
|
|||
return ContainerComponent
|
||||
}
|
||||
|
||||
// UnmarshalJSON is a method for unmarshaling Container from JSON
|
||||
func (c *Container) UnmarshalJSON(data []byte) error {
|
||||
type container Container
|
||||
|
||||
var v struct {
|
||||
container
|
||||
RawComponents []unmarshalableMessageComponent `json:"components"`
|
||||
}
|
||||
|
||||
err := json.Unmarshal(data, &v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*c = Container(v.container)
|
||||
c.Components = make([]MessageComponent, len(v.RawComponents))
|
||||
for i, v := range v.RawComponents {
|
||||
c.Components[i] = v.MessageComponent
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON is a method for marshaling Container to a JSON object.
|
||||
func (c Container) MarshalJSON() ([]byte, error) {
|
||||
type container Container
|
||||
|
@ -563,33 +578,6 @@ func (c Container) MarshalJSON() ([]byte, error) {
|
|||
})
|
||||
}
|
||||
|
||||
// UnmarshalJSON is a method for unmarshaling Container from JSON.
|
||||
// origin https://github.com/bwmarrin/discordgo/pull/1616
|
||||
func (c *Container) UnmarshalJSON(data []byte) error {
|
||||
type containerAlias Container
|
||||
aux := struct {
|
||||
containerAlias
|
||||
RawComponents []json.RawMessage `json:"components"`
|
||||
}{}
|
||||
|
||||
if err := json.Unmarshal(data, &aux); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*c = Container(aux.containerAlias)
|
||||
|
||||
c.Components = make([]MessageComponent, len(aux.RawComponents))
|
||||
for i, raw := range aux.RawComponents {
|
||||
component, err := MessageComponentFromJSON(raw)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse component at index %d: %w", i, err)
|
||||
}
|
||||
c.Components[i] = component
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnfurledMediaItem represents an unfurled media item.
|
||||
type UnfurledMediaItem struct {
|
||||
URL string `json:"url"`
|
||||
|
|
Loading…
Reference in a new issue