feat: thread channel
fix: ThreadManager#fetchArchived() feat: ThreadChannel#firstMessage todo: #486
This commit is contained in:
parent
597eb2979c
commit
1e5e855ca0
@ -67,7 +67,7 @@ class ThreadManager extends CachedManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Obtains a thread from Discord, or the channel cache if it's already available.
|
* Obtains a thread from Discord, or the channel cache if it's already available.
|
||||||
* @param {ThreadChannelResolvable|FetchThreadsOptions} [options] The options to fetch threads. If it is a
|
* @param {ThreadChannelResolvable|FetchChannelThreadsOptions} [options] The options to fetch threads. If it is a
|
||||||
* ThreadChannelResolvable then the specified thread will be fetched. Fetches all active threads if `undefined`
|
* ThreadChannelResolvable then the specified thread will be fetched. Fetches all active threads if `undefined`
|
||||||
* @param {BaseFetchOptions} [cacheOptions] Additional options for this fetch. <warn>The `force` field gets ignored
|
* @param {BaseFetchOptions} [cacheOptions] Additional options for this fetch. <warn>The `force` field gets ignored
|
||||||
* if `options` is not a {@link ThreadChannelResolvable}</warn>
|
* if `options` is not a {@link ThreadChannelResolvable}</warn>
|
||||||
@ -116,38 +116,44 @@ class ThreadManager extends CachedManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Obtains a set of archived threads from Discord, requires `READ_MESSAGE_HISTORY` in the parent channel.
|
* Obtains a set of archived threads from Discord, requires `READ_MESSAGE_HISTORY` in the parent channel.
|
||||||
* @param {FetchArchivedThreadOptions} [options] The options to fetch archived threads
|
* @param {FetchChannelThreadsOptions} [options] The options to fetch archived threads
|
||||||
* @param {boolean} [cache=true] Whether to cache the new thread objects if they aren't already
|
* @param {boolean} [cache=true] Whether to cache the new thread objects if they aren't already
|
||||||
* @returns {Promise<FetchedThreads>}
|
* @returns {Promise<FetchedThreads>}
|
||||||
*/
|
*/
|
||||||
async fetchArchived({ type = 'public', fetchAll = false, before, limit } = {}, cache = true) {
|
async fetchArchived(options = {}, cache = true) {
|
||||||
let path = this.client.api.channels(this.channel.id);
|
if (this.client.user.bot) {
|
||||||
if (type === 'private' && !fetchAll) {
|
let { type = 'public', fetchAll = false, before, limit } = options;
|
||||||
path = path.users('@me');
|
let path = this.client.api.channels(this.channel.id);
|
||||||
}
|
if (type === 'private' && !fetchAll) {
|
||||||
let timestamp;
|
path = path.users('@me');
|
||||||
let id;
|
}
|
||||||
if (typeof before !== 'undefined') {
|
let timestamp;
|
||||||
if (before instanceof ThreadChannel || /^\d{16,19}$/.test(String(before))) {
|
let id;
|
||||||
id = this.resolveId(before);
|
if (typeof before !== 'undefined') {
|
||||||
timestamp = this.resolve(before)?.archivedAt?.toISOString();
|
if (before instanceof ThreadChannel || /^\d{16,19}$/.test(String(before))) {
|
||||||
} else {
|
id = this.resolveId(before);
|
||||||
try {
|
timestamp = this.resolve(before)?.archivedAt?.toISOString();
|
||||||
timestamp = new Date(before).toISOString();
|
} else {
|
||||||
} catch {
|
try {
|
||||||
throw new TypeError('INVALID_TYPE', 'before', 'DateResolvable or ThreadChannelResolvable');
|
timestamp = new Date(before).toISOString();
|
||||||
|
} catch {
|
||||||
|
throw new TypeError('INVALID_TYPE', 'before', 'DateResolvable or ThreadChannelResolvable');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const raw = await path.threads
|
||||||
|
.archived(type)
|
||||||
|
.get({ query: { before: type === 'private' && !fetchAll ? id : timestamp, limit } });
|
||||||
|
return this.constructor._mapThreads(raw, this.client, { parent: this.channel, cache });
|
||||||
|
} else {
|
||||||
|
return this.fetchActive(cache, { archived: true, ...options });
|
||||||
}
|
}
|
||||||
const raw = await path.threads
|
|
||||||
.archived(type)
|
|
||||||
.get({ query: { before: type === 'private' && !fetchAll ? id : timestamp, limit } });
|
|
||||||
return this.constructor._mapThreads(raw, this.client, { parent: this.channel, cache });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Discord.js self-bot specific options field for fetching active threads.
|
* Discord.js self-bot specific options field for fetching active threads.
|
||||||
* @typedef {Object} FetchChannelThreadsOptions
|
* @typedef {Object} FetchChannelThreadsOptions
|
||||||
|
* @property {boolean} [archived] Whether to fetch archived threads (default is false)
|
||||||
* @property {string} [sortBy] The order in which the threads should be fetched in (default is last_message_time)
|
* @property {string} [sortBy] The order in which the threads should be fetched in (default is last_message_time)
|
||||||
* @property {string} [sortOrder] How the threads should be ordered (default is desc)
|
* @property {string} [sortOrder] How the threads should be ordered (default is desc)
|
||||||
* @property {number} [limit] The maximum number of threads to return (default is 25)
|
* @property {number} [limit] The maximum number of threads to return (default is 25)
|
||||||
@ -169,7 +175,7 @@ class ThreadManager extends CachedManager {
|
|||||||
? await this.client.api.guilds(this.channel.guild.id).threads.active.get()
|
? await this.client.api.guilds(this.channel.guild.id).threads.active.get()
|
||||||
: await this.client.api.channels(this.channel.id).threads.search.get({
|
: await this.client.api.channels(this.channel.id).threads.search.get({
|
||||||
query: {
|
query: {
|
||||||
archived: false,
|
archived: options?.archived ?? false,
|
||||||
limit: options?.limit ?? 25,
|
limit: options?.limit ?? 25,
|
||||||
offset: options?.offset ?? 0,
|
offset: options?.offset ?? 0,
|
||||||
sort_by: options?.sortBy ?? 'last_message_time',
|
sort_by: options?.sortBy ?? 'last_message_time',
|
||||||
@ -188,6 +194,10 @@ class ThreadManager extends CachedManager {
|
|||||||
}, new Collection());
|
}, new Collection());
|
||||||
// Discord sends the thread id as id in this object
|
// Discord sends the thread id as id in this object
|
||||||
for (const rawMember of rawThreads.members) client.channels.cache.get(rawMember.id)?.members._add(rawMember);
|
for (const rawMember of rawThreads.members) client.channels.cache.get(rawMember.id)?.members._add(rawMember);
|
||||||
|
// Patch firstMessage
|
||||||
|
for (const rawMessage of rawThreads.first_messages) {
|
||||||
|
client.channels.cache.get(rawMessage.id)?.messages._add(rawMessage);
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
threads,
|
threads,
|
||||||
hasMore: rawThreads.has_more ?? false,
|
hasMore: rawThreads.has_more ?? false,
|
||||||
|
@ -51,6 +51,15 @@ class ThreadChannel extends Channel {
|
|||||||
if (data) this._patch(data, fromInteraction);
|
if (data) this._patch(data, fromInteraction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* First message in the thread
|
||||||
|
* @type {?Message}
|
||||||
|
* @readonly
|
||||||
|
*/
|
||||||
|
get firstMessage() {
|
||||||
|
return this.messages.cache.get(this.id);
|
||||||
|
}
|
||||||
|
|
||||||
_patch(data, partial = false) {
|
_patch(data, partial = false) {
|
||||||
super._patch(data);
|
super._patch(data);
|
||||||
|
|
||||||
|
15
typings/index.d.ts
vendored
15
typings/index.d.ts
vendored
@ -2976,6 +2976,7 @@ export class TextInputComponent extends BaseMessageComponent {
|
|||||||
export class ThreadChannel extends TextBasedChannelMixin(Channel, ['fetchWebhooks', 'createWebhook', 'setNSFW']) {
|
export class ThreadChannel extends TextBasedChannelMixin(Channel, ['fetchWebhooks', 'createWebhook', 'setNSFW']) {
|
||||||
private constructor(guild: Guild, data?: RawThreadChannelData, client?: Client, fromInteraction?: boolean);
|
private constructor(guild: Guild, data?: RawThreadChannelData, client?: Client, fromInteraction?: boolean);
|
||||||
public archived: boolean | null;
|
public archived: boolean | null;
|
||||||
|
public readonly firstMessage: Message | null;
|
||||||
public readonly archivedAt: Date | null;
|
public readonly archivedAt: Date | null;
|
||||||
public archiveTimestamp: number | null;
|
public archiveTimestamp: number | null;
|
||||||
public readonly createdAt: Date | null;
|
public readonly createdAt: Date | null;
|
||||||
@ -4178,9 +4179,9 @@ export class ThreadManager extends CachedManager<Snowflake, ThreadChannel, Threa
|
|||||||
protected constructor(channel: TextChannel | NewsChannel, iterable?: Iterable<RawThreadChannelData>);
|
protected constructor(channel: TextChannel | NewsChannel, iterable?: Iterable<RawThreadChannelData>);
|
||||||
public channel: TextChannel | NewsChannel;
|
public channel: TextChannel | NewsChannel;
|
||||||
public fetch(options: ThreadChannelResolvable, cacheOptions?: BaseFetchOptions): Promise<ThreadChannel | null>;
|
public fetch(options: ThreadChannelResolvable, cacheOptions?: BaseFetchOptions): Promise<ThreadChannel | null>;
|
||||||
public fetch(options?: FetchThreadsOptions, cacheOptions?: { cache?: boolean }): Promise<FetchedThreads>;
|
public fetch(options?: FetchChannelThreadsOptions, cacheOptions?: { cache?: boolean }): Promise<FetchedThreads>;
|
||||||
public fetchArchived(options?: FetchArchivedThreadOptions, cache?: boolean): Promise<FetchedThreads>;
|
public fetchArchived(options?: FetchChannelThreadsOptions, cache?: boolean): Promise<FetchedThreads>;
|
||||||
public fetchActive(cache?: boolean): Promise<FetchedThreads>;
|
public fetchActive(cache?: boolean, options?: FetchChannelThreadsOptions): Promise<FetchedThreads>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ThreadMemberManager extends CachedManager<Snowflake, ThreadMember, ThreadMemberResolvable> {
|
export class ThreadMemberManager extends CachedManager<Snowflake, ThreadMember, ThreadMemberResolvable> {
|
||||||
@ -5708,6 +5709,14 @@ export interface FetchArchivedThreadOptions {
|
|||||||
limit?: number;
|
limit?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface FetchChannelThreadsOptions {
|
||||||
|
archived?: boolean;
|
||||||
|
limit?: number;
|
||||||
|
offset?: number;
|
||||||
|
sort_by?: string;
|
||||||
|
sort_order?: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface FetchBanOptions extends BaseFetchOptions {
|
export interface FetchBanOptions extends BaseFetchOptions {
|
||||||
user: UserResolvable;
|
user: UserResolvable;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user