From 3573afe7e03aed5bcf3bb0ab8d39b56a8c08b257 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Tue, 12 Oct 2021 16:36:16 +0000 Subject: [PATCH] start adding lil cache --- src/bot.ts | 48 ++++++++++++++++++++++++++++++- src/transformers/guild.ts | 2 +- src/util/dispatch_requirements.ts | 12 ++++---- 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index 741c1ee4d..1e96972ea 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -21,7 +21,7 @@ import { DiscordenoChannel, transformChannel } from "./transformers/channel.ts"; import { transformVoiceState } from "./transformers/voice_state.ts"; import { transformRole } from "./transformers/role.ts"; import { transformMessage } from "./transformers/message.ts"; -import { transformGuild } from "./transformers/guild.ts"; +import { DiscordenoGuild, transformGuild } from "./transformers/guild.ts"; import { DiscordenoShard } from "./ws/ws.ts"; import { startGateway } from "./ws/start_gateway.ts"; import { spawnShards } from "./ws/spawn_shards.ts"; @@ -63,6 +63,52 @@ export async function createBot(options: CreateBotOptions) { isReady: false, activeGuildIds: new Set(), constants: createBotConstants(), + cache: { + guilds: { + get: async function (id: bigint): Promise { + return {} as any as DiscordenoGuild; + }, + has: async function (id: bigint): Promise { + return false; + }, + set: async function (id: bigint, guild: DiscordenoGuild): Promise { + return; + }, + }, + channels: { + get: async function (id: bigint): Promise { + return {} as any as DiscordenoChannel; + }, + has: async function (id: bigint): Promise { + return false; + }, + set: async function (id: bigint, guild: DiscordenoChannel): Promise { + return; + }, + }, + dispatchedGuildIds: { + has: async function (id: bigint): Promise { + return false; + }, + set: async function (id: bigint): Promise { + return; + }, + delete: async function (id: bigint): Promise { + return; + }, + }, + dispatchedChannelIds: { + has: async function (id: bigint): Promise { + return false; + }, + set: async function (id: bigint): Promise { + return; + }, + delete: async function (id: bigint): Promise { + return; + }, + }, + }, }; } diff --git a/src/transformers/guild.ts b/src/transformers/guild.ts index 5b5f1c025..ce558d6a0 100644 --- a/src/transformers/guild.ts +++ b/src/transformers/guild.ts @@ -10,7 +10,7 @@ import { DiscordenoRole, transformRole } from "./role.ts"; import { DiscordenoVoiceState, transformVoiceState } from "./voice_state.ts"; import { SnakeCasedPropertiesDeep } from "../types/util.ts"; -export function transformGuild(bot: Bot, payload: { guild: SnakeCasedPropertiesDeep } & { shardId: number }) { +export function transformGuild(bot: Bot, payload: { guild: SnakeCasedPropertiesDeep } & { shardId: number }): DiscordenoGuild { return { afkTimeout: payload.guild.afk_timeout, approximateMemberCount: payload.guild.approximate_member_count, diff --git a/src/util/dispatch_requirements.ts b/src/util/dispatch_requirements.ts index b4167e091..fc65bc629 100644 --- a/src/util/dispatch_requirements.ts +++ b/src/util/dispatch_requirements.ts @@ -22,7 +22,7 @@ export async function dispatchRequirements(bot: Bot, data: DiscordGatewayPayload if (!id || bot.activeGuildIds.has(id)) return; // If this guild is in cache, it has not been swept and we can cancel - if (bot.guilds.has(id)) { + if (await bot.cache.guilds.has(id)) { bot.activeGuildIds.add(id); return; } @@ -77,18 +77,18 @@ export async function dispatchRequirements(bot: Bot, data: DiscordGatewayPayload ); } - const guild = await bot.transformers.guild(bot, { + const guild = bot.transformers.guild(bot, { ...rawGuild, member_count: rawGuild.approximateMemberCount, shardId, }); // Add to cache - bot.guilds.set(id, guild); - bot.dispatchedGuildIds.delete(id); + await bot.cache.guilds.set(id, guild); + bot.cache.dispatchedGuildIds.delete(id); channels.forEach((channel) => { - bot.dispatchedChannelIds.delete(channel.id); - bot.channels.set(channel.id, channel); + bot.cache.dispatchedChannelIds.delete(channel.id); + bot.cache.channels.set(channel.id, channel); }); processing.delete(id);