Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Don't attempt to convert integers to atoms in map casting #572

Merged
merged 1 commit into from
May 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/nostrum/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,19 @@ defmodule Nostrum.Util do
Binary `token`s 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_atom(atom | String.t()) :: atom | integer
@spec maybe_to_atom(atom | String.t() | integer) :: atom | integer
def maybe_to_atom(token) when is_atom(token), do: token

def maybe_to_atom(<<head, _rest::binary>> = token) when head in ?1..?9 do
def maybe_to_atom(token) when is_integer(token), do: token

# We include a check for zero in this overload for 0 in case we have an integer
# indexed map, the variable is still named snowflake for brevity.
def maybe_to_atom(<<head, _rest::binary>> = token) when head in ?0..?9 do
case Integer.parse(token) do
{snowflake, ""} ->
snowflake
Expand Down
Loading