From 422cfb2a80a722579839204265adbee9248acf67 Mon Sep 17 00:00:00 2001 From: Socram03 Date: Fri, 13 Jun 2025 04:11:07 -0400 Subject: [PATCH] feat: list active threads --- src/common/shorters/threads.ts | 70 +++++++++++++++++++++------------- src/structures/Guild.ts | 6 ++- 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/common/shorters/threads.ts b/src/common/shorters/threads.ts index 11840d0..0c4f925 100644 --- a/src/common/shorters/threads.ts +++ b/src/common/shorters/threads.ts @@ -1,15 +1,16 @@ import { CacheFrom } from '../..'; import type { ThreadChannelStructure } from '../../client/transformers'; import { channelFrom } from '../../structures'; -import type { - APIThreadChannel, - APIThreadMember, - RESTGetAPIChannelThreadMembersQuery, - RESTGetAPIChannelThreadsArchivedQuery, - RESTPatchAPIChannelJSONBody, - RESTPostAPIChannelMessagesThreadsJSONBody, - RESTPostAPIChannelThreadsJSONBody, - RESTPostAPIGuildForumThreadsJSONBody, +import { + type APIThreadChannel, + type APIThreadMember, + ChannelType, + type RESTGetAPIChannelThreadMembersQuery, + type RESTGetAPIChannelThreadsArchivedQuery, + type RESTPatchAPIChannelJSONBody, + type RESTPostAPIChannelMessagesThreadsJSONBody, + type RESTPostAPIChannelThreadsJSONBody, + type RESTPostAPIGuildForumThreadsJSONBody, } from '../../types'; import type { MakeRequired, When } from '../types/util'; import { BaseShorter } from './base'; @@ -44,27 +45,22 @@ export class ThreadShorter extends BaseShorter { ); } - fromMessage( + async fromMessage( channelId: string, messageId: string, options: RESTPostAPIChannelMessagesThreadsJSONBody & { reason?: string }, ): Promise { const { reason, ...body } = options; - return this.client.proxy - .channels(channelId) - .messages(messageId) - .threads.post({ body, reason }) - .then(async thread => { - await this.client.cache.channels?.setIfNI( - CacheFrom.Rest, - 'Guilds', - thread.id, - (thread as APIThreadChannel).guild_id!, - thread, - ); - return channelFrom(thread, this.client) as ThreadChannelStructure; - }); + const thread = await this.client.proxy.channels(channelId).messages(messageId).threads.post({ body, reason }); + await this.client.cache.channels?.setIfNI( + CacheFrom.Rest, + 'Guilds', + thread.id, + (thread as APIThreadChannel).guild_id!, + thread, + ); + return await (channelFrom(thread, this.client) as ThreadChannelStructure); } join(threadId: string) { @@ -75,8 +71,9 @@ export class ThreadShorter extends BaseShorter { return this.client.proxy.channels(threadId)['thread-members']('@me').delete(); } - lock(threadId: string, locked = true, reason?: string): Promise { - return this.edit(threadId, { locked }, reason).then(x => channelFrom(x, this.client) as ThreadChannelStructure); + async lock(threadId: string, locked = true, reason?: string): Promise { + const x = await this.edit(threadId, { locked }, reason); + return channelFrom(x, this.client) as ThreadChannelStructure; } async edit(threadId: string, body: RESTPatchAPIChannelJSONBody, reason?: string): Promise { @@ -110,7 +107,7 @@ export class ThreadShorter extends BaseShorter { return this.client.proxy.channels(threadId)['thread-members'].get({ query }) as never; } - async listArchivedThreads( + async listArchived( channelId: string, type: 'public' | 'private', query?: RESTGetAPIChannelThreadsArchivedQuery, @@ -128,6 +125,25 @@ export class ThreadShorter extends BaseShorter { }; } + async listGuildActive(guildId: string, force = false): Promise { + if (!force) { + const cached = await this.client.cache.channels?.valuesRaw(guildId); + if (cached) + return cached + .filter(x => + [ChannelType.PublicThread, ChannelType.PrivateThread, ChannelType.AnnouncementThread].includes(x.type), + ) + .map(x => channelFrom(x, this.client) as ThreadChannelStructure); + } + const data = await this.client.proxy.guilds(guildId).threads.active.get(); + return Promise.all( + data.threads.map(async thread => { + await this.client.cache.channels?.setIfNI(CacheFrom.Rest, 'Guilds', thread.id, guildId, thread); + return channelFrom(thread, this.client) as ThreadChannelStructure; + }), + ); + } + async listJoinedArchivedPrivate( channelId: string, query?: RESTGetAPIChannelThreadsArchivedQuery, diff --git a/src/structures/Guild.ts b/src/structures/Guild.ts index 7912ed3..0f465e0 100644 --- a/src/structures/Guild.ts +++ b/src/structures/Guild.ts @@ -1,4 +1,4 @@ -import type { GuildMemberStructure, GuildStructure } from '../client'; +import type { GuildMemberStructure, GuildStructure, ThreadChannelStructure } from '../client'; import type { UsingClient } from '../commands'; import type { CreateInviteFromChannel } from '../common'; import type { ObjectToLower, StructPropState, StructStates, ToClass } from '../common/types/util'; @@ -71,6 +71,10 @@ export class Guild extends (BaseGuild as unk return this.members.fetch(this.ownerId, force); } + async listActiveThreads(force = false): Promise { + return this.client.threads.listGuildActive(this.id, force); + } + templates = GuildTemplate.methods({ client: this.client, guildId: this.id }); stickers = Sticker.methods({ client: this.client, guildId: this.id }); members = GuildMember.methods({ client: this.client, guildId: this.id });