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) { export async function getActiveThreads(channelId: bigint) {
await requireBotChannelPermissions(channelId, ["VIEW_CHANNEL"]); await requireBotChannelPermissions(channelId, ["VIEW_CHANNEL"]);
// TODO: pagination
const result = (await rest.runMethod("get", endpoints.THREAD_ACTIVE(channelId))) as ListActiveThreads; const result = (await rest.runMethod("get", endpoints.THREAD_ACTIVE(channelId))) as ListActiveThreads;
const threads = new Collection( const threads = new Collection(
@@ -1,8 +1,12 @@
import { rest } from "../../../rest/rest.ts"; 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 { ListPublicArchivedThreads } from "../../../types/channels/threads/list_public_archived_threads.ts";
import { PermissionStrings } from "../../../types/permissions/permission_strings.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 { endpoints } from "../../../util/constants.ts";
import { requireBotChannelPermissions } from "../../../util/permissions.ts"; import { requireBotChannelPermissions } from "../../../util/permissions.ts";
import { channelToThread } from "../../../util/transformers/channel_to_thread.ts";
import { snakelize } from "../../../util/utils.ts"; import { snakelize } from "../../../util/utils.ts";
/** Get the archived threads for this channel, defaults to public */ /** 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"]); const permissions = new Set<PermissionStrings>(["READ_MESSAGE_HISTORY"]);
if (options?.type === "private") permissions.add("MANAGE_THREADS"); if (options?.type === "private") permissions.add("MANAGE_THREADS");
requireBotChannelPermissions(channelId, [...permissions]); await requireBotChannelPermissions(channelId, [...permissions]);
// TODO(threads): check if this works
// TODO: v12 map the result to a nice collection or maybe not, check what it returns // TODO: pagination
return await rest.runMethod(
const result = (await rest.runMethod(
"get", "get",
options?.type === "privateJoinedThreads" options?.type === "privateJoinedThreads"
? endpoints.THREAD_ARCHIVED_PRIVATE_JOINED(channelId) ? endpoints.THREAD_ARCHIVED_PRIVATE_JOINED(channelId)
@@ -27,5 +31,23 @@ export async function getArchivedThreads(
? endpoints.THREAD_ARCHIVED_PRIVATE(channelId) ? endpoints.THREAD_ARCHIVED_PRIVATE(channelId)
: endpoints.THREAD_ARCHIVED_PUBLIC(channelId), : endpoints.THREAD_ARCHIVED_PUBLIC(channelId),
snakelize(options ?? {}) 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 });
}
+1 -1
View File
@@ -1,4 +1,4 @@
// TODO: add docs link /** https://discord.com/developers/docs/resources/channel#modify-channel-json-params-thread */
export interface ModifyThread { export interface ModifyThread {
/** 2-100 character thread name */ /** 2-100 character thread name */
name?: string; name?: string;
+1
View File
@@ -1,6 +1,7 @@
import { botId, eventHandlers } from "../bot.ts"; import { botId, eventHandlers } from "../bot.ts";
import { cache } from "../cache.ts"; import { cache } from "../cache.ts";
import { getChannels } from "../helpers/channels/get_channels.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 { getGuild } from "../helpers/guilds/get_guild.ts";
import { getMember } from "../helpers/members/get_member.ts"; import { getMember } from "../helpers/members/get_member.ts";
import { structures } from "../structures/mod.ts"; import { structures } from "../structures/mod.ts";