View Source Nostrum.Util (Nostrum v0.10.0)

Utility functions

Summary

Functions

Returns the gateway url and shard count for current websocket connections.

Gets the latencies of all shard connections.

Gets the latency of the shard connection from a Nostrum.Struct.WSState.t() struct.

Updates a map with a new value if the key is present. Otherwise, returns the map unchanged.

Attempts to convert a string to an atom.

Converts possibly nil ISO8601 timestamp to a DateTime.

Converts possibly nil ISO8601 timestamp to unix time.

Returns the number of milliseconds since unix epoch.

Returns the current date as an ISO formatted string.

Returns the total amount of shards as per the configuration.

Converts a map into an atom-keyed map.

Helper function for converting a DateTime to a Snowflake. While allowing Snowflakes to be returned as-is and :infinity is also passed through as-is since it's used as a special value by some cache lookup functions to indicate no upper bound.

Since we're being sacrilegious and converting strings to atoms from the WS, there will be some atoms that we see that aren't defined in any Discord structs. This method mainly serves as a means to define those atoms once so the user isn't warned about them in the Nostrum.Util.maybe_to_atom/1 function when they are in fact harmless.

Returns the number of microseconds since unix epoch.

Functions

Link to this function

enum_to_struct(enum, struct)

View Source
@spec gateway() :: {String.t(), integer()}

Returns the gateway url and shard count for current websocket connections.

If by chance no gateway connection has been made, will fetch the url to use and store it for future use.

Link to this function

get_all_shard_latencies()

View Source
@spec get_all_shard_latencies() :: %{
  required(Nostrum.Struct.WSState.shard_num()) => non_neg_integer() | nil
}

Gets the latencies of all shard connections.

Calls get_shard_latency/1 on all shards and returns a map whose keys are shard nums and whose values are latencies in milliseconds.

Link to this function

get_shard_latency(state)

View Source
@spec get_shard_latency(Nostrum.Struct.WSState.t()) :: non_neg_integer() | nil

Gets the latency of the shard connection from a Nostrum.Struct.WSState.t() struct.

Returns the latency in milliseconds as an integer, returning nil if unknown.

Link to this function

map_update_if_present(map, key, fun)

View Source
@spec map_update_if_present(map(), term(), (term() -> term())) :: map()

Updates a map with a new value if the key is present. Otherwise, returns the map unchanged.

@spec maybe_to_atom(atom() | String.t() | integer()) :: atom() | integer()

Attempts to convert a string to an atom.

Binary tokens that consist of digits are assumed to be snowflakes, and will be parsed as such.

Some maps sent from Discord are integer-indexed, for these we just return the integer provided.

If atom does not currently exist, will warn that we're doing an unsafe conversion.

@spec maybe_to_datetime(String.t() | nil | DateTime.t()) :: DateTime.t() | nil

Converts possibly nil ISO8601 timestamp to a DateTime.

If a DateTime is provided, return it as-is.

Link to this function

maybe_to_unixtime(stamp)

View Source
@spec maybe_to_unixtime(String.t() | nil) :: pos_integer() | nil

Converts possibly nil ISO8601 timestamp to unix time.

@spec now() :: integer()

Returns the number of milliseconds since unix epoch.

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

Returns the current date as an ISO formatted string.

@spec num_shards() :: pos_integer()

Returns the total amount of shards as per the configuration.

Return value

  • If you specified your shards as :auto, the return value will be the recommended number of shards as given by the gateway.

  • If you explicitly specified your shard numbers as an integer, it will be the given number.

  • If you specified your shards in the form {lowest, highest, total} to start a specific range of the total shards you want to start, this will be the total value.

Should Discord not supply us with any shard information, this will return 1.

Note that this is not the number of currently active shards, but the number of shards specified in your config.

@spec safe_atom_map(map()) :: map()

Converts a map into an atom-keyed map.

Given a map with variable type keys, returns the same map with all keys as atoms. To support maps keyed with integers (such as in Discord's interaction data), binaries that appear to be integers will be parsed as such.

This function will attempt to convert keys to an existing atom, and if that fails will default to creating a new atom while displaying a warning. The idea here is that we should be able to see if any results from Discord are giving variable keys. Since we will define all types of objects returned by Discord, the amount of new atoms created SHOULD be 0. 👀

Link to this function

timestamp_like_to_snowflake(snowflake)

View Source

Helper function for converting a DateTime to a Snowflake. While allowing Snowflakes to be returned as-is and :infinity is also passed through as-is since it's used as a special value by some cache lookup functions to indicate no upper bound.

Since we're being sacrilegious and converting strings to atoms from the WS, there will be some atoms that we see that aren't defined in any Discord structs. This method mainly serves as a means to define those atoms once so the user isn't warned about them in the Nostrum.Util.maybe_to_atom/1 function when they are in fact harmless.

The function is public to prevent it from being optimized out at compile time.

@spec usec_now() :: integer()

Returns the number of microseconds since unix epoch.