feat(SnowflakeUtil): add timestampFrom (#7058)

This commit is contained in:
Antonio Román
2021-12-06 08:56:29 +01:00
committed by GitHub
parent 1c93faa3ab
commit 8b200c0fee
19 changed files with 27 additions and 17 deletions

View File

@@ -103,7 +103,7 @@ class ApplicationCommand extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}
/**

View File

@@ -43,7 +43,7 @@ class BaseGuild extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}
/**

View File

@@ -51,7 +51,7 @@ class Channel extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}
/**

View File

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

View File

@@ -586,7 +586,7 @@ class GuildAuditLogsEntry {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}
/**

View File

@@ -110,7 +110,7 @@ class GuildPreview extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}
/**

View File

@@ -82,7 +82,7 @@ class Interaction extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}
/**

View File

@@ -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) {
/**

View File

@@ -134,7 +134,7 @@ class Role extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}
/**

View File

@@ -139,7 +139,7 @@ class StageInstance extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}
/**

View File

@@ -125,7 +125,7 @@ class Sticker extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}
/**

View File

@@ -61,7 +61,7 @@ class StickerPack extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}
/**

View File

@@ -76,7 +76,7 @@ class Team extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}
/**

View File

@@ -127,7 +127,7 @@ class User extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}
/**

View File

@@ -379,7 +379,7 @@ class Webhook {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}
/**

View File

@@ -60,7 +60,7 @@ class Application extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}
/**

View File

@@ -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) {

View File

@@ -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}

1
typings/index.d.ts vendored
View File

@@ -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;
}