From 2a07055cc0bd9d7e1ac2e36066d08f15ebd9414b Mon Sep 17 00:00:00 2001 From: ckohen Date: Sat, 31 Jul 2021 04:19:41 -0700 Subject: [PATCH] refactor: use guild-wide route for fetching active threads (#6193) Co-authored-by: SpaceEEC <24881032+SpaceEEC@users.noreply.github.com> --- src/managers/GuildChannelManager.js | 23 ++++++++++++++++++++++- src/managers/ThreadManager.js | 15 ++++++++------- typings/index.d.ts | 1 + 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/managers/GuildChannelManager.js b/src/managers/GuildChannelManager.js index c154ae646..db02c77f7 100644 --- a/src/managers/GuildChannelManager.js +++ b/src/managers/GuildChannelManager.js @@ -2,6 +2,7 @@ const { Collection } = require('@discordjs/collection'); const CachedManager = require('./CachedManager'); +const ThreadManager = require('./ThreadManager'); const { Error } = require('../errors'); const GuildChannel = require('../structures/GuildChannel'); const PermissionOverwrites = require('../structures/PermissionOverwrites'); @@ -149,7 +150,7 @@ class GuildChannelManager extends CachedManager { * @param {BaseFetchOptions} [options] Additional options for this fetch * @returns {Promise>} * @example - * // Fetch all channels from the guild + * // Fetch all channels from the guild (excluding threads) * message.guild.channels.fetch() * .then(channels => console.log(`There are ${channels.size} channels.`)) * .catch(console.error); @@ -177,6 +178,26 @@ class GuildChannelManager extends CachedManager { for (const channel of data) channels.set(channel.id, this.client.channels._add(channel, this.guild, { cache })); return channels; } + + /** + * Obtains all active thread channels in the guild from Discord + * @param {boolean} [cache=true] Whether to cache the fetched data + * @returns {Promise} + * @example + * // Fetch all threads from the guild + * message.guild.channels.fetchActiveThreads() + * .then(fetched => console.log(`There are ${fetched.threads.size} threads.`)) + * .catch(console.error); + */ + async fetchActiveThreads(cache = true) { + const raw = await this.client.api.guilds(this.guild.id).threads.active.get(); + return ThreadManager._mapThreads(raw, this.client, { guild: this.guild, cache }); + } } module.exports = GuildChannelManager; + +/** + * @external APIActiveThreadsList + * @see {@link https://discord.com/developers/docs/resources/guild#list-active-threads-response-body} + */ diff --git a/src/managers/ThreadManager.js b/src/managers/ThreadManager.js index d947e4fa3..429761fd5 100644 --- a/src/managers/ThreadManager.js +++ b/src/managers/ThreadManager.js @@ -220,7 +220,7 @@ class ThreadManager extends CachedManager { const raw = await path.threads .archived(type) .get({ query: { before: type === 'private' && !fetchAll ? id : timestamp, limit } }); - return this._mapThreads(raw, cache); + return this.constructor._mapThreads(raw, this.client, { parent: this.channel, cache }); } /** @@ -229,20 +229,21 @@ class ThreadManager extends CachedManager { * @returns {Promise} */ async fetchActive(cache = true) { - const raw = await this.client.api.channels(this.channel.id).threads.active.get(); - return this._mapThreads(raw, cache); + const raw = await this.client.api.guilds(this.channel.guild.id).threads.active.get(); + return this.constructor._mapThreads(raw, this.client, { parent: this.channel, cache }); } - _mapThreads(rawThreads, cache) { + static _mapThreads(rawThreads, client, { parent, guild, cache }) { const threads = rawThreads.threads.reduce((coll, raw) => { - const thread = this.client.channels._add(raw, null, { cache }); + const thread = client.channels._add(raw, guild ?? parent?.guild, { cache }); + if (parent && thread.parentId !== parent.id) return coll; return coll.set(thread.id, thread); }, new Collection()); // Discord sends the thread id as id in this object - for (const rawMember of rawThreads.members) threads.get(rawMember.id)?.members._add(rawMember); + for (const rawMember of rawThreads.members) client.channels.cache.get(rawMember.id)?.members._add(rawMember); return { threads, - hasMore: rawThreads.has_more, + hasMore: rawThreads.has_more ?? false, }; } } diff --git a/typings/index.d.ts b/typings/index.d.ts index ade7a0caf..974c069f1 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -2303,6 +2303,7 @@ export class GuildChannelManager extends CachedManager< ): Promise< Collection >; + public fetchActiveThreads(cache?: boolean): Promise; } export class GuildEmojiManager extends BaseGuildEmojiManager {