View Source Nostrum.Snowflake (Nostrum v0.9.0)

Functions that work on Snowflakes.

Summary

Types

The type that represents snowflakes in JSON.

t()

The snowflake type.

Functions

Attempts to convert a term into a snowflake.

Same as cast/1, except it raises an ArgumentError on failure.

Returns the creation time of the snowflake.

Convert a snowflake into its external representation.

Converts the given datetime into a snowflake.

Same as from_datetime/1, except it raises an ArgumentError on failure.

Returns true if term is a snowflake; otherwise returns false.

Types

@type external_snowflake() :: String.t()

The type that represents snowflakes in JSON.

In JSON, Snowflakes are typically represented as strings due to some languages not being able to represent such a large number.

@type t() :: 0..18_446_744_073_709_551_615

The snowflake type.

Snowflakes are 64-bit unsigned integers used to represent discord object ids.

Functions

@spec cast(term()) :: {:ok, t() | nil} | :error

Attempts to convert a term into a snowflake.

Examples

iex> Nostrum.Snowflake.cast(200317799350927360)
{:ok, 200317799350927360}

iex> Nostrum.Snowflake.cast("200317799350927360")
{:ok, 200317799350927360}

iex> Nostrum.Snowflake.cast(nil)
{:ok, nil}

iex> Nostrum.Snowflake.cast(true)
:error

iex> Nostrum.Snowflake.cast(-1)
:error
@spec cast!(term()) :: t() | nil | no_return()

Same as cast/1, except it raises an ArgumentError on failure.

Link to this function

creation_time(snowflake)

View Source
@spec creation_time(t()) :: DateTime.t()

Returns the creation time of the snowflake.

Examples

iex> Nostrum.Snowflake.creation_time(177888205536886784)
~U[2016-05-05 21:04:13.203Z]
@spec dump(t()) :: external_snowflake()

Convert a snowflake into its external representation.

Examples

iex> Nostrum.Snowflake.dump(109112383011581952)
"109112383011581952"
@spec from_datetime(DateTime.t()) :: {:ok, t()} | :error

Converts the given datetime into a snowflake.

If datetime occurred before the discord epoch, the function will return :error.

The converted snowflake's last 22 bits will be zeroed out due to missing data.

Examples

iex> {:ok, dt, _} = DateTime.from_iso8601("2016-05-05T21:04:13.203Z")
iex> Nostrum.Snowflake.from_datetime(dt)
{:ok, 177888205536755712}

iex> {:ok, dt, _} = DateTime.from_iso8601("1998-12-25T00:00:00.000Z")
iex> Nostrum.Snowflake.from_datetime(dt)
:error
Link to this function

from_datetime!(datetime)

View Source
@spec from_datetime!(DateTime.t()) :: t() | no_return()

Same as from_datetime/1, except it raises an ArgumentError on failure.

Link to this macro

is_snowflake(term)

View Source (macro)

Returns true if term is a snowflake; otherwise returns false.

Examples

iex> Nostrum.Snowflake.is_snowflake(89918932789497856)
true

iex> Nostrum.Snowflake.is_snowflake(-1)
false

iex> Nostrum.Snowflake.is_snowflake(0xFFFFFFFFFFFFFFFF + 1)
false

iex> Nostrum.Snowflake.is_snowflake("117789813427535878")
false