blob: a397d093d83437696dde75fda2838b1a2ba9fa9a [file] [log] [blame]
pineafan6702cef2022-06-13 17:52:37 +01001import client from "./client.js";
Skyler Grey75ea9172022-08-06 10:22:23 +01002import type { GuildConfig } from "./database.js";
3
4interface GuildData {
5 lastUpdated: number;
6 filters: GuildConfig["filters"];
7 logging: GuildConfig["logging"];
8 tickets: GuildConfig["tickets"];
9}
pineafane625d782022-05-09 18:04:32 +010010
11class Memory {
Skyler Grey75ea9172022-08-06 10:22:23 +010012 memory: Map<string, GuildData>;
pineafane625d782022-05-09 18:04:32 +010013 constructor() {
Skyler Grey75ea9172022-08-06 10:22:23 +010014 this.memory = new Map<string, GuildData>();
pineafan6702cef2022-06-13 17:52:37 +010015
16 setInterval(() => {
Skyler Grey75ea9172022-08-06 10:22:23 +010017 for (const [guild, guildData] of this.memory.entries()) {
18 if (guildData.lastUpdated + 15 * 60 * 1000 < Date.now()) {
19 this.memory.delete(guild);
pineafan6702cef2022-06-13 17:52:37 +010020 }
21 }
pineafan63fc5e22022-08-04 22:04:10 +010022 }, 1000 * 60 * 30);
pineafane625d782022-05-09 18:04:32 +010023 }
24
Skyler Grey75ea9172022-08-06 10:22:23 +010025 async readGuildInfo(guild: string): Promise<GuildData> {
26 if (!this.memory.has(guild)) {
pineafan63fc5e22022-08-04 22:04:10 +010027 const guildData = await client.database.guilds.read(guild);
Skyler Grey75ea9172022-08-06 10:22:23 +010028 this.memory.set(guild, {
pineafan6702cef2022-06-13 17:52:37 +010029 lastUpdated: Date.now(),
pineafane625d782022-05-09 18:04:32 +010030 filters: guildData.filters,
31 logging: guildData.logging,
pineafan63fc5e22022-08-04 22:04:10 +010032 tickets: guildData.tickets
Skyler Grey75ea9172022-08-06 10:22:23 +010033 });
pineafan63fc5e22022-08-04 22:04:10 +010034 }
Skyler Grey75ea9172022-08-06 10:22:23 +010035 return this.memory.get(guild)!;
pineafane625d782022-05-09 18:04:32 +010036 }
37}
38
pineafan6702cef2022-06-13 17:52:37 +010039export default Memory;