From ea0673c25962d62baed835f5f2f8cd24051ef29c Mon Sep 17 00:00:00 2001 From: ITOH Date: Thu, 8 Jul 2021 19:54:55 +0200 Subject: [PATCH] change filter logic --- src/cache.ts | 61 +++++++++++++-------- src/handlers/members/GUILD_MEMBERS_CHUNK.ts | 2 +- src/helpers/channels/category_children.ts | 10 ++-- 3 files changed, 44 insertions(+), 29 deletions(-) diff --git a/src/cache.ts b/src/cache.ts index 13e8cdec3..d862d6338 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -236,30 +236,43 @@ async function forEach( } } +// async function filter( +// table: "threads", +// callback: (value: DiscordenoThread, key: bigint) => boolean +// ): Promise>; +// async function filter( +// table: "guilds", +// callback: (value: DiscordenoGuild, key: bigint) => boolean +// ): Promise>; +// async function filter( +// table: "unavailableGuilds", +// callback: (value: number, key: bigint) => boolean +// ): Promise>; +// async function filter( +// table: "channels", +// callback: (value: DiscordenoChannel, key: bigint) => boolean +// ): Promise>; +// async function filter( +// table: "messages", +// callback: (value: DiscordenoMessage, key: bigint) => boolean +// ): Promise>; +// async function filter( +// table: "members", +// callback: (value: DiscordenoMember, key: bigint) => boolean +// ): Promise>; +// async function filter(table: TableName, callback: (value: any, key: bigint) => boolean) { +// return cache[table].filter(callback); +// } + async function filter( - table: "threads", - callback: (value: DiscordenoThread, key: bigint) => boolean -): Promise>; -async function filter( - table: "guilds", - callback: (value: DiscordenoGuild, key: bigint) => boolean -): Promise>; -async function filter( - table: "unavailableGuilds", - callback: (value: number, key: bigint) => boolean -): Promise>; -async function filter( - table: "channels", - callback: (value: DiscordenoChannel, key: bigint) => boolean -): Promise>; -async function filter( - table: "messages", - callback: (value: DiscordenoMessage, key: bigint) => boolean -): Promise>; -async function filter( - table: "members", - callback: (value: DiscordenoMember, key: bigint) => boolean + type: "GET_MEMBERS_IN_GUILD", + options: { guildId: bigint } ): Promise>; -async function filter(table: TableName, callback: (value: any, key: bigint) => boolean) { - return cache[table].filter(callback); +async function filter( + type: "GET_MEMBERS_IN_GUILD", + options?: Record +): Promise | undefined> { + if (type === "GET_MEMBERS_IN_GUILD") { + return cache.members.filter((member) => member.guilds.has(options?.guildId as bigint)); + } } diff --git a/src/handlers/members/GUILD_MEMBERS_CHUNK.ts b/src/handlers/members/GUILD_MEMBERS_CHUNK.ts index 006eda9f8..61a326e8f 100644 --- a/src/handlers/members/GUILD_MEMBERS_CHUNK.ts +++ b/src/handlers/members/GUILD_MEMBERS_CHUNK.ts @@ -31,7 +31,7 @@ export async function handleGuildMembersChunk(data: DiscordGatewayPayload) { return resolve(new Collection(members.map((m) => [m.id, m]))); } - return resolve(await cacheHandlers.filter("members", (m) => m.guilds.has(guildId))); + return resolve(await cacheHandlers.filter("GET_MEMBERS_IN_GUILD", { guildId })); } } } diff --git a/src/helpers/channels/category_children.ts b/src/helpers/channels/category_children.ts index b3c32e520..c0f31ad83 100644 --- a/src/helpers/channels/category_children.ts +++ b/src/helpers/channels/category_children.ts @@ -1,6 +1,8 @@ -import { cacheHandlers } from "../../cache.ts"; +import { cache } from "../../cache.ts"; -/** Gets an array of all the channels ids that are the children of this category. */ -export async function categoryChildren(id: bigint) { - return await cacheHandlers.filter("channels", (channel) => channel.parentId === id); +/** Gets an array of all the channels ids that are the children of this category. + * ⚠️ This does not work for custom cache users! + */ +export function categoryChildren(parentChannelId: bigint) { + return cache.channels.filter((channel) => channel.parentId === parentChannelId); }