2020-01-02 19:53:08 +00:00
|
|
|
package discord
|
|
|
|
|
2020-12-16 20:24:23 +00:00
|
|
|
import (
|
2020-12-16 22:51:00 +00:00
|
|
|
"net/url"
|
2020-12-16 20:24:23 +00:00
|
|
|
"strings"
|
|
|
|
)
|
2020-01-02 19:53:08 +00:00
|
|
|
|
2020-11-22 14:48:36 +00:00
|
|
|
// https://discord.com/developers/docs/resources/emoji#emoji-object
|
2020-01-02 19:53:08 +00:00
|
|
|
type Emoji struct {
|
2020-11-22 14:48:36 +00:00
|
|
|
// ID is the ID of the Emoji.
|
|
|
|
// The ID will be NullSnowflake, if the Emoji is a Unicode emoji.
|
|
|
|
ID EmojiID `json:"id"`
|
|
|
|
// Name is the name of the emoji.
|
|
|
|
Name string `json:"name"`
|
2020-01-02 19:53:08 +00:00
|
|
|
|
2020-11-22 14:48:36 +00:00
|
|
|
// RoleIDs are the roles the emoji is whitelisted to.
|
|
|
|
//
|
|
|
|
// This field is only available for custom emojis.
|
2020-07-21 20:27:59 +00:00
|
|
|
RoleIDs []RoleID `json:"roles,omitempty"`
|
2020-11-22 14:48:36 +00:00
|
|
|
// User is the user that created the emoji.
|
|
|
|
//
|
|
|
|
// This field is only available for custom emojis.
|
|
|
|
User User `json:"user,omitempty"`
|
2020-01-02 19:53:08 +00:00
|
|
|
|
2020-11-22 14:48:36 +00:00
|
|
|
// RequireColons specifies whether the emoji must be wrapped in colons.
|
|
|
|
//
|
|
|
|
// This field is only available for custom emojis.
|
2020-01-02 19:53:08 +00:00
|
|
|
RequireColons bool `json:"require_colons,omitempty"`
|
2020-11-22 14:48:36 +00:00
|
|
|
// Managed specifies whether the emoji is managed.
|
|
|
|
//
|
|
|
|
// This field is only available for custom emojis.
|
|
|
|
Managed bool `json:"managed,omitempty"`
|
|
|
|
// Animated specifies whether the emoji is animated.
|
|
|
|
//
|
|
|
|
// This field is only available for custom emojis.
|
|
|
|
Animated bool `json:"animated,omitempty"`
|
|
|
|
// Available specifies whether the emoji can be used.
|
|
|
|
// This may be false tue to loss of Server Boosts.
|
|
|
|
//
|
|
|
|
// This field is only available for custom emojis.
|
|
|
|
Available bool `json:"available,omitempty"`
|
2020-01-02 19:53:08 +00:00
|
|
|
}
|
|
|
|
|
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-12-16 20:24:23 +00:00
|
|
|
// APIEmoji represents an emoji identifier string formatted to be used with the
|
|
|
|
// API. It is formatted using Emoji's APIString method as well as the
|
|
|
|
// NewCustomEmoji function. If the emoji is a stock Unicode emoji, then this
|
|
|
|
// string contains it. Otherwise, it is formatted like "emoji_name:123123123",
|
|
|
|
// where "123123123" is the emoji ID.
|
|
|
|
type APIEmoji string
|
|
|
|
|
|
|
|
// NewCustomEmoji creates a new Emoji using a custom guild emoji as base.
|
|
|
|
// Unicode emojis should be directly converted.
|
|
|
|
func NewCustomEmoji(id EmojiID, name string) APIEmoji {
|
|
|
|
return APIEmoji(name + ":" + id.String())
|
|
|
|
}
|
|
|
|
|
2020-12-16 22:51:00 +00:00
|
|
|
// PathString returns the APIEmoji as a path-encoded string.
|
|
|
|
func (e APIEmoji) PathString() string {
|
|
|
|
return url.PathEscape(string(e))
|
2020-12-16 20:24:23 +00:00
|
|
|
}
|
|
|
|
|
2020-01-02 19:53:08 +00:00
|
|
|
// APIString returns a string usable for sending over to the API.
|
2020-12-16 20:24:23 +00:00
|
|
|
func (e Emoji) APIString() APIEmoji {
|
2020-07-29 20:10:17 +00:00
|
|
|
if !e.ID.IsValid() {
|
2020-12-16 20:24:23 +00:00
|
|
|
return APIEmoji(e.Name) // is unicode
|
2020-01-02 19:53:08 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 20:24:23 +00:00
|
|
|
return NewCustomEmoji(e.ID, e.Name)
|
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
|
|
|
}
|