From 55e21f53663a91863c63b6d9f3a8c35564664061 Mon Sep 17 00:00:00 2001 From: Jan <66554238+vaporox@users.noreply.github.com> Date: Sat, 8 Jan 2022 12:41:20 +0100 Subject: [PATCH] refactor(Dates): save timestamps everywhere and use Date.parse (#7108) --- packages/discord.js/src/client/Client.js | 18 ++++++------ .../src/client/websocket/WebSocketManager.js | 2 +- .../websocket/handlers/CHANNEL_PINS_UPDATE.js | 2 +- .../discord.js/src/rest/RequestHandler.js | 6 ++-- .../src/structures/BaseGuildTextChannel.js | 2 +- .../discord.js/src/structures/DMChannel.js | 2 +- packages/discord.js/src/structures/Guild.js | 2 +- .../discord.js/src/structures/GuildMember.js | 8 +++--- .../src/structures/GuildTemplate.js | 28 +++++++++---------- packages/discord.js/src/structures/Invite.js | 9 +++--- packages/discord.js/src/structures/Message.js | 8 +++--- .../discord.js/src/structures/MessageEmbed.js | 5 ++-- .../discord.js/src/structures/Presence.js | 2 +- .../src/structures/ThreadChannel.js | 7 ++--- .../discord.js/src/structures/ThreadMember.js | 4 +-- .../discord.js/src/structures/VoiceState.js | 2 +- .../structures/interfaces/TextBasedChannel.js | 2 +- packages/discord.js/typings/index.d.ts | 12 ++++---- 18 files changed, 60 insertions(+), 61 deletions(-) diff --git a/packages/discord.js/src/client/Client.js b/packages/discord.js/src/client/Client.js index 95792ee9f75a..484bf36caa11 100644 --- a/packages/discord.js/src/client/Client.js +++ b/packages/discord.js/src/client/Client.js @@ -162,11 +162,10 @@ class Client extends BaseClient { this.application = null; /** - * Time at which the client was last regarded as being in the `READY` state - * (each time the client disconnects and successfully reconnects, this will be overwritten) - * @type {?Date} + * Timestamp of the time the client was last `READY` at + * @type {?number} */ - this.readyAt = null; + this.readyTimestamp = null; } /** @@ -183,12 +182,13 @@ class Client extends BaseClient { } /** - * Timestamp of the time the client was last `READY` at - * @type {?number} + * Time at which the client was last regarded as being in the `READY` state + * (each time the client disconnects and successfully reconnects, this will be overwritten) + * @type {?Date} * @readonly */ - get readyTimestamp() { - return this.readyAt?.getTime() ?? null; + get readyAt() { + return this.readyTimestamp && new Date(this.readyTimestamp); } /** @@ -197,7 +197,7 @@ class Client extends BaseClient { * @readonly */ get uptime() { - return this.readyAt ? Date.now() - this.readyAt : null; + return this.readyTimestamp && Date.now() - this.readyTimestamp; } /** diff --git a/packages/discord.js/src/client/websocket/WebSocketManager.js b/packages/discord.js/src/client/websocket/WebSocketManager.js index 65a23f96e338..e66696744007 100644 --- a/packages/discord.js/src/client/websocket/WebSocketManager.js +++ b/packages/discord.js/src/client/websocket/WebSocketManager.js @@ -374,7 +374,7 @@ class WebSocketManager extends EventEmitter { triggerClientReady() { this.status = Status.READY; - this.client.readyAt = new Date(); + this.client.readyTimestamp = Date.now(); /** * Emitted when the client becomes ready to start working. diff --git a/packages/discord.js/src/client/websocket/handlers/CHANNEL_PINS_UPDATE.js b/packages/discord.js/src/client/websocket/handlers/CHANNEL_PINS_UPDATE.js index b49e311f4af5..a0f99b39980d 100644 --- a/packages/discord.js/src/client/websocket/handlers/CHANNEL_PINS_UPDATE.js +++ b/packages/discord.js/src/client/websocket/handlers/CHANNEL_PINS_UPDATE.js @@ -4,7 +4,7 @@ const { Events } = require('../../../util/Constants'); module.exports = (client, { d: data }) => { const channel = client.channels.cache.get(data.channel_id); - const time = data.last_pin_timestamp ? new Date(data.last_pin_timestamp).getTime() : null; + const time = data.last_pin_timestamp ? Date.parse(data.last_pin_timestamp) : null; if (channel) { // Discord sends null for last_pin_timestamp if the last pinned message was removed diff --git a/packages/discord.js/src/rest/RequestHandler.js b/packages/discord.js/src/rest/RequestHandler.js index 355e0ea9a49c..d41e2a4179b5 100644 --- a/packages/discord.js/src/rest/RequestHandler.js +++ b/packages/discord.js/src/rest/RequestHandler.js @@ -16,7 +16,7 @@ function parseResponse(res) { } function getAPIOffset(serverDate) { - return new Date(serverDate).getTime() - Date.now(); + return Date.parse(serverDate) - Date.now(); } function calculateReset(reset, resetAfter, serverDate) { @@ -24,7 +24,7 @@ function calculateReset(reset, resetAfter, serverDate) { if (resetAfter) { return Date.now() + Number(resetAfter) * 1_000; } - return new Date(Number(reset) * 1_000).getTime() - getAPIOffset(serverDate); + return Number(reset) * 1_000 - getAPIOffset(serverDate); } /* Invalid request limiting is done on a per-IP basis, not a per-token basis. @@ -242,7 +242,7 @@ class RequestHandler { // https://github.com/discord/discord-api-docs/issues/182 if (!resetAfter && request.route.includes('reactions')) { - this.reset = new Date(serverDate).getTime() - getAPIOffset(serverDate) + 250; + this.reset = Date.parse(serverDate) - getAPIOffset(serverDate) + 250; } // Handle retryAfter, which means we have actually hit a rate limit diff --git a/packages/discord.js/src/structures/BaseGuildTextChannel.js b/packages/discord.js/src/structures/BaseGuildTextChannel.js index abb407563bde..efa139d9ca9e 100644 --- a/packages/discord.js/src/structures/BaseGuildTextChannel.js +++ b/packages/discord.js/src/structures/BaseGuildTextChannel.js @@ -66,7 +66,7 @@ class BaseGuildTextChannel extends GuildChannel { * The timestamp when the last pinned message was pinned, if there was one * @type {?number} */ - this.lastPinTimestamp = data.last_pin_timestamp ? new Date(data.last_pin_timestamp).getTime() : null; + this.lastPinTimestamp = data.last_pin_timestamp ? Date.parse(data.last_pin_timestamp) : null; } if ('default_auto_archive_duration' in data) { diff --git a/packages/discord.js/src/structures/DMChannel.js b/packages/discord.js/src/structures/DMChannel.js index 555de4f40e0a..cc1a018df62d 100644 --- a/packages/discord.js/src/structures/DMChannel.js +++ b/packages/discord.js/src/structures/DMChannel.js @@ -47,7 +47,7 @@ class DMChannel extends Channel { * The timestamp when the last pinned message was pinned, if there was one * @type {?number} */ - this.lastPinTimestamp = new Date(data.last_pin_timestamp).getTime(); + this.lastPinTimestamp = Date.parse(data.last_pin_timestamp); } else { this.lastPinTimestamp ??= null; } diff --git a/packages/discord.js/src/structures/Guild.js b/packages/discord.js/src/structures/Guild.js index c0d9d2f4dc8b..bdf0715aa889 100644 --- a/packages/discord.js/src/structures/Guild.js +++ b/packages/discord.js/src/structures/Guild.js @@ -329,7 +329,7 @@ class Guild extends AnonymousGuild { * The timestamp the client user joined the guild at * @type {number} */ - this.joinedTimestamp = new Date(data.joined_at).getTime(); + this.joinedTimestamp = Date.parse(data.joined_at); } if ('default_message_notifications' in data) { diff --git a/packages/discord.js/src/structures/GuildMember.js b/packages/discord.js/src/structures/GuildMember.js index f271def0e169..235ee9e3deaa 100644 --- a/packages/discord.js/src/structures/GuildMember.js +++ b/packages/discord.js/src/structures/GuildMember.js @@ -84,9 +84,9 @@ class GuildMember extends Base { } else if (typeof this.avatar !== 'string') { this.avatar = null; } - if ('joined_at' in data) this.joinedTimestamp = new Date(data.joined_at).getTime(); + if ('joined_at' in data) this.joinedTimestamp = Date.parse(data.joined_at); if ('premium_since' in data) { - this.premiumSinceTimestamp = data.premium_since ? new Date(data.premium_since).getTime() : null; + this.premiumSinceTimestamp = data.premium_since ? Date.parse(data.premium_since) : null; } if ('roles' in data) this._roles = data.roles; this.pending = data.pending ?? false; @@ -186,7 +186,7 @@ class GuildMember extends Base { * @readonly */ get joinedAt() { - return this.joinedTimestamp ? new Date(this.joinedTimestamp) : null; + return this.joinedTimestamp && new Date(this.joinedTimestamp); } /** @@ -204,7 +204,7 @@ class GuildMember extends Base { * @readonly */ get premiumSince() { - return this.premiumSinceTimestamp ? new Date(this.premiumSinceTimestamp) : null; + return this.premiumSinceTimestamp && new Date(this.premiumSinceTimestamp); } /** diff --git a/packages/discord.js/src/structures/GuildTemplate.js b/packages/discord.js/src/structures/GuildTemplate.js index 08f327dfd8b5..800a19cd70b0 100644 --- a/packages/discord.js/src/structures/GuildTemplate.js +++ b/packages/discord.js/src/structures/GuildTemplate.js @@ -66,18 +66,18 @@ class GuildTemplate extends Base { if ('created_at' in data) { /** - * The time when this template was created at - * @type {Date} + * The timestamp of when this template was created at + * @type {number} */ - this.createdAt = new Date(data.created_at); + this.createdTimestamp = Date.parse(data.created_at); } if ('updated_at' in data) { /** - * The time when this template was last synced to the guild - * @type {Date} + * The timestamp of when this template was last synced to the guild + * @type {number} */ - this.updatedAt = new Date(data.updated_at); + this.updatedTimestamp = Date.parse(data.updated_at); } if ('source_guild_id' in data) { @@ -180,21 +180,21 @@ class GuildTemplate extends Base { } /** - * The timestamp of when this template was created at - * @type {number} + * The time when this template was created at + * @type {Date} * @readonly */ - get createdTimestamp() { - return this.createdAt.getTime(); + get createdAt() { + return new Date(this.createdTimestamp); } /** - * The timestamp of when this template was last synced to the guild - * @type {number} + * The time when this template was last synced to the guild + * @type {Date} * @readonly */ - get updatedTimestamp() { - return this.updatedAt.getTime(); + get updatedAt() { + return new Date(this.updatedTimestamp); } /** diff --git a/packages/discord.js/src/structures/Invite.js b/packages/discord.js/src/structures/Invite.js index 0ed8b45e24b7..17ee9da0a402 100644 --- a/packages/discord.js/src/structures/Invite.js +++ b/packages/discord.js/src/structures/Invite.js @@ -192,12 +192,12 @@ class Invite extends Base { * The timestamp this invite was created at * @type {?number} */ - this.createdTimestamp = new Date(data.created_at).getTime(); + this.createdTimestamp = Date.parse(data.created_at); } else { this.createdTimestamp ??= null; } - if ('expires_at' in data) this._expiresTimestamp = new Date(data.expires_at).getTime(); + if ('expires_at' in data) this._expiresTimestamp = Date.parse(data.expires_at); else this._expiresTimestamp ??= null; if ('stage_instance' in data) { @@ -227,7 +227,7 @@ class Invite extends Base { * @readonly */ get createdAt() { - return this.createdTimestamp ? new Date(this.createdTimestamp) : null; + return this.createdTimestamp && new Date(this.createdTimestamp); } /** @@ -263,8 +263,7 @@ class Invite extends Base { * @readonly */ get expiresAt() { - const { expiresTimestamp } = this; - return expiresTimestamp ? new Date(expiresTimestamp) : null; + return this.expiresTimestamp && new Date(this.expiresTimestamp); } /** diff --git a/packages/discord.js/src/structures/Message.js b/packages/discord.js/src/structures/Message.js index d02f3fc30453..af8a8c3d6451 100644 --- a/packages/discord.js/src/structures/Message.js +++ b/packages/discord.js/src/structures/Message.js @@ -186,7 +186,7 @@ class Message extends Base { * The timestamp the message was last edited at (if applicable) * @type {?number} */ - this.editedTimestamp = new Date(data.edited_timestamp).getTime(); + this.editedTimestamp = Date.parse(data.edited_timestamp); } else { this.editedTimestamp ??= null; } @@ -424,7 +424,7 @@ class Message extends Base { * @readonly */ get editedAt() { - return this.editedTimestamp ? new Date(this.editedTimestamp) : null; + return this.editedTimestamp && new Date(this.editedTimestamp); } /** @@ -943,8 +943,8 @@ class Message extends Base { if (equal && rawData) { equal = this.mentions.everyone === message.mentions.everyone && - this.createdTimestamp === new Date(rawData.timestamp).getTime() && - this.editedTimestamp === new Date(rawData.edited_timestamp).getTime(); + this.createdTimestamp === Date.parse(rawData.timestamp) && + this.editedTimestamp === Date.parse(rawData.edited_timestamp); } return equal; diff --git a/packages/discord.js/src/structures/MessageEmbed.js b/packages/discord.js/src/structures/MessageEmbed.js index 39fef4492237..7c1b13e05970 100644 --- a/packages/discord.js/src/structures/MessageEmbed.js +++ b/packages/discord.js/src/structures/MessageEmbed.js @@ -87,6 +87,7 @@ class MessageEmbed { * The timestamp of this embed * @type {?number} */ + // Date.parse() cannot be used here because data.timestamp might be a number this.timestamp = 'timestamp' in data ? new Date(data.timestamp).getTime() : null; /** @@ -240,7 +241,7 @@ class MessageEmbed { * @readonly */ get createdAt() { - return this.timestamp ? new Date(this.timestamp) : null; + return this.timestamp && new Date(this.timestamp); } /** @@ -514,7 +515,7 @@ class MessageEmbed { type: 'rich', description: this.description, url: this.url, - timestamp: this.timestamp && new Date(this.timestamp), + timestamp: this.createdAt?.toISOString(), color: this.color, fields: this.fields, thumbnail: this.thumbnail, diff --git a/packages/discord.js/src/structures/Presence.js b/packages/discord.js/src/structures/Presence.js index 744450f7e5c2..35fdb8d029b9 100644 --- a/packages/discord.js/src/structures/Presence.js +++ b/packages/discord.js/src/structures/Presence.js @@ -271,7 +271,7 @@ class Activity { * Creation date of the activity * @type {number} */ - this.createdTimestamp = new Date(data.created_at).getTime(); + this.createdTimestamp = Date.parse(data.created_at); } /** diff --git a/packages/discord.js/src/structures/ThreadChannel.js b/packages/discord.js/src/structures/ThreadChannel.js index 7c45cbc327dd..3d26f491da38 100644 --- a/packages/discord.js/src/structures/ThreadChannel.js +++ b/packages/discord.js/src/structures/ThreadChannel.js @@ -99,7 +99,7 @@ class ThreadChannel extends Channel { * created * @type {?number} */ - this.archiveTimestamp = new Date(data.thread_metadata.archive_timestamp).getTime(); + this.archiveTimestamp = Date.parse(data.thread_metadata.archive_timestamp); } else { this.locked ??= null; this.archived ??= null; @@ -133,7 +133,7 @@ class ThreadChannel extends Channel { * The timestamp when the last pinned message was pinned, if there was one * @type {?number} */ - this.lastPinTimestamp = data.last_pin_timestamp ? new Date(data.last_pin_timestamp).getTime() : null; + this.lastPinTimestamp = data.last_pin_timestamp ? Date.parse(data.last_pin_timestamp) : null; } else { this.lastPinTimestamp ??= null; } @@ -192,8 +192,7 @@ class ThreadChannel extends Channel { * @readonly */ get archivedAt() { - if (!this.archiveTimestamp) return null; - return new Date(this.archiveTimestamp); + return this.archiveTimestamp && new Date(this.archiveTimestamp); } /** diff --git a/packages/discord.js/src/structures/ThreadMember.js b/packages/discord.js/src/structures/ThreadMember.js index 3dcf3dad7755..b925e4e1d3af 100644 --- a/packages/discord.js/src/structures/ThreadMember.js +++ b/packages/discord.js/src/structures/ThreadMember.js @@ -33,7 +33,7 @@ class ThreadMember extends Base { } _patch(data) { - if ('join_timestamp' in data) this.joinedTimestamp = new Date(data.join_timestamp).getTime(); + if ('join_timestamp' in data) this.joinedTimestamp = Date.parse(data.join_timestamp); if ('flags' in data) { /** @@ -59,7 +59,7 @@ class ThreadMember extends Base { * @readonly */ get joinedAt() { - return this.joinedTimestamp ? new Date(this.joinedTimestamp) : null; + return this.joinedTimestamp && new Date(this.joinedTimestamp); } /** diff --git a/packages/discord.js/src/structures/VoiceState.js b/packages/discord.js/src/structures/VoiceState.js index 7ed9ee752133..06cc8cdaddd9 100644 --- a/packages/discord.js/src/structures/VoiceState.js +++ b/packages/discord.js/src/structures/VoiceState.js @@ -120,7 +120,7 @@ class VoiceState extends Base { * The time at which the member requested to speak. This property is specific to stage channels only. * @type {?number} */ - this.requestToSpeakTimestamp = new Date(data.request_to_speak_timestamp).getTime(); + this.requestToSpeakTimestamp = Date.parse(data.request_to_speak_timestamp); } else { this.requestToSpeakTimestamp ??= null; } diff --git a/packages/discord.js/src/structures/interfaces/TextBasedChannel.js b/packages/discord.js/src/structures/interfaces/TextBasedChannel.js index a8b83149772b..54bb7955947d 100644 --- a/packages/discord.js/src/structures/interfaces/TextBasedChannel.js +++ b/packages/discord.js/src/structures/interfaces/TextBasedChannel.js @@ -49,7 +49,7 @@ class TextBasedChannel { * @readonly */ get lastPinAt() { - return this.lastPinTimestamp ? new Date(this.lastPinTimestamp) : null; + return this.lastPinTimestamp && new Date(this.lastPinTimestamp); } /** diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 9d8c4a7e3449..52531b8472f3 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -545,8 +545,8 @@ export class Client extends BaseClient { public readonly emojis: BaseGuildEmojiManager; public guilds: GuildManager; public options: ClientOptions; - public readyAt: If; - public readonly readyTimestamp: If; + public readonly readyAt: If; + public readyTimestamp: If; public sweepers: Sweepers; public shard: ShardClientUtil | null; public token: If; @@ -1200,8 +1200,8 @@ export class GuildScheduledEvent