Made date/timestamps consistent and less shitty

This commit is contained in:
Schuyler Cebulskie
2016-09-27 19:46:53 -04:00
parent b6c26cc2ae
commit 1e57c968dd
10 changed files with 137 additions and 75 deletions

File diff suppressed because one or more lines are too long

View File

@@ -295,7 +295,7 @@ class Client extends EventEmitter {
channels++;
for (const message of channel.messages.values()) {
if (now - (message._editedTimestamp || message._timestamp) > lifetimeMs) {
if (now - (message.editedTimestamp || message.createdTimestamp) > lifetimeMs) {
channel.messages.delete(message.id);
messages++;
}

View File

@@ -32,12 +32,21 @@ class Channel {
}
/**
* The time the channel was created
* The timestamp the channel was created at
* @type {number}
* @readonly
* @type {Date}
*/
get creationDate() {
return new Date((this.id / 4194304) + 1420070400000);
get createdTimestamp() {
return (this.id / 4194304) + 1420070400000;
}
/**
* The time the channel was created
* @type {Date}
* @readonly
*/
get createdAt() {
return new Date(this.createdTimestamp);
}
/**

View File

@@ -51,12 +51,21 @@ class Emoji {
}
/**
* The time the emoji was created
* The timestamp the emoji was created at
* @type {number}
* @readonly
* @type {Date}
*/
get creationDate() {
return new Date((this.id / 4194304) + 1420070400000);
get createdTimestamp() {
return (this.id / 4194304) + 1420070400000;
}
/**
* The time the emoji was created
* @type {Date}
* @readonly
*/
get createdAt() {
return new Date(this.createdTimestamp);
}
/**

View File

@@ -152,10 +152,15 @@ class Guild {
*/
this.verificationLevel = data.verification_level;
/**
* The timestamp the client user joined the guild at
* @type {number}
*/
this.joinedTimestamp = data.joined_at ? new Date(data.joined_at).getTime() : this.joinedTimestamp;
this.id = data.id;
this.available = !data.unavailable;
this.features = data.features || this.features || [];
this._joinedTimestamp = data.joined_at ? new Date(data.joined_at).getTime() : this._joinedTimestamp;
if (data.members) {
this.members.clear();
@@ -208,20 +213,30 @@ class Guild {
}
/**
* The time the guild was created
* The timestamp the guild was created at
* @type {number}
* @readonly
* @type {Date}
*/
get creationDate() {
return new Date((this.id / 4194304) + 1420070400000);
get createdTimestamp() {
return (this.id / 4194304) + 1420070400000;
}
/**
* The date at which the logged-in client joined the guild.
* The time the guild was created
* @type {Date}
* @readonly
*/
get joinDate() {
return new Date(this._joinedTimestamp);
get createdAt() {
return new Date(this.createdTimestamp);
}
/**
* The time the client user joined the guild
* @type {Date}
* @readonly
*/
get joinedAt() {
return new Date(this.joinedTimestamp);
}
/**

View File

@@ -82,9 +82,23 @@ class GuildMember {
*/
this.nickname = data.nick || null;
/**
* The timestamp the member joined the guild at
* @type {number}
*/
this.joinedTimestamp = new Date(data.joined_at).getTime();
this.user = data.user;
this._roles = data.roles;
this._joinDate = new Date(data.joined_at).getTime();
}
/**
* The time the member joined the guild
* @type {Date}
* @readonly
*/
get joinedAt() {
return new Date(this.joinedTimestamp);
}
/**
@@ -96,14 +110,6 @@ class GuildMember {
return this.guild.presences.get(this.id);
}
/**
* The date this member joined the guild
* @type {Date}
*/
get joinDate() {
return new Date(this._joinDate);
}
/**
* A list of roles that are applied to this GuildMember, mapped by the role ID.
* @type {Collection<string, Role>}

View File

@@ -92,23 +92,20 @@ class Invite {
*/
this.channel = this.client.channels.get(data.channel.id) || new PartialGuildChannel(this.client, data.channel);
this._createdAt = new Date(data.created_at).getTime();
/**
* The timestamp the invite was created at
* @type {number}
*/
this.createdTimestamp = new Date(data.created_at).getTime();
}
/**
* The creation date of the invite
* The time the invite was created
* @type {Date}
* @readonly
*/
get createdAt() {
return new Date(this._createdAt);
}
/**
* The creation date of the invite
* @type {Date}
*/
get creationDate() {
return new Date(this._createdAt);
return new Date(this.createdTimestamp);
}
/**

View File

@@ -87,6 +87,18 @@ class Message {
this.attachments = new Collection();
for (const attachment of data.attachments) this.attachments.set(attachment.id, new Attachment(this, attachment));
/**
* The timestamp the message was sent at
* @type {number}
*/
this.createdTimestamp = new Date(data.timestamp).getTime();
/**
* The timestamp the message was last edited at (if applicable)
* @type {?number}
*/
this.editedTimestamp = data.edited_timestamp ? new Date(data.edited_timestamp).getTime() : null;
/**
* An object containing a further users, roles or channels collections
* @type {Object}
@@ -128,8 +140,6 @@ class Message {
}
}
this._timestamp = new Date(data.timestamp).getTime();
this._editedTimestamp = data.edited_timestamp ? new Date(data.edited_timestamp).getTime() : null;
this._edits = [];
}
@@ -139,9 +149,9 @@ class Message {
if (this.guild) this.member = this.guild.member(this.author);
}
if (data.content) this.content = data.content;
if (data.timestamp) this._timestamp = new Date(data.timestamp).getTime();
if (data.timestamp) this.createdTimestamp = new Date(data.timestamp).getTime();
if (data.edited_timestamp) {
this._editedTimestamp = data.edited_timestamp ? new Date(data.edited_timestamp).getTime() : null;
this.editedTimestamp = data.edited_timestamp ? new Date(data.edited_timestamp).getTime() : null;
}
if ('tts' in data) this.tts = data.tts;
if ('mention_everyone' in data) this.mentions.everyone = data.mention_everyone;
@@ -185,19 +195,21 @@ class Message {
}
/**
* When the message was sent
* The time the message was sent
* @type {Date}
* @readonly
*/
get timestamp() {
return new Date(this._timestamp);
get createdAt() {
return new Date(this.createdTimestamp);
}
/**
* If the message was edited, the timestamp at which it was last edited
* The time the message was last edited at (if applicable)
* @type {?Date}
* @readonly
*/
get editedTimestamp() {
return new Date(this._editedTimestamp);
get editedAt() {
return this.editedTimestamp ? new Date(this.editedTimestamp) : null;
}
/**
@@ -401,8 +413,8 @@ class Message {
if (equal && rawData) {
equal = this.mentions.everyone === message.mentions.everyone &&
this._timestamp === new Date(rawData.timestamp).getTime() &&
this._editedTimestamp === new Date(rawData.edited_timestamp).getTime();
this.createdTimestamp === new Date(rawData.timestamp).getTime() &&
this.editedTimestamp === new Date(rawData.edited_timestamp).getTime();
}
return equal;

View File

@@ -72,12 +72,21 @@ class Role {
}
/**
* The time the role was created
* The timestamp the role was created at
* @type {number}
* @readonly
* @type {Date}
*/
get creationDate() {
return new Date((this.id / 4194304) + 1420070400000);
get createdTimestamp() {
return (this.id / 4194304) + 1420070400000;
}
/**
* The time the role was created
* @type {Date}
* @readonly
*/
get createdAt() {
return new Date(this.createdTimestamp);
}
/**

View File

@@ -50,23 +50,6 @@ class User {
this.bot = Boolean(data.bot);
}
/**
* The presence of this user
* @readonly
* @type {Presence}
*/
get presence() {
if (this.client.presences.has(this.id)) {
return this.client.presences.get(this.id);
}
for (const guild of this.client.guilds.values()) {
if (guild.presences.has(this.id)) {
return guild.presences.get(this.id);
}
}
return new Presence();
}
patch(data) {
for (const prop of ['id', 'username', 'discriminator', 'avatar', 'bot']) {
if (typeof data[prop] !== 'undefined') this[prop] = data[prop];
@@ -74,12 +57,34 @@ class User {
}
/**
* The time the user was created
* The timestamp the user was created at
* @type {number}
* @readonly
* @type {Date}
*/
get creationDate() {
return new Date((this.id / 4194304) + 1420070400000);
get createdTimestamp() {
return (this.id / 4194304) + 1420070400000;
}
/**
* The time the user was created
* @type {Date}
* @readonly
*/
get createdAt() {
return new Date(this.createdTimestamp);
}
/**
* The presence of this user
* @readonly
* @type {Presence}
*/
get presence() {
if (this.client.presences.has(this.id)) return this.client.presences.get(this.id);
for (const guild of this.client.guilds.values()) {
if (guild.presences.has(this.id)) return guild.presences.get(this.id);
}
return new Presence();
}
/**