EDA.Emoji (EDA v0.3.0)

Copy Markdown View Source

Represents a Discord emoji (unicode or custom guild emoji).

Unicode emojis have only a name (the unicode character). Custom emojis have an id, name, and optional metadata.

Implements String.Chars so you can interpolate an emoji directly into a message string and get the correct mention format.

Summary

Functions

Returns the API-formatted string for use in REST endpoints.

Returns true if this is a custom emoji (has an id).

Converts a raw Discord emoji map into this struct.

Returns the CDN URL for a custom emoji image.

Returns the mention string for embedding in messages.

Returns true if this is a unicode emoji (no id).

Types

t()

@type t() :: %EDA.Emoji{
  animated: boolean() | nil,
  available: boolean() | nil,
  id: String.t() | nil,
  managed: boolean() | nil,
  name: String.t() | nil,
  require_colons: boolean() | nil,
  roles: [String.t()] | nil,
  user: EDA.User.t() | nil
}

Functions

api_name(emoji)

@spec api_name(t()) :: String.t()

Returns the API-formatted string for use in REST endpoints.

Unicode emojis return just the name. Custom emojis return name:id.

Examples

iex> EDA.Emoji.api_name(%EDA.Emoji{id: nil, name: "👍"})
"👍"

iex> EDA.Emoji.api_name(%EDA.Emoji{id: "123", name: "cool"})
"cool:123"

custom?(emoji)

@spec custom?(t()) :: boolean()

Returns true if this is a custom emoji (has an id).

Examples

iex> EDA.Emoji.custom?(%EDA.Emoji{id: "123", name: "cool"})
true

iex> EDA.Emoji.custom?(%EDA.Emoji{id: nil, name: "👍"})
false

from_raw(raw)

@spec from_raw(map()) :: t()

Converts a raw Discord emoji map into this struct.

Examples

iex> EDA.Emoji.from_raw(%{"id" => "123", "name" => "cool", "animated" => true})
%EDA.Emoji{id: "123", name: "cool", animated: true}

iex> EDA.Emoji.from_raw(%{"id" => nil, "name" => "👍"})
%EDA.Emoji{id: nil, name: "👍"}

image_url(emoji)

@spec image_url(t()) :: String.t() | nil

Returns the CDN URL for a custom emoji image.

Returns nil for unicode emojis. Animated emojis get a .gif extension, others get .png.

Examples

iex> EDA.Emoji.image_url(%EDA.Emoji{id: "123", name: "cool", animated: true})
"https://cdn.discordapp.com/emojis/123.gif"

iex> EDA.Emoji.image_url(%EDA.Emoji{id: "123", name: "cool"})
"https://cdn.discordapp.com/emojis/123.png"

iex> EDA.Emoji.image_url(%EDA.Emoji{id: nil, name: "👍"})
nil

mention(emoji)

@spec mention(t()) :: String.t()

Returns the mention string for embedding in messages.

Unicode emojis return just the name. Custom emojis return <:name:id> or <a:name:id> for animated emojis.

Examples

iex> EDA.Emoji.mention(%EDA.Emoji{id: nil, name: "👍"})
"👍"

iex> EDA.Emoji.mention(%EDA.Emoji{id: "123", name: "cool", animated: false})
"<:cool:123>"

iex> EDA.Emoji.mention(%EDA.Emoji{id: "123", name: "cool", animated: true})
"<a:cool:123>"

unicode?(emoji)

@spec unicode?(t()) :: boolean()

Returns true if this is a unicode emoji (no id).

Examples

iex> EDA.Emoji.unicode?(%EDA.Emoji{id: nil, name: "👍"})
true

iex> EDA.Emoji.unicode?(%EDA.Emoji{id: "123", name: "cool"})
false