discord-tweeter/cmd/embed.go

99 lines
2.5 KiB
Go
Raw Normal View History

2023-08-29 18:00:00 +02:00
package cmd
import (
"errors"
"strconv"
"strings"
)
type Embed struct {
Author Author `json:"author"`
Title string `json:"title"`
URL string `json:"url"`
Description string `json:"description"`
Color int64 `json:"color"`
Fields []Field `json:"fields"`
Thumbnail Image `json:"thumbnail,omitempty"`
Image Image `json:"image,omitempty"`
Footer Footer `json:"footer"`
}
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"`
}
func (w *Webhook) NewEmbed(Title, Description, URL string) {
emb := Embed{Title: Title, Description: Description, URL: URL}
w.Embeds = append(w.Embeds, &emb)
}
func (w *Webhook) SetAuthor(Name, URL, IconURL string) {
if len(w.Embeds) == 0 {
emb := Embed{Author: Author{Name, URL, IconURL}}
w.Embeds = append(w.Embeds, &emb)
} else {
w.Embeds[0].Author = Author{Name, URL, IconURL}
}
}
func (w *Webhook) SetColor(color string) 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 errors.New("Invalid hex code passed")
}
w.Embeds[0].Color = colorInt
return nil
}
func (w *Webhook) SetThumbnail(URL string) error {
if len(w.Embeds) < 1 {
return errors.New("Invalid Embed passed in, Webhook.Embeds must have at least one Embed element")
}
w.Embeds[0].Thumbnail = Image{URL}
return nil
}
func (w *Webhook) SetImage(URL string) error {
if len(w.Embeds) < 1 {
return errors.New("Invalid Embed passed in, Webhook.Embeds must have at least one Embed element")
}
w.Embeds[0].Image = Image{URL}
return nil
}
func (w *Webhook) SetFooter(Text, IconURL string) error {
if len(w.Embeds) < 1 {
return errors.New("Invalid Embed passed in, Webhook.Embeds must have at least one Embed element")
}
w.Embeds[0].Footer = Footer{Text, IconURL}
return nil
}
func (w *Webhook) AddField(Name, Value string, Inline bool) error {
if len(w.Embeds) < 1 {
return errors.New("Invalid Embed passed in, Webhook.Embeds must have at least one Embed element")
}
w.Embeds[0].Fields = append(w.Embeds[0].Fields, Field{Name, Value, Inline})
return nil
}