Using timestamp with EmbedBuilder: RangeError: Invalid time value
See original GitHub issueWhich package is this bug report for?
discord.js
Issue description
I am passing a Date
object into an EmbedBuilder
constructor’s timestamp
field. That results in the following error:
RangeError: Invalid time value
at Date.toISOString (<anonymous>)
at new EmbedBuilder (S:\dev\git\github\obs-webserver\node_modules\@discordjs\builders\dist\index.js:186:54)
at new EmbedBuilder (S:\dev\git\github\obs-webserver\node_modules\discord.js\src\structures\EmbedBuilder.js:13:5)
A little digging shows that the Date
object gets eaten and turned into an empty object {}
by the function toSnakeCase()
in EmbedBuilder.js.
Workaround is to either create the embed without the timestamp and then use .setTimestamp()
with the Date
object on the resulting embed object, or use .toISOString()
on the date first. Workaround was not required for versions earlier than v14.0 when this class was MessageEmbed
.
Code sample
> const DiscordJs = require("discord.js")
undefined
> const embedBroken = new DiscordJs.EmbedBuilder({timestamp: new Date()})
Uncaught RangeError: Invalid time value
at Date.toISOString (<anonymous>)
at new EmbedBuilder (S:\dev\git\github\obs-webserver\node_modules\@discordjs\builders\dist\index.js:186:54)
at new EmbedBuilder (S:\dev\git\github\obs-webserver\node_modules\discord.js\src\structures\EmbedBuilder.js:13:5)
> const embedWorkaround = new DiscordJs.EmbedBuilder({timestamp: new Date().toISOString()})
undefined
> embedWorkaround
EmbedBuilder { data: { timestamp: '2022-07-19T20:34:58.548Z' } }
> const embedWorkaround2 = new DiscordJs.EmbedBuilder()
undefined
> embedWorkaround2.setTimestamp(new Date())
EmbedBuilder { data: { timestamp: '2022-07-19T20:35:09.295Z' } }
>
Package version
14.0.3
Node.js version
18.6.0
Operating system
Windows 11
Priority this issue should have
Medium (should be fixed soon)
Which partials do you have configured?
Not applicable (subpackage bug)
Which gateway intents are you subscribing to?
Not applicable (subpackage bug)
I have tested this issue on a development release
No response
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
RangeError: Invalid time value - javascript - Stack Overflow
This exception occurs when the Date object contains an invalid date. ... make sure that the timestamp variable contains a valid date string....
Read more >RangeError: Invalid time value in JavaScript | bobbyhadz
The "Uncaught RangeError Invalid time value" error occurs when calling a method on an invalid date, e.g. `new Date('asdf').toISOString()`.
Read more >JnD2nSMLlD | SourceBin
RangeError : Invalid time value. at Date.toISOString (<anonymous>). at EmbedBuilder.setTimestamp ...
Read more >RangeError: Invalid time value on 'date-fns' functions... Stumped.
I have two nearly identical React components(edit: also using NextJS) that don't behave as expected. My Post component displays it's date as ...
Read more >RangeError: invalid date - JavaScript - MDN Web Docs
The JavaScript exception "invalid date" occurs when a string leading ... RangeError: Invalid time value (V8-based) RangeError: invalid date ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
While
APIEmbed
is a possible type for the argument, it is not the only one defined. The typings defines theEmbedBuilder
constructor as follows:This should allow for either
APIEmbed
orEmbedData
. And then,EmbedData
definestimestamp
as follows:Date
is a possible type for the value here.It is entirely possible that a breaking change was intended here, but it’s not discussed in the v13 to v14 migration guide. This leads me to believe that a Date was intended to be accepted here, but the change to the embeds broke this particular use case.
I can work on this issue, @Jiralite . It’ll be my first contribution and i think this issue is quite beginner friendly.