From 8b200c0fee0cadf65244b1a84ccb6f244b9bfc7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Rom=C3=A1n?= Date: Mon, 6 Dec 2021 08:56:29 +0100 Subject: [PATCH] feat(SnowflakeUtil): add `timestampFrom` (#7058) --- src/structures/ApplicationCommand.js | 2 +- src/structures/BaseGuild.js | 2 +- src/structures/Channel.js | 2 +- src/structures/Emoji.js | 2 +- src/structures/GuildAuditLogs.js | 2 +- src/structures/GuildPreview.js | 2 +- src/structures/Interaction.js | 2 +- src/structures/Message.js | 2 +- src/structures/Role.js | 2 +- src/structures/StageInstance.js | 2 +- src/structures/Sticker.js | 2 +- src/structures/StickerPack.js | 2 +- src/structures/Team.js | 2 +- src/structures/User.js | 2 +- src/structures/Webhook.js | 2 +- src/structures/interfaces/Application.js | 2 +- src/structures/interfaces/TextBasedChannel.js | 2 +- src/util/SnowflakeUtil.js | 9 +++++++++ typings/index.d.ts | 1 + 19 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/structures/ApplicationCommand.js b/src/structures/ApplicationCommand.js index b1a253b32..a3fb51499 100644 --- a/src/structures/ApplicationCommand.js +++ b/src/structures/ApplicationCommand.js @@ -103,7 +103,7 @@ class ApplicationCommand extends Base { * @readonly */ get createdTimestamp() { - return SnowflakeUtil.deconstruct(this.id).timestamp; + return SnowflakeUtil.timestampFrom(this.id); } /** diff --git a/src/structures/BaseGuild.js b/src/structures/BaseGuild.js index 05e029bb7..a39c44e96 100644 --- a/src/structures/BaseGuild.js +++ b/src/structures/BaseGuild.js @@ -43,7 +43,7 @@ class BaseGuild extends Base { * @readonly */ get createdTimestamp() { - return SnowflakeUtil.deconstruct(this.id).timestamp; + return SnowflakeUtil.timestampFrom(this.id); } /** diff --git a/src/structures/Channel.js b/src/structures/Channel.js index 6cc852031..519066fa5 100644 --- a/src/structures/Channel.js +++ b/src/structures/Channel.js @@ -51,7 +51,7 @@ class Channel extends Base { * @readonly */ get createdTimestamp() { - return SnowflakeUtil.deconstruct(this.id).timestamp; + return SnowflakeUtil.timestampFrom(this.id); } /** diff --git a/src/structures/Emoji.js b/src/structures/Emoji.js index 317cf5695..12f909822 100644 --- a/src/structures/Emoji.js +++ b/src/structures/Emoji.js @@ -68,7 +68,7 @@ class Emoji extends Base { * @readonly */ get createdTimestamp() { - return this.id && SnowflakeUtil.deconstruct(this.id).timestamp; + return this.id && SnowflakeUtil.timestampFrom(this.id); } /** diff --git a/src/structures/GuildAuditLogs.js b/src/structures/GuildAuditLogs.js index b323de987..1faa178ff 100644 --- a/src/structures/GuildAuditLogs.js +++ b/src/structures/GuildAuditLogs.js @@ -586,7 +586,7 @@ class GuildAuditLogsEntry { * @readonly */ get createdTimestamp() { - return SnowflakeUtil.deconstruct(this.id).timestamp; + return SnowflakeUtil.timestampFrom(this.id); } /** diff --git a/src/structures/GuildPreview.js b/src/structures/GuildPreview.js index 5652959c8..4627fafa2 100644 --- a/src/structures/GuildPreview.js +++ b/src/structures/GuildPreview.js @@ -110,7 +110,7 @@ class GuildPreview extends Base { * @readonly */ get createdTimestamp() { - return SnowflakeUtil.deconstruct(this.id).timestamp; + return SnowflakeUtil.timestampFrom(this.id); } /** diff --git a/src/structures/Interaction.js b/src/structures/Interaction.js index d82420c06..7e54a64d0 100644 --- a/src/structures/Interaction.js +++ b/src/structures/Interaction.js @@ -82,7 +82,7 @@ class Interaction extends Base { * @readonly */ get createdTimestamp() { - return SnowflakeUtil.deconstruct(this.id).timestamp; + return SnowflakeUtil.timestampFrom(this.id); } /** diff --git a/src/structures/Message.js b/src/structures/Message.js index 622ab22e5..cb9a89f65 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -59,7 +59,7 @@ class Message extends Base { * The timestamp the message was sent at * @type {number} */ - this.createdTimestamp = SnowflakeUtil.deconstruct(this.id).timestamp; + this.createdTimestamp = SnowflakeUtil.timestampFrom(this.id); if ('type' in data) { /** diff --git a/src/structures/Role.js b/src/structures/Role.js index d96ce5704..5712edc53 100644 --- a/src/structures/Role.js +++ b/src/structures/Role.js @@ -134,7 +134,7 @@ class Role extends Base { * @readonly */ get createdTimestamp() { - return SnowflakeUtil.deconstruct(this.id).timestamp; + return SnowflakeUtil.timestampFrom(this.id); } /** diff --git a/src/structures/StageInstance.js b/src/structures/StageInstance.js index e2e89b8c0..9f8bcfd72 100644 --- a/src/structures/StageInstance.js +++ b/src/structures/StageInstance.js @@ -139,7 +139,7 @@ class StageInstance extends Base { * @readonly */ get createdTimestamp() { - return SnowflakeUtil.deconstruct(this.id).timestamp; + return SnowflakeUtil.timestampFrom(this.id); } /** diff --git a/src/structures/Sticker.js b/src/structures/Sticker.js index b699721be..1035a4d96 100644 --- a/src/structures/Sticker.js +++ b/src/structures/Sticker.js @@ -125,7 +125,7 @@ class Sticker extends Base { * @readonly */ get createdTimestamp() { - return SnowflakeUtil.deconstruct(this.id).timestamp; + return SnowflakeUtil.timestampFrom(this.id); } /** diff --git a/src/structures/StickerPack.js b/src/structures/StickerPack.js index f5740efc0..c09af40cb 100644 --- a/src/structures/StickerPack.js +++ b/src/structures/StickerPack.js @@ -61,7 +61,7 @@ class StickerPack extends Base { * @readonly */ get createdTimestamp() { - return SnowflakeUtil.deconstruct(this.id).timestamp; + return SnowflakeUtil.timestampFrom(this.id); } /** diff --git a/src/structures/Team.js b/src/structures/Team.js index 380e60696..ccb817527 100644 --- a/src/structures/Team.js +++ b/src/structures/Team.js @@ -76,7 +76,7 @@ class Team extends Base { * @readonly */ get createdTimestamp() { - return SnowflakeUtil.deconstruct(this.id).timestamp; + return SnowflakeUtil.timestampFrom(this.id); } /** diff --git a/src/structures/User.js b/src/structures/User.js index e9733cf01..a0a4c08e8 100644 --- a/src/structures/User.js +++ b/src/structures/User.js @@ -127,7 +127,7 @@ class User extends Base { * @readonly */ get createdTimestamp() { - return SnowflakeUtil.deconstruct(this.id).timestamp; + return SnowflakeUtil.timestampFrom(this.id); } /** diff --git a/src/structures/Webhook.js b/src/structures/Webhook.js index a10f5d2df..cb541b209 100644 --- a/src/structures/Webhook.js +++ b/src/structures/Webhook.js @@ -379,7 +379,7 @@ class Webhook { * @readonly */ get createdTimestamp() { - return SnowflakeUtil.deconstruct(this.id).timestamp; + return SnowflakeUtil.timestampFrom(this.id); } /** diff --git a/src/structures/interfaces/Application.js b/src/structures/interfaces/Application.js index 56c5392e4..bc3b09c00 100644 --- a/src/structures/interfaces/Application.js +++ b/src/structures/interfaces/Application.js @@ -60,7 +60,7 @@ class Application extends Base { * @readonly */ get createdTimestamp() { - return SnowflakeUtil.deconstruct(this.id).timestamp; + return SnowflakeUtil.timestampFrom(this.id); } /** diff --git a/src/structures/interfaces/TextBasedChannel.js b/src/structures/interfaces/TextBasedChannel.js index e4b18e47b..24948ce7e 100644 --- a/src/structures/interfaces/TextBasedChannel.js +++ b/src/structures/interfaces/TextBasedChannel.js @@ -294,7 +294,7 @@ class TextBasedChannel { if (Array.isArray(messages) || messages instanceof Collection) { let messageIds = messages instanceof Collection ? [...messages.keys()] : messages.map(m => m.id ?? m); if (filterOld) { - messageIds = messageIds.filter(id => Date.now() - SnowflakeUtil.deconstruct(id).timestamp < 1_209_600_000); + messageIds = messageIds.filter(id => Date.now() - SnowflakeUtil.timestampFrom(id) < 1_209_600_000); } if (messageIds.length === 0) return new Collection(); if (messageIds.length === 1) { diff --git a/src/util/SnowflakeUtil.js b/src/util/SnowflakeUtil.js index 640692535..3e89efd72 100644 --- a/src/util/SnowflakeUtil.js +++ b/src/util/SnowflakeUtil.js @@ -70,6 +70,15 @@ class SnowflakeUtil extends null { }; } + /** + * Retrieves the timestamp field's value from a Discord snowflake. + * @param {Snowflake} snowflake Snowflake to get the timestamp value from + * @returns {number} + */ + static timestampFrom(snowflake) { + return Number(BigInt(snowflake) >> 22n) + EPOCH; + } + /** * Discord's epoch value (2015-01-01T00:00:00.000Z). * @type {number} diff --git a/typings/index.d.ts b/typings/index.d.ts index 2d8f3af9f..04ba5c1f8 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1976,6 +1976,7 @@ export class SnowflakeUtil extends null { private constructor(); public static deconstruct(snowflake: Snowflake): DeconstructedSnowflake; public static generate(timestamp?: number | Date): Snowflake; + public static timestampFrom(snowflake: Snowflake): number; public static readonly EPOCH: number; }