discord-tweeter/cmd/embed.go

117 lines
2.5 KiB
Go
Raw Permalink Normal View History

2023-08-29 18:00:00 +02:00
package cmd
import (
"errors"
"strconv"
"strings"
2023-10-17 00:10:00 +02:00
"time"
2023-08-29 18:00:00 +02:00
)
type Embed struct {
2023-10-17 00:10:00 +02:00
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"`
2023-08-29 18:00:00 +02:00
Thumbnail Image `json:"thumbnail,omitempty"`
Image Image `json:"image,omitempty"`
2023-10-17 00:10:00 +02:00
Video Video `json:"video,omitempty"`
Footer Footer `json:"footer,omitempty"`
2023-08-29 18:00:00 +02:00
}
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"`
}
2023-10-17 00:10:00 +02:00
type Video struct {
URL string `json:"url"`
}
func (w *Webhook) NewEmbedWithURL(URL string) *Embed {
emb := Embed{URL: URL}
2023-08-29 18:00:00 +02:00
w.Embeds = append(w.Embeds, &emb)
2023-10-17 00:10:00 +02:00
return &emb
2023-08-29 18:00:00 +02:00
}
2023-10-17 00:10:00 +02:00
func (w *Webhook) NewEmbed() *Embed {
emb := Embed{}
w.Embeds = append(w.Embeds, &emb)
return &emb
2023-08-29 18:00:00 +02:00
}
2023-10-17 00:10:00 +02:00
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) {
2023-08-29 18:00:00 +02:00
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 {
2023-10-17 00:10:00 +02:00
return nil, errors.New("Invalid hex code passed")
2023-08-29 18:00:00 +02:00
}
2023-10-17 00:10:00 +02:00
e.Color = colorInt
return e, nil
2023-08-29 18:00:00 +02:00
}
2023-10-17 00:10:00 +02:00
func (e *Embed) SetThumbnail(URL string) *Embed {
e.Thumbnail = Image{URL}
return e
2023-08-29 18:00:00 +02:00
}
2023-10-17 00:10:00 +02:00
func (e *Embed) SetImage(URL string) *Embed {
e.Image = Image{URL}
return e
2023-08-29 18:00:00 +02:00
}
2023-10-17 00:10:00 +02:00
func (e *Embed) SetVideo(URL string) *Embed {
e.Video = Video{URL}
return e
2023-08-29 18:00:00 +02:00
}
2023-10-17 00:10:00 +02:00
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
2023-08-29 18:00:00 +02:00
}