test fix cache for tri

This commit is contained in:
Skillz4Killz
2021-11-09 17:57:49 +00:00
committed by GitHub
parent 4d845db515
commit 1c64bf50ac
2 changed files with 55 additions and 42 deletions
+15 -6
View File
@@ -127,7 +127,7 @@ type CacheOptions =
export function createBot<C extends CacheOptions = CacheOptions>(
options: CreateBotOptions<C>
): Bot<C extends { isAsync: true } ? AsyncCache : Cache> {
return {
const bot = {
id: options.botId,
applicationId: options.applicationId || options.botId,
token: `Bot ${options.token}`,
@@ -138,9 +138,12 @@ export function createBot<C extends CacheOptions = CacheOptions>(
activeGuildIds: new Set<bigint>(),
constants: createBotConstants(),
handlers: createBotGatewayHandlers({}),
// @ts-ignore b quiet
cache: createCache(options?.cache?.isAsync ?? false, options?.cache?.customTableCreator),
} as unknown as Bot<C extends { isAsync: true } ? AsyncCache : Cache>;
}
// @ts-ignore itoh cache types plz
bot.cache = createCache(bot as Bot, options.cache);
return bot as unknown as Bot<C extends { isAsync: true } ? AsyncCache : Cache>;
}
export function createEventHandlers(events: Partial<EventHandlers>): 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();
+40 -36
View File
@@ -48,36 +48,40 @@ function channelSweeper(bot: Bot<Cache>, channel: DiscordenoChannel, key: bigint
}
export function createCache(
isAsync: true,
// deno-lint-ignore no-explicit-any
tableCreator: (tableName: TableNames) => AsyncCacheHandler<any>
bot: Bot,
options: {
isAsync: true;
tableCreator: (bot: Bot, tableName: TableNames) => AsyncCacheHandler<any>;
}
): AsyncCache;
export function createCache(
isAsync: false,
// deno-lint-ignore no-explicit-any
tableCreator?: (tableName: TableNames) => CacheHandler<any>
bot: Bot,
options: {
isAsync: false;
tableCreator?: (bot: Bot, tableName: TableNames) => CacheHandler<any>;
}
): Cache;
export function createCache(
isAsync: boolean,
tableCreator?: (
tableName: TableNames
// deno-lint-ignore no-explicit-any
) => CacheHandler<any> | AsyncCacheHandler<any>
bot: Bot,
options: {
isAsync: boolean;
tableCreator?: (bot: Bot, tableName: TableNames) => CacheHandler<any> | AsyncCacheHandler<any>;
}
): Omit<Cache, "execute"> | Omit<AsyncCache, "execute"> {
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<T>(_table: TableNames): CacheHandler<T> {
function createTable<T>(bot: Bot, _table: TableNames): CacheHandler<T> {
const table = new Collection<bigint, T>();
// @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(),