mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
feat(SnowflakeUtil): add timestampFrom (#7058)
This commit is contained in:
@@ -103,7 +103,7 @@ class ApplicationCommand extends Base {
|
||||
* @readonly
|
||||
*/
|
||||
get createdTimestamp() {
|
||||
return SnowflakeUtil.deconstruct(this.id).timestamp;
|
||||
return SnowflakeUtil.timestampFrom(this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -43,7 +43,7 @@ class BaseGuild extends Base {
|
||||
* @readonly
|
||||
*/
|
||||
get createdTimestamp() {
|
||||
return SnowflakeUtil.deconstruct(this.id).timestamp;
|
||||
return SnowflakeUtil.timestampFrom(this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -51,7 +51,7 @@ class Channel extends Base {
|
||||
* @readonly
|
||||
*/
|
||||
get createdTimestamp() {
|
||||
return SnowflakeUtil.deconstruct(this.id).timestamp;
|
||||
return SnowflakeUtil.timestampFrom(this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -586,7 +586,7 @@ class GuildAuditLogsEntry {
|
||||
* @readonly
|
||||
*/
|
||||
get createdTimestamp() {
|
||||
return SnowflakeUtil.deconstruct(this.id).timestamp;
|
||||
return SnowflakeUtil.timestampFrom(this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -110,7 +110,7 @@ class GuildPreview extends Base {
|
||||
* @readonly
|
||||
*/
|
||||
get createdTimestamp() {
|
||||
return SnowflakeUtil.deconstruct(this.id).timestamp;
|
||||
return SnowflakeUtil.timestampFrom(this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -82,7 +82,7 @@ class Interaction extends Base {
|
||||
* @readonly
|
||||
*/
|
||||
get createdTimestamp() {
|
||||
return SnowflakeUtil.deconstruct(this.id).timestamp;
|
||||
return SnowflakeUtil.timestampFrom(this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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) {
|
||||
/**
|
||||
|
||||
@@ -134,7 +134,7 @@ class Role extends Base {
|
||||
* @readonly
|
||||
*/
|
||||
get createdTimestamp() {
|
||||
return SnowflakeUtil.deconstruct(this.id).timestamp;
|
||||
return SnowflakeUtil.timestampFrom(this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -139,7 +139,7 @@ class StageInstance extends Base {
|
||||
* @readonly
|
||||
*/
|
||||
get createdTimestamp() {
|
||||
return SnowflakeUtil.deconstruct(this.id).timestamp;
|
||||
return SnowflakeUtil.timestampFrom(this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -125,7 +125,7 @@ class Sticker extends Base {
|
||||
* @readonly
|
||||
*/
|
||||
get createdTimestamp() {
|
||||
return SnowflakeUtil.deconstruct(this.id).timestamp;
|
||||
return SnowflakeUtil.timestampFrom(this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -61,7 +61,7 @@ class StickerPack extends Base {
|
||||
* @readonly
|
||||
*/
|
||||
get createdTimestamp() {
|
||||
return SnowflakeUtil.deconstruct(this.id).timestamp;
|
||||
return SnowflakeUtil.timestampFrom(this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -76,7 +76,7 @@ class Team extends Base {
|
||||
* @readonly
|
||||
*/
|
||||
get createdTimestamp() {
|
||||
return SnowflakeUtil.deconstruct(this.id).timestamp;
|
||||
return SnowflakeUtil.timestampFrom(this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -127,7 +127,7 @@ class User extends Base {
|
||||
* @readonly
|
||||
*/
|
||||
get createdTimestamp() {
|
||||
return SnowflakeUtil.deconstruct(this.id).timestamp;
|
||||
return SnowflakeUtil.timestampFrom(this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -379,7 +379,7 @@ class Webhook {
|
||||
* @readonly
|
||||
*/
|
||||
get createdTimestamp() {
|
||||
return SnowflakeUtil.deconstruct(this.id).timestamp;
|
||||
return SnowflakeUtil.timestampFrom(this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -60,7 +60,7 @@ class Application extends Base {
|
||||
* @readonly
|
||||
*/
|
||||
get createdTimestamp() {
|
||||
return SnowflakeUtil.deconstruct(this.id).timestamp;
|
||||
return SnowflakeUtil.timestampFrom(this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
1
typings/index.d.ts
vendored
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user