mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
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:
@@ -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<?GuildChannel|Collection<Snowflake, GuildChannel>>}
|
||||
* @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<FetchedThreads>}
|
||||
* @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}
|
||||
*/
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
1
typings/index.d.ts
vendored
1
typings/index.d.ts
vendored
@@ -2303,6 +2303,7 @@ export class GuildChannelManager extends CachedManager<
|
||||
): Promise<
|
||||
Collection<Snowflake, TextChannel | VoiceChannel | CategoryChannel | NewsChannel | StoreChannel | StageChannel>
|
||||
>;
|
||||
public fetchActiveThreads(cache?: boolean): Promise<FetchedThreads>;
|
||||
}
|
||||
|
||||
export class GuildEmojiManager extends BaseGuildEmojiManager {
|
||||
|
||||
Reference in New Issue
Block a user