feat: finalizing threads

This commit is contained in:
Skillz4Killz
2021-06-18 00:02:54 +00:00
committed by GitHub
parent c63d5b9aeb
commit a7d11bf7bb
8 changed files with 53 additions and 5 deletions
@@ -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 });
}
@@ -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(
@@ -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<PermissionStrings>(["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;
}
@@ -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 });
}
@@ -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 });
}
@@ -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 });
}