blob: 870ffaf7f11e0fedbb7f556164120de402b9082e [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"];
PineaFana34d04b2023-01-03 22:05:42 +00009 tags: GuildConfig["tags"];
Skyler Grey75ea9172022-08-06 10:22:23 +010010}
pineafane625d782022-05-09 18:04:32 +010011
12class Memory {
Skyler Grey75ea9172022-08-06 10:22:23 +010013 memory: Map<string, GuildData>;
pineafane625d782022-05-09 18:04:32 +010014 constructor() {
Skyler Grey75ea9172022-08-06 10:22:23 +010015 this.memory = new Map<string, GuildData>();
pineafan6702cef2022-06-13 17:52:37 +010016
17 setInterval(() => {
Skyler Grey75ea9172022-08-06 10:22:23 +010018 for (const [guild, guildData] of this.memory.entries()) {
19 if (guildData.lastUpdated + 15 * 60 * 1000 < Date.now()) {
20 this.memory.delete(guild);
pineafan6702cef2022-06-13 17:52:37 +010021 }
22 }
pineafan63fc5e22022-08-04 22:04:10 +010023 }, 1000 * 60 * 30);
PineaFana34d04b2023-01-03 22:05:42 +000024 };
pineafane625d782022-05-09 18:04:32 +010025
Skyler Grey75ea9172022-08-06 10:22:23 +010026 async readGuildInfo(guild: string): Promise<GuildData> {
27 if (!this.memory.has(guild)) {
pineafan63fc5e22022-08-04 22:04:10 +010028 const guildData = await client.database.guilds.read(guild);
Skyler Grey75ea9172022-08-06 10:22:23 +010029 this.memory.set(guild, {
pineafan6702cef2022-06-13 17:52:37 +010030 lastUpdated: Date.now(),
pineafane625d782022-05-09 18:04:32 +010031 filters: guildData.filters,
32 logging: guildData.logging,
PineaFana34d04b2023-01-03 22:05:42 +000033 tickets: guildData.tickets,
34 tags: guildData.tags
Skyler Grey75ea9172022-08-06 10:22:23 +010035 });
pineafan63fc5e22022-08-04 22:04:10 +010036 }
Skyler Grey75ea9172022-08-06 10:22:23 +010037 return this.memory.get(guild)!;
PineaFana34d04b2023-01-03 22:05:42 +000038 };
39
40 async forceUpdate(guild: string) {
41 if (this.memory.has(guild)) this.memory.delete(guild);
pineafane625d782022-05-09 18:04:32 +010042 }
43}
44
pineafan6702cef2022-06-13 17:52:37 +010045export default Memory;