Skip to content

Commit

Permalink
refactor(Dates): save timestamps everywhere and use Date.parse (#7108)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaporoxx committed Jan 8, 2022
1 parent d06d70c commit 55e21f5
Show file tree
Hide file tree
Showing 18 changed files with 60 additions and 61 deletions.
18 changes: 9 additions & 9 deletions packages/discord.js/src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions packages/discord.js/src/rest/RequestHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ function parseResponse(res) {
}

function getAPIOffset(serverDate) {
return new Date(serverDate).getTime() - Date.now();
return Date.parse(serverDate) - Date.now();
}

function calculateReset(reset, resetAfter, serverDate) {
// Use direct reset time when available, server date becomes irrelevant in this case
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.
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/src/structures/BaseGuildTextChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/src/structures/DMChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/src/structures/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions packages/discord.js/src/structures/GuildMember.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
28 changes: 14 additions & 14 deletions packages/discord.js/src/structures/GuildTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

/**
Expand Down
9 changes: 4 additions & 5 deletions packages/discord.js/src/structures/Invite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/discord.js/src/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions packages/discord.js/src/structures/MessageEmbed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -240,7 +241,7 @@ class MessageEmbed {
* @readonly
*/
get createdAt() {
return this.timestamp ? new Date(this.timestamp) : null;
return this.timestamp && new Date(this.timestamp);
}

/**
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/src/structures/Presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
7 changes: 3 additions & 4 deletions packages/discord.js/src/structures/ThreadChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ThreadChannel extends Channel {
* created</info>
* @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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/discord.js/src/structures/ThreadMember.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
/**
Expand All @@ -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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/src/structures/VoiceState.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class TextBasedChannel {
* @readonly
*/
get lastPinAt() {
return this.lastPinTimestamp ? new Date(this.lastPinTimestamp) : null;
return this.lastPinTimestamp && new Date(this.lastPinTimestamp);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,8 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
public readonly emojis: BaseGuildEmojiManager;
public guilds: GuildManager;
public options: ClientOptions;
public readyAt: If<Ready, Date>;
public readonly readyTimestamp: If<Ready, number>;
public readonly readyAt: If<Ready, Date>;
public readyTimestamp: If<Ready, number>;
public sweepers: Sweepers;
public shard: ShardClientUtil | null;
public token: If<Ready, string, string | null>;
Expand Down Expand Up @@ -1200,17 +1200,17 @@ export class GuildScheduledEvent<S extends GuildScheduledEventStatus = GuildSche

export class GuildTemplate extends Base {
private constructor(client: Client, data: RawGuildTemplateData);
public readonly createdTimestamp: number;
public readonly updatedTimestamp: number;
public createdTimestamp: number;
public updatedTimestamp: number;
public readonly url: string;
public code: string;
public name: string;
public description: string | null;
public usageCount: number;
public creator: User;
public creatorId: Snowflake;
public createdAt: Date;
public updatedAt: Date;
public readonly createdAt: Date;
public readonly updatedAt: Date;
public guild: Guild | null;
public guildId: Snowflake;
public serializedGuild: APITemplateSerializedSourceGuild;
Expand Down

0 comments on commit 55e21f5

Please sign in to comment.