Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
118 lines (93 sloc) 3.47 KB
defmodule Crux.Structs.Emoji do
@moduledoc """
Represents a Discord [Emoji Object](
Differences opposed to the Discord API Object:
- `:user` is just the user id
@behaviour Crux.Structs
alias Crux.Structs.{Emoji, Util}
require Util
animated: nil,
id: nil,
name: nil,
roles: nil,
user: nil,
require_colons: nil,
managed: nil
@type t :: %__MODULE__{
animated: boolean() | nil,
id: Crux.Rest.snowflake() | nil,
name: String.t(),
roles: MapSet.t(Crux.Rest.snowflake()),
user: Crux.Rest.snowflake() | nil,
require_colons: boolean() | nil,
managed: boolean() | nil
@doc """
Creates a `Crux.Structs.Emoji` struct from raw data.
> Automatically invoked by `Crux.Structs.create/2`.
@spec create(data :: map()) :: t()
def create(data) do
emoji =
|> Util.atomify()
|> Map.update(:id, nil, &Util.id_to_int/1)
|> Map.update(:roles,, &, fn role -> Util.id_to_int(role) end))
|> Map.update(:user, nil, Util.map_to_id())
struct(__MODULE__, emoji)
@doc ~S"""
Converts an `Crux.Structs.Emoji`, a `Crux.Structs.Reaction`, or a `String.t()` to its discord identifier format.
> This is automatically done if using a appropriate rest function.
## Examples
# A custom emoji
iex> %Crux.Structs.Emoji{animated: false, id: 396521773216301056, name: "blobwavereverse"}
...> |> Crux.Structs.Emoji.to_identifier()
# A custom animated emoji
iex> %Crux.Structs.Emoji{animated: true, id: 396521774466203659, name: "ablobwavereverse"}
...> |> Crux.Structs.Emoji.to_identifier()
# A regular emoji
iex> %Crux.Structs.Emoji{animated: false, id: nil, name: "👋"}
...> |> Crux.Structs.Emoji.to_identifier()
# A reaction struct
iex> %Crux.Structs.Reaction{
...> emoji: %Crux.Structs.Emoji{animated: false, id: 356830260626456586, name: "blobReach"}
...> }
...> |> Crux.Structs.Emoji.to_identifier()
# An already encoded identifier
iex> "👀" |> URI.encode_www_form()
...> |> Crux.Structs.Emoji.to_identifier()
# A custom emoji's identifier
iex> "eyesRight:271412698267254784"
...> |> Crux.Structs.Emoji.to_identifier()
@spec to_identifier(emoji :: Crux.Structs.Emoji.t() | Crux.Structs.Reaction.t() | String.t()) ::
def to_identifier(%Crux.Structs.Reaction{emoji: emoji}), do: to_identifier(emoji)
def to_identifier(%__MODULE__{id: nil, name: name}), do: URI.encode_www_form(name)
def to_identifier(%__MODULE__{id: id, name: name, animated: true}), do: "a:#{name}:#{id}"
def to_identifier(%__MODULE__{id: id, name: name}), do: "#{name}:#{id}"
def to_identifier(identifier) when is_bitstring(identifier), do: identifier
defimpl String.Chars, for: Crux.Structs.Emoji do
@spec to_string(Emoji.t()) :: String.t()
def to_string(%Emoji{id: nil, name: name}), do: name
def to_string(%Emoji{id: id, name: name, animated: true}),
do: "<a:#{name}:#{id}>"
def to_string(%Emoji{id: id, name: name}), do: "<:#{name}:#{id}>"
You can’t perform that action at this time.