빌더들 구현 #4
2 changed files with 301 additions and 95 deletions
280
builder.go
280
builder.go
|
@ -2,77 +2,295 @@ package discordgo
|
|||
|
||||
import "time"
|
||||
|
||||
// MessageEmbedBuilder for make MessageEmbed struct.
|
||||
type MessageEmbedBuilder struct {
|
||||
*MessageEmbed
|
||||
}
|
||||
|
||||
// NewMessageEmbedBuilder creates a new MessageEmbedBuilder struct.
|
||||
func NewMessageEmbedBuilder() *MessageEmbedBuilder {
|
||||
return &MessageEmbedBuilder{}
|
||||
// MessageEmbedBuilder creates a new MessageEmbed struct.
|
||||
func MessageEmbedBuilder() *MessageEmbed {
|
||||
return &MessageEmbed{}
|
||||
}
|
||||
|
||||
// SetTitle sets title for MessageEmbed.
|
||||
func (b *MessageEmbedBuilder) SetTitle(title string) *MessageEmbedBuilder {
|
||||
b.MessageEmbed.Title = title
|
||||
func (b *MessageEmbed) SetTitle(title string) *MessageEmbed {
|
||||
b.Title = title
|
||||
return b
|
||||
}
|
||||
|
||||
// SetDescription sets description for MessageEmbed.
|
||||
func (b *MessageEmbedBuilder) SetDescription(desc string) *MessageEmbedBuilder {
|
||||
b.MessageEmbed.Description = desc
|
||||
func (b *MessageEmbed) SetDescription(desc string) *MessageEmbed {
|
||||
b.Description = desc
|
||||
return b
|
||||
}
|
||||
|
||||
// SetColor sets color for MessageEmbed.
|
||||
func (b *MessageEmbedBuilder) SetColor(color int) *MessageEmbedBuilder {
|
||||
b.MessageEmbed.Color = color
|
||||
func (b *MessageEmbed) SetColor(color int) *MessageEmbed {
|
||||
b.Color = color
|
||||
return b
|
||||
}
|
||||
|
||||
// SetThumbnail sets thumbnail for MessageEmbed.
|
||||
func (b *MessageEmbedBuilder) SetThumbnail(url string) *MessageEmbedBuilder {
|
||||
b.MessageEmbed.Thumbnail = &MessageEmbedThumbnail{URL: url}
|
||||
func (b *MessageEmbed) SetThumbnail(url string) *MessageEmbed {
|
||||
b.Thumbnail = &MessageEmbedThumbnail{URL: url}
|
||||
return b
|
||||
}
|
||||
|
||||
// SetImage sets image for MessageEmbed.
|
||||
func (b *MessageEmbedBuilder) SetImage(url string) *MessageEmbedBuilder {
|
||||
b.MessageEmbed.Image = &MessageEmbedImage{URL: url}
|
||||
func (b *MessageEmbed) SetImage(url string) *MessageEmbed {
|
||||
b.Image = &MessageEmbedImage{URL: url}
|
||||
return b
|
||||
}
|
||||
|
||||
// SetTimestamp sets timestamp for MessageEmbed.
|
||||
func (b *MessageEmbedBuilder) SetTimestamp(times time.Time) *MessageEmbedBuilder {
|
||||
b.MessageEmbed.Timestamp = times.Format(time.RFC3339)
|
||||
func (b *MessageEmbed) SetTimestamp(times time.Time) *MessageEmbed {
|
||||
b.Timestamp = times.Format(time.RFC3339)
|
||||
return b
|
||||
}
|
||||
|
||||
// SetFooter sets footer for MessageEmbed.
|
||||
func (b *MessageEmbedBuilder) SetFooter(footer MessageEmbedFooter) *MessageEmbedBuilder {
|
||||
b.MessageEmbed.Footer = &footer
|
||||
func (b *MessageEmbed) SetFooter(footer MessageEmbedFooter) *MessageEmbed {
|
||||
b.Footer = &footer
|
||||
return b
|
||||
}
|
||||
|
||||
// AddFields adds fields for MessageEmbed.
|
||||
func (b *MessageEmbedBuilder) AddFields(fields ...*MessageEmbedField) *MessageEmbedBuilder {
|
||||
b.MessageEmbed.Fields = fields
|
||||
func (b *MessageEmbed) AddFields(fields ...*MessageEmbedField) *MessageEmbed {
|
||||
b.Fields = fields
|
||||
return b
|
||||
}
|
||||
|
||||
// SetAuthor sets author for MessageEmbed.
|
||||
func (b *MessageEmbedBuilder) SetAuthor(author MessageEmbedAuthor) *MessageEmbedBuilder {
|
||||
b.MessageEmbed.Author = &author
|
||||
func (b *MessageEmbed) SetAuthor(author MessageEmbedAuthor) *MessageEmbed {
|
||||
b.Author = &author
|
||||
return b
|
||||
}
|
||||
|
||||
// SetURL sets url for MessageEmbed.
|
||||
func (b *MessageEmbedBuilder) SetURL(url string) *MessageEmbedBuilder {
|
||||
b.MessageEmbed.URL = url
|
||||
func (b *MessageEmbed) SetURL(url string) *MessageEmbed {
|
||||
b.URL = url
|
||||
return b
|
||||
}
|
||||
|
||||
// Build to MessageEmbed.
|
||||
func (b *MessageEmbedBuilder) Build() *MessageEmbed {
|
||||
return b.MessageEmbed
|
||||
// ActionRowBuilder creates new ActionRow struct.
|
||||
func ActionRowBuilder() *ActionsRow {
|
||||
return &ActionsRow{}
|
||||
}
|
||||
|
||||
// AddComponents adds components for MessageComponent.
|
||||
func (r *ActionsRow) AddComponents(components ...MessageComponent) {
|
||||
r.Components = append(r.Components, components...)
|
||||
}
|
||||
|
||||
// ButtonBuilder creates new Button struct.
|
||||
func ButtonBuilder() *Button {
|
||||
return &Button{}
|
||||
}
|
||||
|
||||
// SetCustomID sets customID for Button.
|
||||
func (b *Button) SetCustomID(customID string) *Button {
|
||||
b.CustomID = customID
|
||||
return b
|
||||
}
|
||||
|
||||
// SetLabel sets label for Button.
|
||||
func (b *Button) SetLabel(label string) *Button {
|
||||
b.Label = label
|
||||
return b
|
||||
}
|
||||
|
||||
// SetStyle sets style for Button.
|
||||
func (b *Button) SetStyle(style ButtonStyle) *Button {
|
||||
b.Style = style
|
||||
return b
|
||||
}
|
||||
|
||||
// SetEmoji sets emoji for Button.
|
||||
func (b *Button) SetEmoji(emoji ComponentEmoji) *Button {
|
||||
b.Emoji = &emoji
|
||||
return b
|
||||
}
|
||||
|
||||
// SetURL sets URL for Button.
|
||||
func (b *Button) SetURL(url string) *Button {
|
||||
b.URL = url
|
||||
return b
|
||||
}
|
||||
|
||||
// SetDisabled sets disabled for Button.
|
||||
func (b *Button) SetDisabled(disabled bool) *Button {
|
||||
b.Disabled = disabled
|
||||
return b
|
||||
}
|
||||
|
||||
// SetSKUID sets sku id for Button.
|
||||
func (b *Button) SetSKUID(skuID string) *Button {
|
||||
b.SKUID = skuID
|
||||
return b
|
||||
}
|
||||
|
||||
// SelectMenuBuilder creates a new SelectMenu struct.
|
||||
func SelectMenuBuilder(menuType SelectMenuType) *SelectMenu {
|
||||
return &SelectMenu{MenuType: menuType}
|
||||
}
|
||||
|
||||
// SetCustomID sets customID for SelectMenu.
|
||||
func (s *SelectMenu) SetCustomID(customID string) *SelectMenu {
|
||||
s.CustomID = customID
|
||||
return s
|
||||
}
|
||||
|
||||
// AddOptions adds options for SelectMenu.
|
||||
// NOTE: It can be used in string SelectMenu.
|
||||
func (s *SelectMenu) AddOptions(options ...SelectMenuOption) *SelectMenu {
|
||||
s.Options = append(s.Options, options...)
|
||||
return s
|
||||
}
|
||||
|
||||
// SetPlaceholder sets placeholder for SelectMenu.
|
||||
func (s *SelectMenu) SetPlaceholder(placeholder string) *SelectMenu {
|
||||
s.Placeholder = placeholder
|
||||
return s
|
||||
}
|
||||
|
||||
// SetMinValues sets min values for SelectMenu.
|
||||
func (s *SelectMenu) SetMinValues(minValues int) *SelectMenu {
|
||||
s.MinValues = &minValues
|
||||
return s
|
||||
}
|
||||
|
||||
// SetMaxValues sets max values for SelectMenu.
|
||||
func (s *SelectMenu) SetMaxValues(maxValues int) *SelectMenu {
|
||||
s.MaxValues = maxValues
|
||||
return s
|
||||
}
|
||||
|
||||
// SetDisabled sets disabled for SelectMenu.
|
||||
func (s *SelectMenu) SetDisabled(disabled bool) *SelectMenu {
|
||||
s.Disabled = disabled
|
||||
return s
|
||||
}
|
||||
|
||||
// AddDefaultValues adds default values for SelectMenu.
|
||||
// NOTE: It can not be used in string SelectMenu.
|
||||
func (s *SelectMenu) AddDefaultValues(defaultValues ...SelectMenuDefaultValue) *SelectMenu {
|
||||
s.DefaultValues = append(s.DefaultValues, defaultValues...)
|
||||
return s
|
||||
}
|
||||
|
||||
// SelectMenuOptionBuilder creates a new SelectMenuOption struct.
|
||||
func SelectMenuOptionBuilder() *SelectMenuOption {
|
||||
return &SelectMenuOption{}
|
||||
}
|
||||
|
||||
// SetLabel sets label for SelectMenuOption.
|
||||
func (o *SelectMenuOption) SetLabel(label string) *SelectMenuOption {
|
||||
o.Label = label
|
||||
return o
|
||||
}
|
||||
|
||||
// SetValue sets value for SelectMenuOption.
|
||||
func (o *SelectMenuOption) SetValue(value string) *SelectMenuOption {
|
||||
o.Value = value
|
||||
return o
|
||||
}
|
||||
|
||||
// SetDescription sets description for SelectMenuOption.
|
||||
func (o *SelectMenuOption) SetDescription(desc string) *SelectMenuOption {
|
||||
o.Description = desc
|
||||
return o
|
||||
}
|
||||
|
||||
// SetEmoji sets emoji for SelectMenuOption.
|
||||
func (o *SelectMenuOption) SetEmoji(emoji ComponentEmoji) *SelectMenuOption {
|
||||
o.Emoji = &emoji
|
||||
return o
|
||||
}
|
||||
|
||||
// SetDefault sets default for SelectMenuOption.
|
||||
func (o *SelectMenuOption) SetDefault(optionDefault bool) *SelectMenuOption {
|
||||
o.Default = optionDefault
|
||||
return o
|
||||
}
|
||||
|
||||
// SelectMenuDefaultValueBuilder creates a new SelectMenuDefaultValue struct.
|
||||
func SelectMenuDefaultValueBuilder(valueType SelectMenuDefaultValueType) *SelectMenuDefaultValue {
|
||||
return &SelectMenuDefaultValue{Type: valueType}
|
||||
}
|
||||
|
||||
// SetID sets ID for SelectMenuDefaultValue.
|
||||
func (d *SelectMenuDefaultValue) SetID(id string) *SelectMenuDefaultValue {
|
||||
d.ID = id
|
||||
return d
|
||||
}
|
||||
|
||||
// ComponentEmojiBuilder creates a new ComponentEmoji struct.
|
||||
func ComponentEmojiBuilder() *ComponentEmoji {
|
||||
return &ComponentEmoji{}
|
||||
}
|
||||
|
||||
// SetID sets ID for ComponentEmoji.
|
||||
func (c *ComponentEmoji) SetID(id string) *ComponentEmoji {
|
||||
c.ID = id
|
||||
return c
|
||||
}
|
||||
|
||||
// SetName sets name for ComponentEmoji.
|
||||
func (c *ComponentEmoji) SetName(name string) *ComponentEmoji {
|
||||
c.Name = name
|
||||
return c
|
||||
}
|
||||
|
||||
// SetAnimated sets animated for ComponentEmoji.
|
||||
func (c *ComponentEmoji) SetAnimated(animated bool) *ComponentEmoji {
|
||||
c.Animated = animated
|
||||
return c
|
||||
}
|
||||
|
||||
// TextInputBuilder creates a new TextInput struct.
|
||||
func TextInputBuilder() *TextInput {
|
||||
return &TextInput{}
|
||||
}
|
||||
|
||||
// SetCustomID sets customID for TextInput.
|
||||
func (t *TextInput) SetCustomID(customID string) *TextInput {
|
||||
t.CustomID = customID
|
||||
return t
|
||||
}
|
||||
|
||||
// SetStyle sets style for TextInput.
|
||||
func (t *TextInput) SetStyle(style TextInputStyle) *TextInput {
|
||||
t.Style = style
|
||||
return t
|
||||
}
|
||||
|
||||
// SetLabel sets label for TextInput.
|
||||
func (t *TextInput) SetLabel(label string) *TextInput {
|
||||
t.Label = label
|
||||
return t
|
||||
}
|
||||
|
||||
// SetMinLength sets min length for TextInput.
|
||||
func (t *TextInput) SetMinLength(minLength int) *TextInput {
|
||||
t.MinLength = minLength
|
||||
return t
|
||||
}
|
||||
|
||||
// SetMaxLength sets max length for TextInput.
|
||||
func (t *TextInput) SetMaxLength(maxLength int) *TextInput {
|
||||
t.MaxLength = maxLength
|
||||
return t
|
||||
}
|
||||
|
||||
// SetRequired sets required for TextInput.
|
||||
func (t *TextInput) SetRequired(required bool) *TextInput {
|
||||
t.Required = required
|
||||
return t
|
||||
}
|
||||
|
||||
// SetValue sets value for TextInput.
|
||||
func (t *TextInput) SetValue(value string) *TextInput {
|
||||
t.Value = value
|
||||
return t
|
||||
}
|
||||
|
||||
// SetPlaceholder sets placeholder for TextInput.
|
||||
func (t *TextInput) SetPlaceholder(placeholder string) *TextInput {
|
||||
t.Placeholder = placeholder
|
||||
return t
|
||||
}
|
||||
|
|
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