From 86dfa86b5278a94a5fcfc4f926b757a90f7eb1a4 Mon Sep 17 00:00:00 2001 From: Papageorgiadis Savvas Date: Thu, 5 Aug 2021 16:09:35 +0300 Subject: [PATCH 1/6] feat(Threads): max autoArchiveDuration --- src/managers/ThreadManager.js | 12 +++++++++++- src/structures/Message.js | 9 +++++++++ src/structures/ThreadChannel.js | 18 ++++++++++++++++++ typings/index.d.ts | 2 +- 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/managers/ThreadManager.js b/src/managers/ThreadManager.js index 315a00bac78b..c43444b430df 100644 --- a/src/managers/ThreadManager.js +++ b/src/managers/ThreadManager.js @@ -66,7 +66,8 @@ class ThreadManager extends CachedManager { * * `1440` (1 day) * * `4320` (3 days) This is only available when the guild has the `THREE_DAY_THREAD_ARCHIVE` feature. * * `10080` (7 days) This is only available when the guild has the `SEVEN_DAY_THREAD_ARCHIVE` feature. - * @typedef {number} ThreadAutoArchiveDuration + * * `'MAX'` Based on the guilds boost count + * @typedef {number | string} ThreadAutoArchiveDuration */ /** @@ -119,6 +120,15 @@ class ThreadManager extends CachedManager { } else if (this.channel.type !== 'GUILD_NEWS') { resolvedType = typeof type === 'string' ? ChannelTypes[type] : type ?? resolvedType; } + if (autoArchiveDuration === 'MAX') { + let maxAutoArchiveDuration = 1440; + if (this.channel.guild.premiumSubscriptionCount >= 15) { + maxAutoArchiveDuration = 10080; + } else if (this.channel.guild.premiumSubscriptionCount >= 2) { + maxAutoArchiveDuration = 4320; + } + autoArchiveDuration = maxAutoArchiveDuration; + } const data = await path.threads.post({ data: { diff --git a/src/structures/Message.js b/src/structures/Message.js index 71f62654a0e1..939ad055d58c 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -763,6 +763,15 @@ class Message extends Base { return Promise.reject(new Error('MESSAGE_THREAD_PARENT')); } if (this.hasThread) return Promise.reject(new Error('MESSAGE_EXISTING_THREAD')); + if (options.autoArchiveDuration === 'MAX') { + let maxAutoArchiveDuration = 1440; + if (this.guild.premiumSubscriptionCount >= 15) { + maxAutoArchiveDuration = 10080; + } else if (this.guild.premiumSubscriptionCount >= 2) { + maxAutoArchiveDuration = 4320; + } + options.autoArchiveDuration = maxAutoArchiveDuration; + } return this.channel.threads.create({ ...options, startMessage: this }); } diff --git a/src/structures/ThreadChannel.js b/src/structures/ThreadChannel.js index f31af56b9af0..f5125259c1b0 100644 --- a/src/structures/ThreadChannel.js +++ b/src/structures/ThreadChannel.js @@ -275,6 +275,15 @@ class ThreadChannel extends Channel { * .catch(console.error); */ async edit(data, reason) { + if (data.autoArchiveDuration === 'MAX') { + let maxAutoArchiveDuration = 1440; + if (this.guild.premiumSubscriptionCount >= 15) { + maxAutoArchiveDuration = 10080; + } else if (this.guild.premiumSubscriptionCount >= 2) { + maxAutoArchiveDuration = 4320; + } + data.autoArchiveDuration = maxAutoArchiveDuration; + } const newData = await this.client.api.channels(this.id).patch({ data: { name: (data.name ?? this.name).trim(), @@ -319,6 +328,15 @@ class ThreadChannel extends Channel { * .catch(console.error); */ setAutoArchiveDuration(autoArchiveDuration, reason) { + if (autoArchiveDuration === 'MAX') { + let maxAutoArchiveDuration = 1440; + if (this.guild.premiumSubscriptionCount >= 15) { + maxAutoArchiveDuration = 10080; + } else if (this.guild.premiumSubscriptionCount >= 2) { + maxAutoArchiveDuration = 4320; + } + autoArchiveDuration = maxAutoArchiveDuration; + } return this.edit({ autoArchiveDuration }, reason); } diff --git a/typings/index.d.ts b/typings/index.d.ts index d8a301031763..584fc32ec104 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -4419,7 +4419,7 @@ export type TextBasedChannelTypes = TextBasedChannels['type']; export type TextChannelResolvable = Snowflake | TextChannel; -export type ThreadAutoArchiveDuration = 60 | 1440 | 4320 | 10080; +export type ThreadAutoArchiveDuration = 60 | 1440 | 4320 | 10080 | 'MAX'; export type ThreadChannelResolvable = ThreadChannel | Snowflake; From 1bbc10ebba73e15475aa4bbc188cd464d33769a0 Mon Sep 17 00:00:00 2001 From: Papageorgiadis Savvas Date: Thu, 5 Aug 2021 17:25:31 +0300 Subject: [PATCH 2/6] fix: direct assignment --- src/managers/ThreadManager.js | 9 ++++----- src/structures/Message.js | 7 +++---- src/structures/ThreadChannel.js | 14 ++++++-------- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/managers/ThreadManager.js b/src/managers/ThreadManager.js index c43444b430df..6f7d85080fa5 100644 --- a/src/managers/ThreadManager.js +++ b/src/managers/ThreadManager.js @@ -67,7 +67,7 @@ class ThreadManager extends CachedManager { * * `4320` (3 days) This is only available when the guild has the `THREE_DAY_THREAD_ARCHIVE` feature. * * `10080` (7 days) This is only available when the guild has the `SEVEN_DAY_THREAD_ARCHIVE` feature. * * `'MAX'` Based on the guilds boost count - * @typedef {number | string} ThreadAutoArchiveDuration + * @typedef {number|string} ThreadAutoArchiveDuration */ /** @@ -121,13 +121,12 @@ class ThreadManager extends CachedManager { resolvedType = typeof type === 'string' ? ChannelTypes[type] : type ?? resolvedType; } if (autoArchiveDuration === 'MAX') { - let maxAutoArchiveDuration = 1440; + autoArchiveDuration = 1440; if (this.channel.guild.premiumSubscriptionCount >= 15) { - maxAutoArchiveDuration = 10080; + autoArchiveDuration = 10080; } else if (this.channel.guild.premiumSubscriptionCount >= 2) { - maxAutoArchiveDuration = 4320; + autoArchiveDuration = 4320; } - autoArchiveDuration = maxAutoArchiveDuration; } const data = await path.threads.post({ diff --git a/src/structures/Message.js b/src/structures/Message.js index 939ad055d58c..9deb9fe113c2 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -764,13 +764,12 @@ class Message extends Base { } if (this.hasThread) return Promise.reject(new Error('MESSAGE_EXISTING_THREAD')); if (options.autoArchiveDuration === 'MAX') { - let maxAutoArchiveDuration = 1440; + options.autoArchiveDuration = 1440; if (this.guild.premiumSubscriptionCount >= 15) { - maxAutoArchiveDuration = 10080; + options.autoArchiveDuration = 10080; } else if (this.guild.premiumSubscriptionCount >= 2) { - maxAutoArchiveDuration = 4320; + options.autoArchiveDuration = 4320; } - options.autoArchiveDuration = maxAutoArchiveDuration; } return this.channel.threads.create({ ...options, startMessage: this }); } diff --git a/src/structures/ThreadChannel.js b/src/structures/ThreadChannel.js index f5125259c1b0..30ce7ed61eec 100644 --- a/src/structures/ThreadChannel.js +++ b/src/structures/ThreadChannel.js @@ -276,13 +276,12 @@ class ThreadChannel extends Channel { */ async edit(data, reason) { if (data.autoArchiveDuration === 'MAX') { - let maxAutoArchiveDuration = 1440; + data.autoArchiveDuration = 1440; if (this.guild.premiumSubscriptionCount >= 15) { - maxAutoArchiveDuration = 10080; + data.autoArchiveDuration = 10080; } else if (this.guild.premiumSubscriptionCount >= 2) { - maxAutoArchiveDuration = 4320; + data.autoArchiveDuration = 4320; } - data.autoArchiveDuration = maxAutoArchiveDuration; } const newData = await this.client.api.channels(this.id).patch({ data: { @@ -329,13 +328,12 @@ class ThreadChannel extends Channel { */ setAutoArchiveDuration(autoArchiveDuration, reason) { if (autoArchiveDuration === 'MAX') { - let maxAutoArchiveDuration = 1440; + autoArchiveDuration = 1440; if (this.guild.premiumSubscriptionCount >= 15) { - maxAutoArchiveDuration = 10080; + autoArchiveDuration = 10080; } else if (this.guild.premiumSubscriptionCount >= 2) { - maxAutoArchiveDuration = 4320; + autoArchiveDuration = 4320; } - autoArchiveDuration = maxAutoArchiveDuration; } return this.edit({ autoArchiveDuration }, reason); } From 0a3e7d7054e8b00f03fe73e15298ad81b130a671 Mon Sep 17 00:00:00 2001 From: Papageorgiadis Savvas Date: Thu, 5 Aug 2021 21:54:57 +0300 Subject: [PATCH 3/6] fix: use features & remove duplicates --- src/managers/ThreadManager.js | 4 ++-- src/structures/Message.js | 8 -------- src/structures/ThreadChannel.js | 12 ++---------- 3 files changed, 4 insertions(+), 20 deletions(-) diff --git a/src/managers/ThreadManager.js b/src/managers/ThreadManager.js index 6f7d85080fa5..bd2245737de8 100644 --- a/src/managers/ThreadManager.js +++ b/src/managers/ThreadManager.js @@ -122,9 +122,9 @@ class ThreadManager extends CachedManager { } if (autoArchiveDuration === 'MAX') { autoArchiveDuration = 1440; - if (this.channel.guild.premiumSubscriptionCount >= 15) { + if (this.channel.guild.features.includes('SEVEN_DAY_THREAD_ARCHIVE')) { autoArchiveDuration = 10080; - } else if (this.channel.guild.premiumSubscriptionCount >= 2) { + } else if (this.channel.guild.features.includes('THREE_DAY_THREAD_ARCHIVE')) { autoArchiveDuration = 4320; } } diff --git a/src/structures/Message.js b/src/structures/Message.js index 9deb9fe113c2..71f62654a0e1 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -763,14 +763,6 @@ class Message extends Base { return Promise.reject(new Error('MESSAGE_THREAD_PARENT')); } if (this.hasThread) return Promise.reject(new Error('MESSAGE_EXISTING_THREAD')); - if (options.autoArchiveDuration === 'MAX') { - options.autoArchiveDuration = 1440; - if (this.guild.premiumSubscriptionCount >= 15) { - options.autoArchiveDuration = 10080; - } else if (this.guild.premiumSubscriptionCount >= 2) { - options.autoArchiveDuration = 4320; - } - } return this.channel.threads.create({ ...options, startMessage: this }); } diff --git a/src/structures/ThreadChannel.js b/src/structures/ThreadChannel.js index 30ce7ed61eec..d3c7f266145f 100644 --- a/src/structures/ThreadChannel.js +++ b/src/structures/ThreadChannel.js @@ -277,9 +277,9 @@ class ThreadChannel extends Channel { async edit(data, reason) { if (data.autoArchiveDuration === 'MAX') { data.autoArchiveDuration = 1440; - if (this.guild.premiumSubscriptionCount >= 15) { + if (this.channel.guild.features.includes('SEVEN_DAY_THREAD_ARCHIVE')) { data.autoArchiveDuration = 10080; - } else if (this.guild.premiumSubscriptionCount >= 2) { + } else if (this.channel.guild.features.includes('THREE_DAY_THREAD_ARCHIVE')) { data.autoArchiveDuration = 4320; } } @@ -327,14 +327,6 @@ class ThreadChannel extends Channel { * .catch(console.error); */ setAutoArchiveDuration(autoArchiveDuration, reason) { - if (autoArchiveDuration === 'MAX') { - autoArchiveDuration = 1440; - if (this.guild.premiumSubscriptionCount >= 15) { - autoArchiveDuration = 10080; - } else if (this.guild.premiumSubscriptionCount >= 2) { - autoArchiveDuration = 4320; - } - } return this.edit({ autoArchiveDuration }, reason); } From 12c2260d197a3a905550ed384cf5c7632cfc6058 Mon Sep 17 00:00:00 2001 From: Papageorgiadis Savvas Date: Thu, 5 Aug 2021 21:58:49 +0300 Subject: [PATCH 4/6] fix: access guild --- src/structures/ThreadChannel.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/structures/ThreadChannel.js b/src/structures/ThreadChannel.js index d3c7f266145f..56ca08f3ebd4 100644 --- a/src/structures/ThreadChannel.js +++ b/src/structures/ThreadChannel.js @@ -277,9 +277,9 @@ class ThreadChannel extends Channel { async edit(data, reason) { if (data.autoArchiveDuration === 'MAX') { data.autoArchiveDuration = 1440; - if (this.channel.guild.features.includes('SEVEN_DAY_THREAD_ARCHIVE')) { + if (this.guild.features.includes('SEVEN_DAY_THREAD_ARCHIVE')) { data.autoArchiveDuration = 10080; - } else if (this.channel.guild.features.includes('THREE_DAY_THREAD_ARCHIVE')) { + } else if (this.guild.features.includes('THREE_DAY_THREAD_ARCHIVE')) { data.autoArchiveDuration = 4320; } } From 7d3fb5dc473c8ccf59ec6914d9b74e54945f5949 Mon Sep 17 00:00:00 2001 From: Papageorgiadis Savvas Date: Thu, 5 Aug 2021 23:06:15 +0300 Subject: [PATCH 5/6] fix: avoid options mutation --- src/structures/ThreadChannel.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/structures/ThreadChannel.js b/src/structures/ThreadChannel.js index 56ca08f3ebd4..2487cd8f815d 100644 --- a/src/structures/ThreadChannel.js +++ b/src/structures/ThreadChannel.js @@ -275,19 +275,20 @@ class ThreadChannel extends Channel { * .catch(console.error); */ async edit(data, reason) { + let maxAutoArchiveDuration; if (data.autoArchiveDuration === 'MAX') { - data.autoArchiveDuration = 1440; + maxAutoArchiveDuration = 1440; if (this.guild.features.includes('SEVEN_DAY_THREAD_ARCHIVE')) { - data.autoArchiveDuration = 10080; + maxAutoArchiveDuration = 10080; } else if (this.guild.features.includes('THREE_DAY_THREAD_ARCHIVE')) { - data.autoArchiveDuration = 4320; + maxAutoArchiveDuration = 4320; } } const newData = await this.client.api.channels(this.id).patch({ data: { name: (data.name ?? this.name).trim(), archived: data.archived, - auto_archive_duration: data.autoArchiveDuration, + auto_archive_duration: maxAutoArchiveDuration ?? data.autoArchiveDuration, rate_limit_per_user: data.rateLimitPerUser, locked: data.locked, }, From b5c5833ed18475d857fb63586fd2c40abffe469b Mon Sep 17 00:00:00 2001 From: Papageorgiadis Savvas Date: Thu, 5 Aug 2021 23:18:43 +0300 Subject: [PATCH 6/6] fix: remove nullish coalescing --- src/structures/ThreadChannel.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/structures/ThreadChannel.js b/src/structures/ThreadChannel.js index 2487cd8f815d..2c78d3923b8c 100644 --- a/src/structures/ThreadChannel.js +++ b/src/structures/ThreadChannel.js @@ -275,20 +275,20 @@ class ThreadChannel extends Channel { * .catch(console.error); */ async edit(data, reason) { - let maxAutoArchiveDuration; + let autoArchiveDuration = data.autoArchiveDuration; if (data.autoArchiveDuration === 'MAX') { - maxAutoArchiveDuration = 1440; + autoArchiveDuration = 1440; if (this.guild.features.includes('SEVEN_DAY_THREAD_ARCHIVE')) { - maxAutoArchiveDuration = 10080; + autoArchiveDuration = 10080; } else if (this.guild.features.includes('THREE_DAY_THREAD_ARCHIVE')) { - maxAutoArchiveDuration = 4320; + autoArchiveDuration = 4320; } } const newData = await this.client.api.channels(this.id).patch({ data: { name: (data.name ?? this.name).trim(), archived: data.archived, - auto_archive_duration: maxAutoArchiveDuration ?? data.autoArchiveDuration, + auto_archive_duration: autoArchiveDuration, rate_limit_per_user: data.rateLimitPerUser, locked: data.locked, },