Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(SnowflakeUtil): add timestampFrom #7058

Merged
merged 1 commit into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/structures/ApplicationCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class ApplicationCommand extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/structures/BaseGuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class BaseGuild extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/structures/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Channel extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}

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

/**
Expand Down
2 changes: 1 addition & 1 deletion src/structures/GuildAuditLogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ class GuildAuditLogsEntry {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/structures/GuildPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class GuildPreview extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/structures/Interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Interaction extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/structures/Role.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class Role extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/structures/StageInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class StageInstance extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/structures/Sticker.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class Sticker extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/structures/StickerPack.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class StickerPack extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/structures/Team.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Team extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/structures/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class User extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/structures/Webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ class Webhook {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/structures/interfaces/Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Application extends Base {
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
return SnowflakeUtil.timestampFrom(this.id);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/structures/interfaces/TextBasedChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions src/util/SnowflakeUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
5 changes: 4 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1546,7 +1546,9 @@ export class MessageComponentInteraction<Cached extends CacheType = CacheType> e
public static resolveType(type: MessageComponentTypeResolvable): MessageComponentType;
}

export class MessageContextMenuInteraction<Cached extends CacheType = CacheType> extends ContextMenuInteraction<Cached> {
export class MessageContextMenuInteraction<
Cached extends CacheType = CacheType,
> extends ContextMenuInteraction<Cached> {
public readonly targetMessage: CacheTypeReducer<Cached, Message, APIMessage>;
public inGuild(): this is MessageContextMenuInteraction<'present'>;
public inCachedGuild(): this is MessageContextMenuInteraction<'cached'>;
Expand Down Expand Up @@ -1974,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;
}

Expand Down