github.com/diamondburned/arikawa/v2@v2.1.0/discord/message_embed.go (about) 1 package discord 2 3 import "fmt" 4 5 type Color uint32 6 7 var DefaultEmbedColor Color = 0x303030 8 9 func (c Color) Uint32() uint32 { 10 return uint32(c) 11 } 12 13 func (c Color) Int() int { 14 return int(c) 15 } 16 17 // RGB splits Color into red, green, and blue. The maximum value is 255. 18 func (c Color) RGB() (uint8, uint8, uint8) { 19 var ( 20 color = c.Uint32() 21 22 r = uint8((color >> 16) & 255) 23 g = uint8((color >> 8) & 255) 24 b = uint8(color & 255) 25 ) 26 27 return r, g, b 28 } 29 30 type Embed struct { 31 Title string `json:"title,omitempty"` 32 Type EmbedType `json:"type,omitempty"` 33 Description string `json:"description,omitempty"` 34 35 URL URL `json:"url,omitempty"` 36 37 Timestamp Timestamp `json:"timestamp,omitempty"` 38 Color Color `json:"color,omitempty"` 39 40 Footer *EmbedFooter `json:"footer,omitempty"` 41 Image *EmbedImage `json:"image,omitempty"` 42 Thumbnail *EmbedThumbnail `json:"thumbnail,omitempty"` 43 Video *EmbedVideo `json:"video,omitempty"` 44 Provider *EmbedProvider `json:"provider,omitempty"` 45 Author *EmbedAuthor `json:"author,omitempty"` 46 Fields []EmbedField `json:"fields,omitempty"` 47 } 48 49 func NewEmbed() *Embed { 50 return &Embed{ 51 Type: NormalEmbed, 52 Color: DefaultEmbedColor, 53 } 54 } 55 56 type ErrOverbound struct { 57 Count int 58 Max int 59 60 Thing string 61 } 62 63 var _ error = (*ErrOverbound)(nil) 64 65 func (e ErrOverbound) Error() string { 66 if e.Thing == "" { 67 return fmt.Sprintf("Overbound error: %d > %d", e.Count, e.Max) 68 } 69 70 return fmt.Sprintf(e.Thing+" overbound: %d > %d", e.Count, e.Max) 71 } 72 73 func (e *Embed) Validate() error { 74 if e.Type == "" { 75 e.Type = NormalEmbed 76 } 77 78 if e.Color == 0 { 79 e.Color = DefaultEmbedColor 80 } 81 82 if len(e.Title) > 256 { 83 return &ErrOverbound{len(e.Title), 256, "title"} 84 } 85 86 if len(e.Description) > 2048 { 87 return &ErrOverbound{len(e.Description), 2048, "description"} 88 } 89 90 if len(e.Fields) > 25 { 91 return &ErrOverbound{len(e.Fields), 25, "fields"} 92 } 93 94 var sum = 0 + 95 len(e.Title) + 96 len(e.Description) 97 98 if e.Footer != nil { 99 if len(e.Footer.Text) > 2048 { 100 return &ErrOverbound{len(e.Footer.Text), 2048, "footer text"} 101 } 102 103 sum += len(e.Footer.Text) 104 } 105 106 if e.Author != nil { 107 if len(e.Author.Name) > 256 { 108 return &ErrOverbound{len(e.Author.Name), 256, "author name"} 109 } 110 111 sum += len(e.Author.Name) 112 } 113 114 for i, field := range e.Fields { 115 if len(field.Name) > 256 { 116 return &ErrOverbound{len(field.Name), 256, 117 fmt.Sprintf("field %d name", i)} 118 } 119 120 if len(field.Value) > 1024 { 121 return &ErrOverbound{len(field.Value), 1024, 122 fmt.Sprintf("field %d value", i)} 123 } 124 125 sum += len(field.Name) + len(field.Value) 126 } 127 128 if sum > 6000 { 129 return &ErrOverbound{sum, 6000, "sum of all characters"} 130 } 131 132 return nil 133 } 134 135 type EmbedType string 136 137 const ( 138 NormalEmbed EmbedType = "rich" 139 ImageEmbed EmbedType = "image" 140 VideoEmbed EmbedType = "video" 141 GIFVEmbed EmbedType = "gifv" 142 ArticleEmbed EmbedType = "article" 143 LinkEmbed EmbedType = "link" 144 // Undocumented 145 ) 146 147 type EmbedFooter struct { 148 Text string `json:"text"` 149 Icon URL `json:"icon_url,omitempty"` 150 ProxyIcon URL `json:"proxy_icon_url,omitempty"` 151 } 152 153 type EmbedImage struct { 154 URL URL `json:"url"` 155 Proxy URL `json:"proxy_url"` 156 Height uint `json:"height,omitempty"` 157 Width uint `json:"width,omitempty"` 158 } 159 160 type EmbedThumbnail struct { 161 URL URL `json:"url,omitempty"` 162 Proxy URL `json:"proxy_url,omitempty"` 163 Height uint `json:"height,omitempty"` 164 Width uint `json:"width,omitempty"` 165 } 166 167 type EmbedVideo struct { 168 URL URL `json:"url"` 169 Height uint `json:"height"` 170 Width uint `json:"width"` 171 } 172 173 type EmbedProvider struct { 174 Name string `json:"name"` 175 URL URL `json:"url"` 176 } 177 178 type EmbedAuthor struct { 179 Name string `json:"name,omitempty"` 180 URL URL `json:"url,omitempty"` 181 Icon URL `json:"icon_url,omitempty"` 182 ProxyIcon URL `json:"proxy_icon_url,omitempty"` 183 } 184 185 type EmbedField struct { 186 Name string `json:"name"` 187 Value string `json:"value"` 188 Inline bool `json:"inline,omitempty"` 189 }