From 228c71d3bd47d8cf42479cf35ec4aeae490f9929 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Wed, 3 Nov 2021 22:32:19 +0000 Subject: [PATCH] Closes #1179 --- src/cache.ts | 2 ++ tests/helpers/channels/categoryChannels.ts | 40 ++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 tests/helpers/channels/categoryChannels.ts diff --git a/src/cache.ts b/src/cache.ts index e8f6b045f..16615d79d 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -170,6 +170,8 @@ export type CacheExecutor = ( export function createExecute(cache: Cache): CacheExecutor { return function (type, options) { switch (type) { + case "FILTER_CATEGORY_CHILDREN_CHANNELS": + return cache.channels.filter(c => c.parentId === options.parentChannelId); case "DELETE_MESSAGES_FROM_CHANNEL": cache.messages.forEach((message) => { if (message.channelId === options.channelId) { diff --git a/tests/helpers/channels/categoryChannels.ts b/tests/helpers/channels/categoryChannels.ts new file mode 100644 index 000000000..e0228a689 --- /dev/null +++ b/tests/helpers/channels/categoryChannels.ts @@ -0,0 +1,40 @@ +import { Bot } from "../../../src/bot.ts"; +import { DiscordChannelTypes } from "../../../src/types/channels/channel_types.ts"; +import { assertExists } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; + +export async function categoryChildrenTest(bot: Bot, guildId: bigint, t: Deno.TestContext) { + const category = await bot.helpers.createChannel(guildId, { + name: "Discordeno-test", + type: DiscordChannelTypes.GuildCategory, + }); + + // Assertions + assertExists(category); + // Delay the execution to allow event to be processed + await delayUntil(10000, () => bot.cache.channels.has(category.id)); + + assertExists(bot.cache.channels.has(category.id)); + + const channelsToCreate = [1, 2, 3, 4, 5]; + const channels = await Promise.all( + channelsToCreate.map((num) => + bot.helpers.createChannel(guildId, { + name: `Discordeno-test-${num}`, + parentId: category.id, + }) + ) + ); + // Delay the execution to allow event to be processed + await delayUntil(10000, () => channels.every((c) => bot.cache.channels.has(c.id))); + + // If every channel is not present in the cache, error out + if (!channels.every((c) => bot.cache.channels.has(c.id))) { + throw new Error("The channels seemed to be created but it was not cached."); + } + + const ids = await bot.helpers.categoryChildren(category.id); + if (ids.size !== channelsToCreate.length || !channels.every((c) => ids.has(c.id))) { + throw new Error("The category channel ids did not match with the category channels."); + } +}