From 56e2163d6d28bdfa159cd7877688f00bd5c9054f Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Mon, 8 Nov 2021 16:53:26 +0000 Subject: [PATCH] test enable sweepers --- src/cache.ts | 97 ++++++++++++++++++++++++------------------ src/util/collection.ts | 10 +++-- 2 files changed, 62 insertions(+), 45 deletions(-) diff --git a/src/cache.ts b/src/cache.ts index feb865d4b..887292fc7 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -1,3 +1,4 @@ +import type { Bot } from "./bot.ts"; import type { DiscordenoChannel } from "./transformers/channel.ts"; import type { DiscordenoGuild } from "./transformers/guild.ts"; import type { DiscordenoMember, DiscordenoUser } from "./transformers/member.ts"; @@ -6,6 +7,46 @@ import { DiscordenoPresence } from "./transformers/presence.ts"; import { GuildMember } from "./types/members/guild_member.ts"; import { Collection } from "./util/collection.ts"; +function messageSweeper(bot: Bot, message: DiscordenoMessage) { + // DM messages aren't needed + if (!message.guildId) return true; + + // Only delete messages older than 10 minutes + return Date.now() - message.timestamp > 600000; +} + +function memberSweeper(bot: Bot, member: DiscordenoMember) { + // Don't sweep the bot else strange things will happen + if (member.id === bot.id) return false; + + // Only sweep members who were not active the last 30 minutes + return Date.now() - member.cachedAt > 1800000; +} + +function guildSweeper(bot: Bot, guild: DiscordenoGuild) { + // Reset activity for next interval + if (bot.cache.activeGuildIds.delete(guild.id)) return false; + + // This is inactive guild. Not a single thing has happened for atleast 30 minutes. + // Not a reaction, not a message, not any event! + bot.cache.dispatchedGuildIds.add(guild.id); + + return true; +} + +function channelSweeper(bot: Bot, channel: DiscordenoChannel, key: bigint) { + // If this is in a guild and the guild was dispatched, then we can dispatch the channel + if (channel.guildId && bot.cache.dispatchedGuildIds.has(channel.guildId)) { + bot.cache.dispatchedChannelIds.add(channel.id); + return true; + } + + // THE KEY DM CHANNELS ARE STORED BY IS THE USER ID. If the user is not cached, we dont need to cache their dm channel. + if (!channel.guildId && !bot.cache.members.has(key)) return true; + + return false; +} + export function createCache( isAsync: true, // deno-lint-ignore no-explicit-any @@ -78,8 +119,9 @@ export interface Cache { presences: CacheHandler; // threads: CacheHandler; unavailableGuilds: CacheHandler; - dispatchedGuildIds: CacheHandler; - dispatchedChannelIds: CacheHandler; + dispatchedGuildIds: Set; + dispatchedChannelIds: Set; + activeGuildIds: Set; executedSlashCommands: Set; fetchAllMembersProcessingRequests: Map; execute: CacheExecutor; @@ -109,6 +151,17 @@ export interface AsyncCache { function createTable(_table: TableNames): CacheHandler { const table = new Collection(); + + // @ts-ignore TODO: fix type error itoh pwease + if (_table === "guilds") table.startSweeper({ filter: guildSweeper, interval: 3660000 }); + // @ts-ignore TODO: fix type error itoh pwease + if (_table === "channels") table.startSweeper({ filter: channelSweeper, interval: 3660000 }); + // @ts-ignore TODO: fix type error itoh pwease + if (_table === "messages") table.startSweeper({ filter: messageSweeper, interval: 300000 }); + // @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 }); + return { clear: () => table.clear(), delete: (key) => table.delete(key), @@ -212,43 +265,3 @@ export type TableNames = | "threads" | "unavailableGuilds" | "members"; - -// function messageSweeper(bot: Bot, message: DiscordenoMessage) { -// // DM messages aren't needed -// if (!message.guildId) return true; - -// // Only delete messages older than 10 minutes -// return Date.now() - message.timestamp > 600000; -// } - -// function memberSweeper(bot: Bot, member: DiscordenoMember) { -// // Don't sweep the bot else strange things will happen -// if (member.id === bot.id) return false; - -// // Only sweep members who were not active the last 30 minutes -// return Date.now() - member.cachedAt > 1800000; -// } - -// async function guildSweeper(bot: Bot, guild: DiscordenoGuild) { -// // Reset activity for next interval -// if (await bot.cache.activeGuildIds.delete(guild.id)) return false; - -// // This is inactive guild. Not a single thing has happened for atleast 30 minutes. -// // Not a reaction, not a message, not any event! -// await bot.cache.dispatchedGuildIds.set(guild.id); - -// return true; -// } - -// async function channelSweeper(bot: Bot, channel: DiscordenoChannel, key: bigint) { -// // If this is in a guild and the guild was dispatched, then we can dispatch the channel -// if (channel.guildId && (await bot.cache.dispatchedGuildIds.has(channel.guildId))) { -// await bot.cache.dispatchedChannelIds.set(channel.id); -// return true; -// } - -// // THE KEY DM CHANNELS ARE STORED BY IS THE USER ID. If the user is not cached, we dont need to cache their dm channel. -// if (!channel.guildId && !(await bot.cache.members.has(key))) return true; - -// return false; -// } diff --git a/src/util/collection.ts b/src/util/collection.ts index 894542077..daad789cc 100644 --- a/src/util/collection.ts +++ b/src/util/collection.ts @@ -1,3 +1,5 @@ +import { Bot } from "../bot.ts"; + export class Collection extends Map { maxSize?: number; private sweeper?: CollectionSweeper & { intervalId?: number }; @@ -17,7 +19,7 @@ export class Collection extends Map { this.sweeper = options; this.sweeper.intervalId = setInterval(() => { this.map(async (value, key) => { - if (!(await this.sweeper?.filter(value, key))) return; + if (!(await this.sweeper?.filter(options.bot!, value, key))) return; this.delete(key); return key; @@ -37,7 +39,7 @@ export class Collection extends Map { this.startSweeper({ filter: this.sweeper.filter, interval: newInterval }); } - changeSweeperFilter(newFilter: (value: V, key: K) => boolean | Promise) { + changeSweeperFilter(newFilter: (bot: Bot, value: V, key: K) => boolean | Promise) { if (!this.sweeper) return; this.startSweeper({ filter: newFilter, interval: this.sweeper.interval }); @@ -134,7 +136,9 @@ interface CollectionOptions { interface CollectionSweeper { /** The filter to determine whether an element should be deleted or not */ - filter: (value: V, key: K) => boolean | Promise; + filter: (bot: Bot, value: V, key: K) => boolean | Promise; /** The interval in which the sweeper should run */ interval: number; + /** The bot object itself */ + bot?: Bot; }