From 1c64bf50acd2457b1e57baaf5c62a640af8e2daa Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Tue, 9 Nov 2021 17:57:49 +0000 Subject: [PATCH] test fix cache for tri --- src/bot.ts | 21 ++++++++++----- src/cache.ts | 76 +++++++++++++++++++++++++++------------------------- 2 files changed, 55 insertions(+), 42 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index f538e0da1..44037bd4a 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -127,7 +127,7 @@ type CacheOptions = export function createBot( options: CreateBotOptions ): Bot { - return { + const bot = { id: options.botId, applicationId: options.applicationId || options.botId, token: `Bot ${options.token}`, @@ -138,9 +138,12 @@ export function createBot( activeGuildIds: new Set(), constants: createBotConstants(), handlers: createBotGatewayHandlers({}), - // @ts-ignore b quiet - cache: createCache(options?.cache?.isAsync ?? false, options?.cache?.customTableCreator), - } as unknown as Bot; + } + + // @ts-ignore itoh cache types plz + bot.cache = createCache(bot as Bot, options.cache); + + return bot as unknown as Bot; } export function createEventHandlers(events: Partial): EventHandlers { @@ -260,12 +263,18 @@ export function createRestManager(options: CreateRestManagerOptions) { }; } -export async function startBot(bot: Bot) { - // SETUP +export function setupBot(bot: Bot) { bot.utils = createUtils({}); bot.transformers = createTransformers(bot.transformers || {}); bot.helpers = createHelpers(bot); + return bot; +} + +export async function startBot(bot: Bot) { + // SETUP BOT + bot = setupBot(bot); + // START REST bot.rest = createRestManager({ token: bot.token, debug: bot.events.debug }); if (!bot.botGatewayData) bot.botGatewayData = await bot.helpers.getGatewayBot(); diff --git a/src/cache.ts b/src/cache.ts index 887292fc7..e10b234b1 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -48,36 +48,40 @@ function channelSweeper(bot: Bot, channel: DiscordenoChannel, key: bigint } export function createCache( - isAsync: true, - // deno-lint-ignore no-explicit-any - tableCreator: (tableName: TableNames) => AsyncCacheHandler + bot: Bot, + options: { + isAsync: true; + tableCreator: (bot: Bot, tableName: TableNames) => AsyncCacheHandler; + } ): AsyncCache; export function createCache( - isAsync: false, - // deno-lint-ignore no-explicit-any - tableCreator?: (tableName: TableNames) => CacheHandler + bot: Bot, + options: { + isAsync: false; + tableCreator?: (bot: Bot, tableName: TableNames) => CacheHandler; + } ): Cache; export function createCache( - isAsync: boolean, - tableCreator?: ( - tableName: TableNames - // deno-lint-ignore no-explicit-any - ) => CacheHandler | AsyncCacheHandler + bot: Bot, + options: { + isAsync: boolean; + tableCreator?: (bot: Bot, tableName: TableNames) => CacheHandler | AsyncCacheHandler; + } ): Omit | Omit { - if (isAsync) { - if (!tableCreator) { + if (options.isAsync) { + if (!options.tableCreator) { throw new Error("Async cache requires a tableCreator to be passed."); } const cache = { - guilds: tableCreator("guilds"), - users: tableCreator("users"), - members: tableCreator("members"), - channels: tableCreator("channels"), - messages: tableCreator("messages"), - presences: tableCreator("presences"), - // threads: tableCreator("threads"), - unavailableGuilds: tableCreator("unavailableGuilds"), + guilds: options.tableCreator(bot, "guilds"), + users: options.tableCreator(bot, "users"), + members: options.tableCreator(bot, "members"), + channels: options.tableCreator(bot, "channels"), + messages: options.tableCreator(bot, "messages"), + presences: options.tableCreator(bot, "presences"), + // threads: options.tableCreator(bot, "threads"), + unavailableGuilds: options.tableCreator(bot, "unavailableGuilds"), executedSlashCommands: new Set(), fetchAllMembersProcessingRequests: new Map(), } as AsyncCache; @@ -88,17 +92,17 @@ export function createCache( return cache; } - if (!tableCreator) tableCreator = createTable; + if (!options.tableCreator) options.tableCreator = createTable; const cache = { - guilds: tableCreator("guilds"), - users: tableCreator("users"), - members: tableCreator("members"), - channels: tableCreator("channels"), - messages: tableCreator("messages"), - presences: tableCreator("presences"), - // threads: tableCreator("threads"), - unavailableGuilds: tableCreator("unavailableGuilds"), + guilds: options.tableCreator(bot, "guilds"), + users: options.tableCreator(bot, "users"), + members: options.tableCreator(bot, "members"), + channels: options.tableCreator(bot, "channels"), + messages: options.tableCreator(bot, "messages"), + presences: options.tableCreator(bot, "presences"), + // threads: options.tableCreator(bot, "threads"), + unavailableGuilds: options.tableCreator(bot, "unavailableGuilds"), executedSlashCommands: new Set(), fetchAllMembersProcessingRequests: new Map(), } as Cache; @@ -149,18 +153,18 @@ export interface AsyncCache { execute: CacheExecutor; } -function createTable(_table: TableNames): CacheHandler { +function createTable(bot: Bot, _table: TableNames): CacheHandler { const table = new Collection(); // @ts-ignore TODO: fix type error itoh pwease - if (_table === "guilds") table.startSweeper({ filter: guildSweeper, interval: 3660000 }); + if (_table === "guilds") table.startSweeper({ filter: guildSweeper, interval: 3660000, bot }); // @ts-ignore TODO: fix type error itoh pwease - if (_table === "channels") table.startSweeper({ filter: channelSweeper, interval: 3660000 }); + if (_table === "channels") table.startSweeper({ filter: channelSweeper, interval: 3660000, bot }); // @ts-ignore TODO: fix type error itoh pwease - if (_table === "messages") table.startSweeper({ filter: messageSweeper, interval: 300000 }); + if (_table === "messages") table.startSweeper({ filter: messageSweeper, interval: 300000, bot }); // @ts-ignore TODO: fix type error itoh pwease - if (_table === "members") table.startSweeper({ filter: memberSweeper, interval: 300000 }); - if (_table === "presences") table.startSweeper({ filter: () => true, interval: 300000 }); + if (_table === "members") table.startSweeper({ filter: memberSweeper, interval: 300000, bot }); + if (_table === "presences") table.startSweeper({ filter: () => true, interval: 300000, bot }); return { clear: () => table.clear(),