discord/component.go
2025-06-02 13:18:10 -04:00

205 lines
4.1 KiB
Go

package discord
type ComponentType int
const (
ComponentTypeActionRow ComponentType = 1
ComponentTypeButton ComponentType = 2
ComponentTypeSection ComponentType = 9
ComponentTypeText ComponentType = 10
ComponentTypeThumbnail ComponentType = 11
ComponentTypeSep ComponentType = 14
ComponentTypeContainer ComponentType = 17
)
type BaseComponent struct {
ID *int `json:"id,omitempty"`
Type ComponentType `json:"type"`
}
type ComponentLike interface {
Base() BaseComponent
}
type AccessoryLike interface {
AccessoryLike() bool
}
type TextDisplayLike interface {
TextDisplayLike() bool
}
type ActionRowComponent struct {
BaseComponent
Components []ComponentLike `json:"components"`
}
func (t ActionRowComponent) Base() BaseComponent {
return t.BaseComponent
}
func ActionRow(components ...ComponentLike) ActionRowComponent {
return ActionRowComponent{
BaseComponent: BaseComponent{
Type: ComponentTypeActionRow,
},
Components: components,
}
}
type TextComponent struct {
BaseComponent
Content string `json:"content"`
}
func (t TextComponent) Base() BaseComponent {
return t.BaseComponent
}
func (t TextComponent) TextDisplayLike() bool {
return true
}
type ContainerComponent struct {
BaseComponent
Components any `json:"components"`
Color *int `json:"accent_color,omitempty"`
}
func (t ContainerComponent) Base() BaseComponent {
return t.BaseComponent
}
type UnfurledMedia struct {
URL string `json:"url"`
}
type ThumbnailComponent struct {
BaseComponent
Media UnfurledMedia `json:"unfurledMedia"`
Description string `json:"description"`
}
func (t ThumbnailComponent) Base() BaseComponent {
return t.BaseComponent
}
func (t ThumbnailComponent) AccessoryLike() bool {
return true
}
type SectionComponent struct {
BaseComponent
Components []TextDisplayLike `json:"components"`
Accessory AccessoryLike `json:"accessory"`
}
func (t SectionComponent) Base() BaseComponent {
return t.BaseComponent
}
func Section(text []TextDisplayLike, accessory AccessoryLike) SectionComponent {
return SectionComponent{
BaseComponent: BaseComponent{
Type: ComponentTypeSection,
},
Components: text,
Accessory: accessory,
}
}
type SeparatorComponent struct {
BaseComponent
Divider bool `json:"divider"`
Spacing int `json:"spacing"`
}
func (t SeparatorComponent) Base() BaseComponent {
return t.BaseComponent
}
type ButtonStyle int
const (
ButtonStylePrimary ButtonStyle = iota + 1
ButtonStyleSecondary
ButtonStyleSuccess
ButtonStyleDanger
ButtonStyleLink
ButtonStylePremium
)
type ButtonComponent struct {
BaseComponent
Style ButtonStyle `json:"style"`
Label string `json:"label"`
Emoji *PartialEmoji `json:"emoji,omitempty"`
CustomID string `json:"custom_id,omitempty"`
URL string `json:"url,omitempty"`
}
func (t ButtonComponent) Base() BaseComponent {
return t.BaseComponent
}
func (t ButtonComponent) AccessoryLike() bool {
return true
}
func Container(id int, color *int, components ...any) ContainerComponent {
return ContainerComponent{
BaseComponent: BaseComponent{
ID: &id,
Type: ComponentTypeContainer,
},
Color: color,
Components: components,
}
}
func Text(id int, content string) TextComponent {
return TextComponent{
BaseComponent: BaseComponent{
ID: &id,
Type: ComponentTypeText,
},
Content: content,
}
}
func Separator(id int, divider bool, spacing int) SeparatorComponent {
return SeparatorComponent{
BaseComponent: BaseComponent{
ID: &id,
Type: ComponentTypeSep,
},
Divider: divider,
Spacing: spacing,
}
}
func Button(style ButtonStyle, customID, label string, emoji *PartialEmoji) ButtonComponent {
return ButtonComponent{
BaseComponent: BaseComponent{
Type: ComponentTypeButton,
},
Style: style,
CustomID: customID,
Label: label,
Emoji: emoji,
}
}
func LinkButton(label, url string, emoji *PartialEmoji) ButtonComponent {
return ButtonComponent{
BaseComponent: BaseComponent{
Type: ComponentTypeButton,
},
Style: ButtonStyleLink,
URL: url,
Label: label,
Emoji: emoji,
}
}