github.com/diamondburned/arikawa@v1.3.14/discord/emoji.go (about)

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