-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
emoji.go
71 lines (55 loc) · 1.47 KB
/
emoji.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package discord
import "strings"
type Emoji struct {
ID Snowflake `json:"id,string"` // NullSnowflake for unicode emojis
Name string `json:"name"`
// These fields are optional
RoleIDs []Snowflake `json:"roles,omitempty"`
User User `json:"user,omitempty"`
RequireColons bool `json:"require_colons,omitempty"`
Managed bool `json:"managed,omitempty"`
Animated bool `json:"animated,omitempty"`
}
// EmojiURL returns the URL of the emoji and auto-detects a suitable type.
//
// This will only work for custom emojis.
func (e Emoji) EmojiURL() string {
if e.Animated {
return e.EmojiURLWithType(GIFImage)
}
return e.EmojiURLWithType(PNGImage)
}
// EmojiURLWithType returns the URL to the emoji's image.
//
// This will only work for custom emojis.
//
// Supported ImageTypes: PNG, GIF
func (e Emoji) EmojiURLWithType(t ImageType) string {
if e.ID == NullSnowflake {
return ""
}
if t == AutoImage {
return e.EmojiURL()
}
return "https://cdn.discordapp.com/emojis/" + t.format(e.ID.String())
}
// APIString returns a string usable for sending over to the API.
func (e Emoji) APIString() string {
if !e.ID.Valid() {
return e.Name // is unicode
}
return e.Name + ":" + e.ID.String()
}
// String formats the string like how the client does.
func (e Emoji) String() string {
if e.ID == 0 {
return e.Name
}
var parts = [3]string{
"", e.Name, e.ID.String(),
}
if e.Animated {
parts[0] = "a"
}
return "<" + strings.Join(parts[:], ":") + ">"
}