refactor: use guild-wide route for fetching active threads (#6193)

Co-authored-by: SpaceEEC <24881032+SpaceEEC@users.noreply.github.com>
This commit is contained in:
ckohen
2021-07-31 04:19:41 -07:00
committed by GitHub
parent 00bae4fe6b
commit 2a07055cc0
3 changed files with 31 additions and 8 deletions

View File

@@ -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<FetchedThreads>}
*/
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,
};
}
}