feat: Add embeds, rework embed struct

This commit is contained in:
Manuel 2023-10-17 00:10:00 +02:00
parent 712d83c0f0
commit e6bf12fa35
Signed by: Manuel
GPG key ID: 4085037435E1F07A
2 changed files with 129 additions and 54 deletions

View file

@ -4,18 +4,21 @@ import (
"errors"
"strconv"
"strings"
"time"
)
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"`
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"`
Footer Footer `json:"footer"`
Video Video `json:"video,omitempty"`
Footer Footer `json:"footer,omitempty"`
}
type Author struct {
@ -39,60 +42,75 @@ type Image struct {
URL string `json:"url"`
}
func (w *Webhook) NewEmbed(Title, Description, URL string) {
emb := Embed{Title: Title, Description: Description, URL: 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) 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) NewEmbed() *Embed {
emb := Embed{}
w.Embeds = append(w.Embeds, &emb)
return &emb
}
func (w *Webhook) SetColor(color string) error {
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 errors.New("Invalid hex code passed")
return nil, errors.New("Invalid hex code passed")
}
w.Embeds[0].Color = colorInt
return nil
e.Color = colorInt
return e, 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 (e *Embed) SetThumbnail(URL string) *Embed {
e.Thumbnail = Image{URL}
return e
}
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 (e *Embed) SetImage(URL string) *Embed {
e.Image = Image{URL}
return e
}
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 (e *Embed) SetVideo(URL string) *Embed {
e.Video = Video{URL}
return e
}
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
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
}