From a7d11bf7bb892bbab580bd534d91594e71855ab0 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Fri, 18 Jun 2021 00:02:54 +0000 Subject: [PATCH] feat: finalizing threads --- .../channels/threads/archive_thread.ts | 6 ++++ .../channels/threads/get_active_threads.ts | 1 + .../channels/threads/get_archived_threads.ts | 30 ++++++++++++++++--- src/helpers/channels/threads/lock_thread.ts | 6 ++++ .../channels/threads/unarchive_thread.ts | 6 ++++ src/helpers/channels/threads/unlock_thread.ts | 6 ++++ src/types/channels/threads/modify_thread.ts | 2 +- src/util/dispatch_requirements.ts | 1 + 8 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 src/helpers/channels/threads/archive_thread.ts create mode 100644 src/helpers/channels/threads/lock_thread.ts create mode 100644 src/helpers/channels/threads/unarchive_thread.ts create mode 100644 src/helpers/channels/threads/unlock_thread.ts diff --git a/src/helpers/channels/threads/archive_thread.ts b/src/helpers/channels/threads/archive_thread.ts new file mode 100644 index 000000000..1a3260fc0 --- /dev/null +++ b/src/helpers/channels/threads/archive_thread.ts @@ -0,0 +1,6 @@ +import { editChannel } from "../edit_channel.ts"; + +/** Sets a thread channel to be archived. */ +export function archiveThread(threadId: bigint) { + return editChannel(threadId, { archived: true }); +} diff --git a/src/helpers/channels/threads/get_active_threads.ts b/src/helpers/channels/threads/get_active_threads.ts index bd8dc04db..6c963ba81 100644 --- a/src/helpers/channels/threads/get_active_threads.ts +++ b/src/helpers/channels/threads/get_active_threads.ts @@ -10,6 +10,7 @@ import { channelToThread } from "../../../util/transformers/channel_to_thread.ts export async function getActiveThreads(channelId: bigint) { await requireBotChannelPermissions(channelId, ["VIEW_CHANNEL"]); + // TODO: pagination const result = (await rest.runMethod("get", endpoints.THREAD_ACTIVE(channelId))) as ListActiveThreads; const threads = new Collection( diff --git a/src/helpers/channels/threads/get_archived_threads.ts b/src/helpers/channels/threads/get_archived_threads.ts index e9cd45507..430746d5e 100644 --- a/src/helpers/channels/threads/get_archived_threads.ts +++ b/src/helpers/channels/threads/get_archived_threads.ts @@ -1,8 +1,12 @@ import { rest } from "../../../rest/rest.ts"; +import { ListActiveThreads } from "../../../types/channels/threads/list_active_threads.ts"; import { ListPublicArchivedThreads } from "../../../types/channels/threads/list_public_archived_threads.ts"; import { PermissionStrings } from "../../../types/permissions/permission_strings.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"; import { snakelize } from "../../../util/utils.ts"; /** Get the archived threads for this channel, defaults to public */ @@ -15,11 +19,11 @@ export async function getArchivedThreads( const permissions = new Set(["READ_MESSAGE_HISTORY"]); if (options?.type === "private") permissions.add("MANAGE_THREADS"); - requireBotChannelPermissions(channelId, [...permissions]); - // TODO(threads): check if this works + await requireBotChannelPermissions(channelId, [...permissions]); - // TODO: v12 map the result to a nice collection or maybe not, check what it returns - return await rest.runMethod( + // TODO: pagination + + const result = (await rest.runMethod( "get", options?.type === "privateJoinedThreads" ? endpoints.THREAD_ARCHIVED_PRIVATE_JOINED(channelId) @@ -27,5 +31,23 @@ export async function getArchivedThreads( ? endpoints.THREAD_ARCHIVED_PRIVATE(channelId) : endpoints.THREAD_ARCHIVED_PUBLIC(channelId), snakelize(options ?? {}) + )) as ListActiveThreads; + + 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/helpers/channels/threads/lock_thread.ts b/src/helpers/channels/threads/lock_thread.ts new file mode 100644 index 000000000..f2ec4cdf0 --- /dev/null +++ b/src/helpers/channels/threads/lock_thread.ts @@ -0,0 +1,6 @@ +import { editChannel } from "../edit_channel.ts"; + +/** Sets a thread channel to be locked. */ +export function lockThread(threadId: bigint) { + return editChannel(threadId, { locked: true }); +} diff --git a/src/helpers/channels/threads/unarchive_thread.ts b/src/helpers/channels/threads/unarchive_thread.ts new file mode 100644 index 000000000..709257b56 --- /dev/null +++ b/src/helpers/channels/threads/unarchive_thread.ts @@ -0,0 +1,6 @@ +import { editChannel } from "../edit_channel.ts"; + +/** Sets a thread channel to be unarchived. */ +export function unarchiveThread(threadId: bigint) { + return editChannel(threadId, { archived: false }); +} \ No newline at end of file diff --git a/src/helpers/channels/threads/unlock_thread.ts b/src/helpers/channels/threads/unlock_thread.ts new file mode 100644 index 000000000..ffa391d4d --- /dev/null +++ b/src/helpers/channels/threads/unlock_thread.ts @@ -0,0 +1,6 @@ +import { editChannel } from "../edit_channel.ts"; + +/** Sets a thread channel to be unlocked. */ +export function unlockThread(threadId: bigint) { + return editChannel(threadId, { locked: false }); +} \ No newline at end of file diff --git a/src/types/channels/threads/modify_thread.ts b/src/types/channels/threads/modify_thread.ts index d261cab1d..24dc79963 100644 --- a/src/types/channels/threads/modify_thread.ts +++ b/src/types/channels/threads/modify_thread.ts @@ -1,4 +1,4 @@ -// TODO: add docs link +/** https://discord.com/developers/docs/resources/channel#modify-channel-json-params-thread */ export interface ModifyThread { /** 2-100 character thread name */ name?: string; diff --git a/src/util/dispatch_requirements.ts b/src/util/dispatch_requirements.ts index 612024f60..a2ace0a54 100644 --- a/src/util/dispatch_requirements.ts +++ b/src/util/dispatch_requirements.ts @@ -1,6 +1,7 @@ import { botId, eventHandlers } from "../bot.ts"; import { cache } from "../cache.ts"; import { getChannels } from "../helpers/channels/get_channels.ts"; +import { getActiveThreads } from "../helpers/channels/threads/get_active_threads.ts"; import { getGuild } from "../helpers/guilds/get_guild.ts"; import { getMember } from "../helpers/members/get_member.ts"; import { structures } from "../structures/mod.ts";