diff --git a/src/structures/Channel.js b/src/structures/Channel.js index 6b42b2cd6..5c5bef1c4 100644 --- a/src/structures/Channel.js +++ b/src/structures/Channel.js @@ -1,3 +1,5 @@ +const Snowflake = require('../util/Snowflake'); + /** * Represents any channel on Discord */ @@ -38,7 +40,7 @@ class Channel { * @readonly */ get createdTimestamp() { - return (this.id / 4194304) + 1420070400000; + return Snowflake.deconstruct(this.id).timestamp; } /** diff --git a/src/structures/Emoji.js b/src/structures/Emoji.js index 999cb42a9..06a0c2503 100644 --- a/src/structures/Emoji.js +++ b/src/structures/Emoji.js @@ -1,5 +1,6 @@ const Constants = require('../util/Constants'); const Collection = require('../util/Collection'); +const Snowflake = require('../util/Snowflake'); /** * Represents a custom emoji @@ -57,7 +58,7 @@ class Emoji { * @readonly */ get createdTimestamp() { - return (this.id / 4194304) + 1420070400000; + return Snowflake.deconstruct(this.id).timestamp; } /** diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 4498fe297..617ce1e79 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -6,6 +6,7 @@ const GuildMember = require('./GuildMember'); const Constants = require('../util/Constants'); const Collection = require('../util/Collection'); const Util = require('../util/Util'); +const Snowflake = require('../util/Snowflake'); /** * Represents a guild (or a server) on Discord. @@ -216,7 +217,7 @@ class Guild { * @readonly */ get createdTimestamp() { - return (this.id / 4194304) + 1420070400000; + return Snowflake.deconstruct(this.id).timestamp; } /** diff --git a/src/structures/OAuth2Application.js b/src/structures/OAuth2Application.js index 30e9b3f34..68c003183 100644 --- a/src/structures/OAuth2Application.js +++ b/src/structures/OAuth2Application.js @@ -1,3 +1,5 @@ +const Snowflake = require('../util/Snowflake'); + /** * Represents an OAuth2 Application */ @@ -100,7 +102,7 @@ class OAuth2Application { * @readonly */ get createdTimestamp() { - return (this.id / 4194304) + 1420070400000; + return Snowflake.deconstruct(this.id).timestamp; } /** diff --git a/src/structures/Role.js b/src/structures/Role.js index ff4aee6e6..6d1556b41 100644 --- a/src/structures/Role.js +++ b/src/structures/Role.js @@ -1,3 +1,4 @@ +const Snowflake = require('../util/Snowflake'); const Permissions = require('../util/Permissions'); /** @@ -78,7 +79,7 @@ class Role { * @readonly */ get createdTimestamp() { - return (this.id / 4194304) + 1420070400000; + return Snowflake.deconstruct(this.id).timestamp; } /** diff --git a/src/structures/User.js b/src/structures/User.js index 939d3f157..48b2e5759 100644 --- a/src/structures/User.js +++ b/src/structures/User.js @@ -1,6 +1,7 @@ const TextBasedChannel = require('./interface/TextBasedChannel'); const Constants = require('../util/Constants'); const Presence = require('./Presence').Presence; +const Snowflake = require('../util/Snowflake'); /** * Represents a user on Discord. @@ -76,7 +77,7 @@ class User { * @readonly */ get createdTimestamp() { - return (this.id / 4194304) + 1420070400000; + return Snowflake.deconstruct(this.id).timestamp; } /** diff --git a/src/util/Snowflake.js b/src/util/Snowflake.js index 60d0926ac..a137a4699 100644 --- a/src/util/Snowflake.js +++ b/src/util/Snowflake.js @@ -34,7 +34,8 @@ class SnowflakeUtil { /** * A deconstructed snowflake * @typedef {Object} DeconstructedSnowflake - * @property {Date} date Date in the snowflake + * @property {number} timestamp Timestamp the snowflake was created + * @property {Date} date Date the snowflake was created * @property {number} workerID Worker ID in the snowflake * @property {number} processID Process ID in the snowflake * @property {number} increment Increment in the snowflake @@ -48,13 +49,18 @@ class SnowflakeUtil { */ static deconstruct(snowflake) { const BINARY = pad(Long.fromString(snowflake).toString(2), 64); - return { - date: new Date(parseInt(BINARY.substring(0, 42), 2) + EPOCH), + const res = { + timestamp: parseInt(BINARY.substring(0, 42), 2) + EPOCH, workerID: parseInt(BINARY.substring(42, 47), 2), processID: parseInt(BINARY.substring(47, 52), 2), increment: parseInt(BINARY.substring(52, 64), 2), binary: BINARY, }; + Object.defineProperty(res, 'date', { + get: function get() { return new Date(this.timestamp); }, + enumerable: true, + }); + return res; } }