feat: list active threads

This commit is contained in:
Socram03 2025-06-13 04:11:07 -04:00
parent 3f6c6dc4d4
commit 422cfb2a80
2 changed files with 48 additions and 28 deletions

View File

@ -1,15 +1,16 @@
import { CacheFrom } from '../..'; import { CacheFrom } from '../..';
import type { ThreadChannelStructure } from '../../client/transformers'; import type { ThreadChannelStructure } from '../../client/transformers';
import { channelFrom } from '../../structures'; import { channelFrom } from '../../structures';
import type { import {
APIThreadChannel, type APIThreadChannel,
APIThreadMember, type APIThreadMember,
RESTGetAPIChannelThreadMembersQuery, ChannelType,
RESTGetAPIChannelThreadsArchivedQuery, type RESTGetAPIChannelThreadMembersQuery,
RESTPatchAPIChannelJSONBody, type RESTGetAPIChannelThreadsArchivedQuery,
RESTPostAPIChannelMessagesThreadsJSONBody, type RESTPatchAPIChannelJSONBody,
RESTPostAPIChannelThreadsJSONBody, type RESTPostAPIChannelMessagesThreadsJSONBody,
RESTPostAPIGuildForumThreadsJSONBody, type RESTPostAPIChannelThreadsJSONBody,
type RESTPostAPIGuildForumThreadsJSONBody,
} from '../../types'; } from '../../types';
import type { MakeRequired, When } from '../types/util'; import type { MakeRequired, When } from '../types/util';
import { BaseShorter } from './base'; import { BaseShorter } from './base';
@ -44,27 +45,22 @@ export class ThreadShorter extends BaseShorter {
); );
} }
fromMessage( async fromMessage(
channelId: string, channelId: string,
messageId: string, messageId: string,
options: RESTPostAPIChannelMessagesThreadsJSONBody & { reason?: string }, options: RESTPostAPIChannelMessagesThreadsJSONBody & { reason?: string },
): Promise<ThreadChannelStructure> { ): Promise<ThreadChannelStructure> {
const { reason, ...body } = options; const { reason, ...body } = options;
return this.client.proxy const thread = await this.client.proxy.channels(channelId).messages(messageId).threads.post({ body, reason });
.channels(channelId) await this.client.cache.channels?.setIfNI(
.messages(messageId) CacheFrom.Rest,
.threads.post({ body, reason }) 'Guilds',
.then(async thread => { thread.id,
await this.client.cache.channels?.setIfNI( (thread as APIThreadChannel).guild_id!,
CacheFrom.Rest, thread,
'Guilds', );
thread.id, return await (channelFrom(thread, this.client) as ThreadChannelStructure);
(thread as APIThreadChannel).guild_id!,
thread,
);
return channelFrom(thread, this.client) as ThreadChannelStructure;
});
} }
join(threadId: string) { join(threadId: string) {
@ -75,8 +71,9 @@ export class ThreadShorter extends BaseShorter {
return this.client.proxy.channels(threadId)['thread-members']('@me').delete(); return this.client.proxy.channels(threadId)['thread-members']('@me').delete();
} }
lock(threadId: string, locked = true, reason?: string): Promise<ThreadChannelStructure> { async lock(threadId: string, locked = true, reason?: string): Promise<ThreadChannelStructure> {
return this.edit(threadId, { locked }, reason).then(x => channelFrom(x, this.client) as ThreadChannelStructure); const x = await this.edit(threadId, { locked }, reason);
return channelFrom(x, this.client) as ThreadChannelStructure;
} }
async edit(threadId: string, body: RESTPatchAPIChannelJSONBody, reason?: string): Promise<ThreadChannelStructure> { async edit(threadId: string, body: RESTPatchAPIChannelJSONBody, reason?: string): Promise<ThreadChannelStructure> {
@ -110,7 +107,7 @@ export class ThreadShorter extends BaseShorter {
return this.client.proxy.channels(threadId)['thread-members'].get({ query }) as never; return this.client.proxy.channels(threadId)['thread-members'].get({ query }) as never;
} }
async listArchivedThreads( async listArchived(
channelId: string, channelId: string,
type: 'public' | 'private', type: 'public' | 'private',
query?: RESTGetAPIChannelThreadsArchivedQuery, query?: RESTGetAPIChannelThreadsArchivedQuery,
@ -128,6 +125,25 @@ export class ThreadShorter extends BaseShorter {
}; };
} }
async listGuildActive(guildId: string, force = false): Promise<ThreadChannelStructure[]> {
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( async listJoinedArchivedPrivate(
channelId: string, channelId: string,
query?: RESTGetAPIChannelThreadsArchivedQuery, query?: RESTGetAPIChannelThreadsArchivedQuery,

View File

@ -1,4 +1,4 @@
import type { GuildMemberStructure, GuildStructure } from '../client'; import type { GuildMemberStructure, GuildStructure, ThreadChannelStructure } from '../client';
import type { UsingClient } from '../commands'; import type { UsingClient } from '../commands';
import type { CreateInviteFromChannel } from '../common'; import type { CreateInviteFromChannel } from '../common';
import type { ObjectToLower, StructPropState, StructStates, ToClass } from '../common/types/util'; import type { ObjectToLower, StructPropState, StructStates, ToClass } from '../common/types/util';
@ -71,6 +71,10 @@ export class Guild<State extends StructStates = 'api'> extends (BaseGuild as unk
return this.members.fetch(this.ownerId, force); return this.members.fetch(this.ownerId, force);
} }
async listActiveThreads(force = false): Promise<ThreadChannelStructure[]> {
return this.client.threads.listGuildActive(this.id, force);
}
templates = GuildTemplate.methods({ client: this.client, guildId: this.id }); templates = GuildTemplate.methods({ client: this.client, guildId: this.id });
stickers = Sticker.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 }); members = GuildMember.methods({ client: this.client, guildId: this.id });