discord-tweeter/cmd/embed.go

116 lines
2.5 KiB
Go

package cmd
import (
"errors"
"strconv"
"strings"
"time"
)
type Embed struct {
Author Author `json:"author,omitempty"`
Title string `json:"title,omitempty"`
URL string `json:"url,omitempty"`
Description string `json:"description,omitempty"`
Timestamp string `json:"timestamp,omitempty"`
Color int64 `json:"color,omitempty"`
Fields []Field `json:"fields,omitempty"`
Thumbnail Image `json:"thumbnail,omitempty"`
Image Image `json:"image,omitempty"`
Video Video `json:"video,omitempty"`
Footer Footer `json:"footer,omitempty"`
}
type Author struct {
Name string `json:"name"`
URL string `json:"url"`
IconURL string `json:"icon_url"`
}
type Field struct {
Name string `json:"name"`
Value string `json:"value"`
Inline bool `json:"inline,omitempty"`
}
type Footer struct {
Text string `json:"text"`
IconURL string `json:"icon_url,omitempty"`
}
type Image struct {
URL string `json:"url"`
}
type Video struct {
URL string `json:"url"`
}
func (w *Webhook) NewEmbedWithURL(URL string) *Embed {
emb := Embed{URL: URL}
w.Embeds = append(w.Embeds, &emb)
return &emb
}
func (w *Webhook) NewEmbed() *Embed {
emb := Embed{}
w.Embeds = append(w.Embeds, &emb)
return &emb
}
func (e *Embed) SetTitle(Title string) *Embed {
e.Title = Title
return e
}
func (e *Embed) SetText(Description string) *Embed {
e.Description = Description
return e
}
func (e *Embed) SetAuthor(Name, URL, IconURL string) *Embed {
e.Author = Author{Name, URL, IconURL}
return e
}
func (e *Embed) SetColor(color string) (*Embed, error) {
color = strings.Replace(color, "0x", "", -1)
color = strings.Replace(color, "0X", "", -1)
color = strings.Replace(color, "#", "", -1)
colorInt, err := strconv.ParseInt(color, 16, 64)
if err != nil {
return nil, errors.New("Invalid hex code passed")
}
e.Color = colorInt
return e, nil
}
func (e *Embed) SetThumbnail(URL string) *Embed {
e.Thumbnail = Image{URL}
return e
}
func (e *Embed) SetImage(URL string) *Embed {
e.Image = Image{URL}
return e
}
func (e *Embed) SetVideo(URL string) *Embed {
e.Video = Video{URL}
return e
}
func (e *Embed) SetFooter(Text, IconURL string) *Embed {
e.Footer = Footer{Text, IconURL}
return e
}
func (e *Embed) SetTimestamp(timestamp time.Time) *Embed {
e.Timestamp = timestamp.Format(time.RFC3339)
return e
}
func (e *Embed) AddField(Name, Value string, Inline bool) *Embed {
e.Fields = append(e.Fields, Field{Name, Value, Inline})
return e
}