From 8fcbd3530d4c29681298f413c2b7e2e9ed601689 Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Sat, 4 May 2024 20:14:56 +0100 Subject: [PATCH] Fix the error caused by trying to cast a datetime to datetime Users have reported a state machine error caused by what looks to be the guild upsert process calling the `Nostrum.Util.maybe_to_datetime` function on a value that is already a datetime. We can resolve this by just returning datetimes as is from that function if we are passed one as an argument. --- lib/nostrum/util.ex | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/nostrum/util.ex b/lib/nostrum/util.ex index 0c5b97d05..92fd235cb 100644 --- a/lib/nostrum/util.ex +++ b/lib/nostrum/util.ex @@ -209,13 +209,19 @@ defmodule Nostrum.Util do end @doc """ - Converts possibly nil ISO8601 timestamp to a `DateTime` + Converts possibly nil ISO8601 timestamp to a `DateTime`. + + If a `DateTime` is provided, return it as-is. """ - @spec maybe_to_datetime(String.t() | nil) :: DateTime.t() | nil + @spec maybe_to_datetime(String.t() | nil | DateTime.t()) :: DateTime.t() | nil def maybe_to_datetime(nil) do nil end + def maybe_to_datetime(%DateTime{} = dt) do + dt + end + def maybe_to_datetime(stamp) do {:ok, casted, 0} = DateTime.from_iso8601(stamp) casted