Crux v2019-09-15 12:18:11Z Crux.Structs.Snowflake View Source

Custom non discord api struct to help with working with Discord's snowflakes.

For more information see Discord Docs.

Link to this section Summary

Types

All valid types that can be resolved into a t/0.

t()

A discord snowflake, an unsigned 64 bit integer.

Functions

Constructs a t/0 from its Crux.Structs.Snowflake.Parts.t/0 or a keyword of its fields.

The discord epoch, the first second of 2015 or 1420070400000.

Returns true if term is a t/0; otherwise returns false..

Converts a String.t/0 to a t/0 while allowing t/0 to pass through.

Converts a String.t/0 to a t/0 while allowing t/0 and nil to pass through.

Link to this section Types

Link to this type

resolvable() View Source (since 0.2.1)
resolvable() :: String.t() | t()

All valid types that can be resolved into a t/0.

Link to this type

t() View Source (since 0.2.1)
t() :: 0..18_446_744_073_709_551_615

A discord snowflake, an unsigned 64 bit integer.

Link to this section Functions

Link to this function

construct(parts) View Source (since 0.2.1)

Constructs a t/0 from its Crux.Structs.Snowflake.Parts.t/0 or a keyword of its fields.

iex> %Crux.Structs.Snowflake.Parts{increment: 0, process_id: 0, timestamp: 1472128634889, worker_id: 1}
...> |> Crux.Structs.Snowflake.construct()
218348062828003328

iex> Crux.Structs.Snowflake.construct(increment: 1, timestamp: 1451106635493)
130175406673231873

iex> Crux.Structs.Snowflake.construct(timestamp: Crux.Structs.Snowflake.discord_epoch())
0
Link to this function

deconstruct(snowflake) View Source (since 0.2.1)
deconstruct(t()) :: Crux.Structs.Snowflake.Parts.t()

Deconstructs a t/0 to its Crux.Structs.Snowflake.Parts.t/0.

iex> Crux.Structs.Snowflake.deconstruct(218348062828003328)
%Crux.Structs.Snowflake.Parts{
  increment: 0,
  process_id: 0,
  timestamp: 1472128634889,
  worker_id: 1
}
Link to this function

discord_epoch() View Source (since 0.2.1)
discord_epoch() :: non_neg_integer()

The discord epoch, the first second of 2015 or 1420070400000.

iex> Crux.Structs.Snowflake.discord_epoch()
1_420_070_400_000
Link to this function

from_integer(snowflake) View Source (since 0.2.1)
from_integer(t()) :: Crux.Structs.Snowflake.Parts.t()

Deconstructs a t/0 to its Crux.Structs.Snowflake.Parts.t/0.

Link to this macro

is_snowflake(snowflake) View Source (macro) (since 0.2.1)

Returns true if term is a t/0; otherwise returns false..

Link to this function

parse(snowflake) View Source (since 0.2.1)
parse(t()) :: t()
parse(String.t()) :: t() | :error

Converts a String.t/0 to a t/0 while allowing t/0 to pass through.

Returns :error if the provided string is not a t/0.

  iex> Crux.Structs.Snowflake.parse("invalid")
  :error

  iex> Crux.Structs.Snowflake.parse(218348062828003328)
  218348062828003328

  # Fallbacks
  iex> Crux.Structs.Snowflake.parse("218348062828003328")
  218348062828003328
Link to this function

to_integer(t) View Source (since 0.2.1)
to_integer(Crux.Structs.Snowflake.Parts.t()) :: t()

Constructs a t/0 from its Crux.Structs.Snowflake.Parts.t/0.

Link to this function

to_snowflake(snowflake) View Source (since 0.2.1)
to_snowflake(t()) :: t()
to_snowflake(String.t()) :: t() | no_return()
to_snowflake(nil) :: nil

Converts a String.t/0 to a t/0 while allowing t/0 and nil to pass through.

Raises an ArgumentError if the provided string is not an integer.

  iex> Crux.Structs.Snowflake.to_snowflake(218348062828003328)
  218348062828003328

  # Fallbacks
  iex> Crux.Structs.Snowflake.to_snowflake("218348062828003328")
  218348062828003328

  iex> Crux.Structs.Snowflake.to_snowflake(nil)
  nil