github.com/diamondburned/arikawa/v2@v2.1.0/discord/emoji.go (about)

     1  package discord
     2  
     3  import (
     4  	"net/url"
     5  	"strings"
     6  	"time"
     7  )
     8  
     9  // https://discord.com/developers/docs/resources/emoji#emoji-object
    10  type Emoji struct {
    11  	// ID is the ID of the Emoji.
    12  	// The ID will be NullSnowflake, if the Emoji is a Unicode emoji.
    13  	ID EmojiID `json:"id"`
    14  	// Name is the name of the emoji.
    15  	Name string `json:"name"`
    16  
    17  	// RoleIDs are the roles the emoji is whitelisted to.
    18  	//
    19  	// This field is only available for custom emojis.
    20  	RoleIDs []RoleID `json:"roles,omitempty"`
    21  	// User is the user that created the emoji.
    22  	//
    23  	// This field is only available for custom emojis.
    24  	User User `json:"user,omitempty"`
    25  
    26  	// RequireColons specifies whether the emoji must be wrapped in colons.
    27  	//
    28  	// This field is only available for custom emojis.
    29  	RequireColons bool `json:"require_colons,omitempty"`
    30  	// Managed specifies whether the emoji is managed.
    31  	//
    32  	// This field is only available for custom emojis.
    33  	Managed bool `json:"managed,omitempty"`
    34  	// Animated specifies whether the emoji is animated.
    35  	//
    36  	// This field is only available for custom emojis.
    37  	Animated bool `json:"animated,omitempty"`
    38  	// Available specifies whether the emoji can be used.
    39  	// This may be false due to loss of Server Boosts.
    40  	//
    41  	// This field is only available for custom emojis.
    42  	Available bool `json:"available,omitempty"`
    43  }
    44  
    45  // IsCustom returns whether the emoji is a custom emoji.
    46  func (e Emoji) IsCustom() bool {
    47  	return e.ID.IsValid()
    48  }
    49  
    50  // IsUnicode returns whether the emoji is a unicode emoji.
    51  func (e Emoji) IsUnicode() bool {
    52  	return !e.IsCustom()
    53  }
    54  
    55  // CreatedAt returns a time object representing when the emoji was created.
    56  //
    57  // This will only work for custom emojis.
    58  func (e Emoji) CreatedAt() time.Time {
    59  	return e.ID.Time()
    60  }
    61  
    62  // EmojiURL returns the URL of the emoji and auto-detects a suitable type.
    63  //
    64  // This will only work for custom emojis.
    65  func (e Emoji) EmojiURL() string {
    66  	if e.Animated {
    67  		return e.EmojiURLWithType(GIFImage)
    68  	}
    69  
    70  	return e.EmojiURLWithType(PNGImage)
    71  }
    72  
    73  // EmojiURLWithType returns the URL to the emoji's image.
    74  //
    75  // This will only work for custom emojis.
    76  //
    77  // Supported ImageTypes: PNG, GIF
    78  func (e Emoji) EmojiURLWithType(t ImageType) string {
    79  	if e.ID.IsNull() {
    80  		return ""
    81  	}
    82  
    83  	if t == AutoImage {
    84  		return e.EmojiURL()
    85  	}
    86  
    87  	return "https://cdn.discordapp.com/emojis/" + t.format(e.ID.String())
    88  }
    89  
    90  // APIEmoji represents an emoji identifier string formatted to be used with the
    91  // API. It is formatted using Emoji's APIString method as well as the
    92  // NewCustomEmoji function. If the emoji is a stock Unicode emoji, then this
    93  // string contains it. Otherwise, it is formatted like "emoji_name:123123123",
    94  // where "123123123" is the emoji ID.
    95  type APIEmoji string
    96  
    97  // NewCustomEmoji creates a new Emoji using a custom guild emoji as base.
    98  // Unicode emojis should be directly converted.
    99  func NewCustomEmoji(id EmojiID, name string) APIEmoji {
   100  	return APIEmoji(name + ":" + id.String())
   101  }
   102  
   103  // PathString returns the APIEmoji as a path-encoded string.
   104  func (e APIEmoji) PathString() string {
   105  	return url.PathEscape(string(e))
   106  }
   107  
   108  // APIString returns a string usable for sending over to the API.
   109  func (e Emoji) APIString() APIEmoji {
   110  	if !e.ID.IsValid() {
   111  		return APIEmoji(e.Name) // is unicode
   112  	}
   113  
   114  	return NewCustomEmoji(e.ID, e.Name)
   115  }
   116  
   117  // String formats the string like how the client does.
   118  func (e Emoji) String() string {
   119  	if e.ID == 0 {
   120  		return e.Name
   121  	}
   122  
   123  	var parts = [3]string{
   124  		"", e.Name, e.ID.String(),
   125  	}
   126  
   127  	if e.Animated {
   128  		parts[0] = "a"
   129  	}
   130  
   131  	return "<" + strings.Join(parts[:], ":") + ">"
   132  }