Merge branch 'more-thread-stuff' of https://github.com/discordeno/discordeno into more-thread-stuff

This commit is contained in:
Skillz4Killz
2021-06-17 21:55:51 +00:00
committed by GitHub
3 changed files with 51 additions and 16 deletions
@@ -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;
}