arikawa/discord/message_embed.go

164 lines
3.4 KiB
Go
Raw Normal View History

package discord
2020-01-02 05:39:52 +00:00
import "fmt"
2020-01-02 05:39:52 +00:00
type Color uint
const DefaultColor Color = 0x303030
2020-01-02 05:39:52 +00:00
type Embed struct {
Title string `json:"title,omitempty"`
Type EmbedType `json:"type,omitempty"`
Description string `json:"description,omitempty"`
URL URL `json:"url,omitempty"`
2020-01-02 05:39:52 +00:00
Timestamp Timestamp `json:"timestamp,omitempty"`
Color Color `json:"color,omitempty"`
2020-01-02 05:39:52 +00:00
Footer *EmbedFooter `json:"footer,omitempty"`
Image *EmbedImage `json:"image,omitempty"`
Thumbnail *EmbedThumbnail `json:"thumbnail,omitempty"`
Video *EmbedVideo `json:"video,omitempty"`
Provider *EmbedProvider `json:"provider,omitempty"`
Author *EmbedAuthor `json:"author,omitempty"`
Fields []EmbedField `json:"fields,omitempty"`
}
func NewEmbed() *Embed {
return &Embed{
Type: NormalEmbed,
Color: DefaultColor,
}
}
type ErrOverbound struct {
Count int
Max int
Thing string
}
var _ error = (*ErrOverbound)(nil)
func (e ErrOverbound) Error() string {
if e.Thing == "" {
return fmt.Sprintf("Overbound error: %d > %d", e.Count, e.Max)
2020-01-02 05:39:52 +00:00
}
return fmt.Sprintf(e.Thing+" overbound: %d > %d", e.Count, e.Max)
2020-01-02 05:39:52 +00:00
}
func (e *Embed) Validate() error {
if e.Type == "" {
e.Type = NormalEmbed
}
if e.Color == 0 {
e.Color = DefaultColor
2020-01-02 05:39:52 +00:00
}
if len(e.Title) > 256 {
return &ErrOverbound{len(e.Title), 256, "Title"}
}
if len(e.Description) > 2048 {
return &ErrOverbound{len(e.Description), 2048, "Description"}
}
if len(e.Fields) > 25 {
return &ErrOverbound{len(e.Fields), 25, "Fields"}
}
var sum = 0 +
len(e.Title) +
len(e.Description)
if e.Footer != nil {
if len(e.Footer.Text) > 2048 {
return &ErrOverbound{len(e.Footer.Text), 2048, "Footer text"}
}
sum += len(e.Footer.Text)
}
if e.Author != nil {
if len(e.Author.Name) > 256 {
return &ErrOverbound{len(e.Author.Name), 256, "Author name"}
}
sum += len(e.Author.Name)
}
for i, field := range e.Fields {
if len(field.Name) > 256 {
return &ErrOverbound{len(field.Name), 256,
fmt.Sprintf("field %d name", i)}
}
if len(field.Value) > 1024 {
return &ErrOverbound{len(field.Value), 1024,
2020-01-16 04:51:22 +00:00
fmt.Sprintf("field %d value", i)}
2020-01-02 05:39:52 +00:00
}
sum += len(field.Name) + len(field.Value)
}
if sum > 6000 {
return &ErrOverbound{sum, 6000, "Sum of all characters"}
}
return nil
}
type EmbedType string
const (
NormalEmbed EmbedType = "rich"
ImageEmbed EmbedType = "image"
VideoEmbed EmbedType = "video"
2020-01-02 05:39:52 +00:00
// Undocumented
)
type EmbedFooter struct {
Text string `json:"text"`
Icon URL `json:"icon_url,omitempty"`
ProxyIcon URL `json:"proxy_icon_url,omitempty"`
2020-01-02 05:39:52 +00:00
}
type EmbedImage struct {
URL URL `json:"url"`
Proxy URL `json:"proxy_url"`
2020-01-02 05:39:52 +00:00
}
type EmbedThumbnail struct {
URL URL `json:"url,omitempty"`
Proxy URL `json:"proxy_url,omitempty"`
Height uint `json:"height,omitempty"`
Width uint `json:"width,omitempty"`
2020-01-02 05:39:52 +00:00
}
type EmbedVideo struct {
URL URL `json:"url"`
Height uint `json:"height"`
Width uint `json:"width"`
2020-01-02 05:39:52 +00:00
}
type EmbedProvider struct {
Name string `json:"name"`
URL URL `json:"url"`
2020-01-02 05:39:52 +00:00
}
type EmbedAuthor struct {
Name string `json:"name,omitempty"`
URL URL `json:"url,omitempty"`
Icon URL `json:"icon_url,omitempty"`
ProxyIcon URL `json:"proxy_icon_url,omitempty"`
2020-01-02 05:39:52 +00:00
}
type EmbedField struct {
Name string `json:"name"`
Value string `json:"value"`
Inline bool `json:"inline,omitempty"`
}