diff --git a/src/helpers/channels/threads/get_active_threads.ts b/src/helpers/channels/threads/get_active_threads.ts index 45ab33513..bd8dc04db 100644 --- a/src/helpers/channels/threads/get_active_threads.ts +++ b/src/helpers/channels/threads/get_active_threads.ts @@ -1,14 +1,32 @@ import { rest } from "../../../rest/rest.ts"; +import { ListActiveThreads } from "../../../types/channels/threads/list_active_threads.ts"; +import { snowflakeToBigint } from "../../../util/bigint.ts"; +import { Collection } from "../../../util/collection.ts"; import { endpoints } from "../../../util/constants.ts"; import { requireBotChannelPermissions } from "../../../util/permissions.ts"; +import { channelToThread } from "../../../util/transformers/channel_to_thread.ts"; -/** Returns all active threads in the channel, including public and private threads. Threads are ordered by their id, in descending order. Requires the READ_MESSAGE_HISTORY permission. */ +/** Returns all active threads in the channel, including public and private threads. Threads are ordered by their id, in descending order. Requires the VIEW_CHANNEL permission. */ export async function getActiveThreads(channelId: bigint) { - await requireBotChannelPermissions(channelId, ["READ_MESSAGE_HISTORY"]); + await requireBotChannelPermissions(channelId, ["VIEW_CHANNEL"]); - // TODO: pagination?? seriously doubt discord will send thousands of threads at once - // TODO: max limits? + const result = (await rest.runMethod("get", endpoints.THREAD_ACTIVE(channelId))) as ListActiveThreads; - // TODO: v12 map the result to a nice collection, check what it returns - return await rest.runMethod("get", endpoints.THREAD_ACTIVE(channelId)); + const threads = new Collection( + result.threads.map((t) => { + const ddThread = channelToThread(t); + return [ddThread.id, ddThread]; + }) + ); + + for (const member of result.members) { + const thread = threads.get(snowflakeToBigint(member.id)); + thread?.members.set(snowflakeToBigint(member.userId), { + userId: snowflakeToBigint(member.userId), + flags: member.flags, + joinTimestamp: Date.parse(member.joinTimestamp), + }); + } + + return threads; } diff --git a/src/structures/guild.ts b/src/structures/guild.ts index 3483a30b3..5bc78fd52 100644 --- a/src/structures/guild.ts +++ b/src/structures/guild.ts @@ -26,6 +26,7 @@ import { snowflakeToBigint } from "../util/bigint.ts"; import { cacheMembers } from "../util/cache_members.ts"; import { Collection } from "../util/collection.ts"; import { iconHashToBigInt } from "../util/hash.ts"; +import { channelToThread } from "../util/transformers/channel_to_thread.ts"; import { createNewProp } from "../util/utils.ts"; import { DiscordenoChannel } from "./channel.ts"; import { DiscordenoMember } from "./member.ts"; @@ -245,6 +246,26 @@ export async function createDiscordenoGuild(data: Guild, shardId: number) { let bitfield = 0n; const guildId = snowflakeToBigint(rest.id); + const promises = []; + + for (const channel of channels) { + promises.push(async () => { + const discordenoChannel = await structures.createDiscordenoChannel(channel, guildId); + + return cacheHandlers.set("channels", discordenoChannel.id, discordenoChannel); + }); + } + + for (const thread of threads) { + promises.push(() => { + const discordenoThread = channelToThread(thread); + + return cacheHandlers.set("threads", discordenoThread.id, discordenoThread); + }); + } + + await Promise.all(promises); + const roles = await Promise.all( (data.roles || []).map((role) => structures.createDiscordenoRole({ @@ -261,14 +282,6 @@ export async function createDiscordenoGuild(data: Guild, shardId: number) { }) ); - await Promise.all( - [...channels, ...threads].map(async (channel) => { - const discordenoChannel = await structures.createDiscordenoChannel(channel, guildId); - - return await cacheHandlers.set("channels", discordenoChannel.id, discordenoChannel); - }) - ); - const props: Record> = {}; (Object.keys(rest) as (keyof typeof rest)[]).forEach((key) => { eventHandlers.debug?.("loop", `Running for of loop in createDiscordenoGuild function.`); diff --git a/src/util/transformers/channel_to_thread.ts b/src/util/transformers/channel_to_thread.ts index 4d6c2c1bf..d45867ec9 100644 --- a/src/util/transformers/channel_to_thread.ts +++ b/src/util/transformers/channel_to_thread.ts @@ -1,6 +1,8 @@ import { Channel } from "../../types/channels/channel.ts"; import { DiscordChannelTypes } from "../../types/channels/channel_types.ts"; +import { ThreadMemberModified } from "../../types/channels/threads/thread_member.ts"; import { snowflakeToBigint } from "../bigint.ts"; +import { Collection } from "../collection.ts"; import { createNewProp } from "../utils.ts"; export const threadToggles = { @@ -60,8 +62,9 @@ export function channelToThread(channel: Channel) { autoArchiveDuration: createNewProp(channel.threadMetadata?.autoArchiveDuration || 0), bitfield: createNewProp(bitfield), ownerId: createNewProp(snowflakeToBigint(channel.ownerId!)), - botIsMember: createNewProp(Boolean(channel.member)) - }); + botIsMember: createNewProp(Boolean(channel.member)), + members: createNewProp(new Collection>()), + }) as DiscordenoThread; } export interface Thread { @@ -101,5 +104,6 @@ export interface DiscordenoThread { isPrivate: boolean; isPublic: boolean; botIsMember: boolean; + members: Collection>; toJSON(): Thread; }