2020-01-02 19:53:08 +00:00
|
|
|
package discord
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
|
|
|
type Emoji struct {
|
2020-07-21 20:27:59 +00:00
|
|
|
ID EmojiID `json:"id,string"` // NullSnowflake for unicode emojis
|
|
|
|
Name string `json:"name"`
|
2020-01-02 19:53:08 +00:00
|
|
|
|
|
|
|
// These fields are optional
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
RoleIDs []RoleID `json:"roles,omitempty"`
|
|
|
|
User User `json:"user,omitempty"`
|
2020-01-02 19:53:08 +00:00
|
|
|
|
|
|
|
RequireColons bool `json:"require_colons,omitempty"`
|
|
|
|
Managed bool `json:"managed,omitempty"`
|
|
|
|
Animated bool `json:"animated,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-05-23 17:17:30 +00:00
|
|
|
// EmojiURL returns the URL of the emoji and auto-detects a suitable type.
|
2020-05-15 20:07:27 +00:00
|
|
|
//
|
|
|
|
// This will only work for custom emojis.
|
|
|
|
func (e Emoji) EmojiURL() string {
|
2020-05-23 17:17:30 +00:00
|
|
|
if e.Animated {
|
|
|
|
return e.EmojiURLWithType(GIFImage)
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.EmojiURLWithType(PNGImage)
|
2020-05-15 20:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2020-07-21 20:27:59 +00:00
|
|
|
if e.ID.IsNull() {
|
2020-05-15 20:07:27 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-05-23 17:17:30 +00:00
|
|
|
if t == AutoImage {
|
|
|
|
return e.EmojiURL()
|
|
|
|
}
|
|
|
|
|
2020-05-15 20:07:27 +00:00
|
|
|
return "https://cdn.discordapp.com/emojis/" + t.format(e.ID.String())
|
|
|
|
}
|
|
|
|
|
2020-01-02 19:53:08 +00:00
|
|
|
// APIString returns a string usable for sending over to the API.
|
|
|
|
func (e Emoji) APIString() string {
|
2020-07-29 20:10:17 +00:00
|
|
|
if !e.ID.IsValid() {
|
2020-01-02 19:53:08 +00:00
|
|
|
return e.Name // is unicode
|
|
|
|
}
|
|
|
|
|
2020-01-25 08:05:14 +00:00
|
|
|
return e.Name + ":" + e.ID.String()
|
2020-01-02 19:53:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// String formats the string like how the client does.
|
|
|
|
func (e Emoji) String() string {
|
|
|
|
if e.ID == 0 {
|
|
|
|
return e.Name
|
|
|
|
}
|
|
|
|
|
2020-01-25 08:05:14 +00:00
|
|
|
var parts = [3]string{
|
2020-01-02 19:53:08 +00:00
|
|
|
"", e.Name, e.ID.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Animated {
|
|
|
|
parts[0] = "a"
|
|
|
|
}
|
|
|
|
|
2020-01-25 08:05:14 +00:00
|
|
|
return "<" + strings.Join(parts[:], ":") + ">"
|
2020-01-02 19:53:08 +00:00
|
|
|
}
|