Skip to content

Commit

Permalink
fix(ThreadChannel): check for existence of properties when patching (#…
Browse files Browse the repository at this point in the history
…5961)

Co-authored-by: Noel <buechler.noel@outlook.com>
  • Loading branch information
monbrey and iCrawl committed Jul 1, 2021
1 parent 71fb33a commit 9ac6867
Showing 1 changed file with 85 additions and 69 deletions.
154 changes: 85 additions & 69 deletions src/structures/ThreadChannel.js
Expand Up @@ -50,75 +50,91 @@ class ThreadChannel extends Channel {
*/
this.name = data.name;

/**
* The ID of the parent channel to this thread
* @type {Snowflake}
*/
this.parentID = data.parent_id;

/**
* Whether the thread is locked
* @type {boolean}
*/
this.locked = data.thread_metadata.locked ?? false;

/**
* Whether the thread is active (false) or archived (true)
* @type {boolean}
*/
this.archived = data.thread_metadata.archived;

/**
* The id of the member that created this thread
* @type {?Snowflake}
*/
this.ownerID = data.owner_id;

/**
* How long in minutes after recent activity before the thread is automatically archived
* @type {number}
*/
this.autoArchiveDuration = data.thread_metadata.auto_archive_duration;

/**
* The ID of the last message sent in this thread, if one was sent
* @type {?Snowflake}
*/
this.lastMessageID = data.last_message_id;

/**
* The timestamp when the last pinned message was pinned, if there was one
* @type {?number}
*/
this.lastPinTimestamp = data.last_pin_timestamp ? new Date(data.last_pin_timestamp).getTime() : null;

/**
* The ratelimit per user for this thread in seconds
* @type {number}
*/
this.rateLimitPerUser = data.rate_limit_per_user ?? 0;

/**
* The timestamp when the thread's archive status was last changed
* <info>If the thread was never archived or unarchived, this is set when it's created</info>
* @type {number}
*/
this.archiveTimestamp = new Date(data.thread_metadata.archive_timestamp).getTime();

/**
* The approximate count of messages in this thread
* <info>This value will not count above 50 even when there are more than 50 messages
* If you need an approximate value higher than this, use ThreadChannel#messages.cache.size</info>
* @type {number}
*/
this.messageCount = data.message_count;

/**
* The approximate count of users in this thread
* <info>This value will not count above 50 even when there are more than 50 members</info>
* @type {number}
*/
this.memberCount = data.member_count;
if ('parent_id' in data) {
/**
* The ID of the parent channel to this thread
* @type {Snowflake}
*/
this.parentID = data.parent_id;
}

if ('thread_metadata' in data) {
/**
* Whether the thread is locked
* @type {boolean}
*/
this.locked = data.thread_metadata.locked ?? false;

/**
* Whether the thread is active (false) or archived (true)
* @type {boolean}
*/
this.archived = data.thread_metadata.archived;

/**
* How long in minutes after recent activity before the thread is automatically archived
* @type {number}
*/
this.autoArchiveDuration = data.thread_metadata.auto_archive_duration;

/**
* The timestamp when the thread's archive status was last changed
* <info>If the thread was never archived or unarchived, this is set when it's created</info>
* @type {number}
*/
this.archiveTimestamp = new Date(data.thread_metadata.archive_timestamp).getTime();
}

if ('owner_id' in data) {
/**
* The id of the member that created this thread
* @type {?Snowflake}
*/
this.ownerID = data.owner_id;
}

if ('last_message_id' in data) {
/**
* The ID of the last message sent in this thread, if one was sent
* @type {?Snowflake}
*/
this.lastMessageID = data.last_message_id;
}

if ('last_pin_timestamp' in data) {
/**
* The timestamp when the last pinned message was pinned, if there was one
* @type {?number}
*/
this.lastPinTimestamp = data.last_pin_timestamp ? new Date(data.last_pin_timestamp).getTime() : null;
}

if ('rate_limit_per_user' in data) {
/**
* The ratelimit per user for this thread in seconds
* @type {number}
*/
this.rateLimitPerUser = data.rate_limit_per_user ?? 0;
}

if ('message_count' in data) {
/**
* The approximate count of messages in this thread
* <info>This value will not count above 50 even when there are more than 50 messages
* If you need an approximate value higher than this, use ThreadChannel#messages.cache.size</info>
* @type {number}
*/
this.messageCount = data.message_count;
}

if ('member_count' in data) {
/**
* The approximate count of users in this thread
* <info>This value will not count above 50 even when there are more than 50 members</info>
* @type {number}
*/
this.memberCount = data.member_count;
}

if (data.member && this.client.user) this.members._add({ user_id: this.client.user.id, ...data.member });
if (data.messages) for (const message of data.messages) this.messages.add(message);
Expand Down

0 comments on commit 9ac6867

Please sign in to comment.