blob: e43f82c728fc994ce5c4914e8c8fefc0159999e3 [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"];
TheCodedProfbaee2c12023-02-18 16:11:06 -050010 autoPublish: GuildConfig["autoPublish"];
Skyler Grey75ea9172022-08-06 10:22:23 +010011}
pineafane625d782022-05-09 18:04:32 +010012
13class Memory {
Skyler Grey75ea9172022-08-06 10:22:23 +010014 memory: Map<string, GuildData>;
pineafane625d782022-05-09 18:04:32 +010015 constructor() {
Skyler Grey75ea9172022-08-06 10:22:23 +010016 this.memory = new Map<string, GuildData>();
pineafan6702cef2022-06-13 17:52:37 +010017
18 setInterval(() => {
Skyler Grey75ea9172022-08-06 10:22:23 +010019 for (const [guild, guildData] of this.memory.entries()) {
20 if (guildData.lastUpdated + 15 * 60 * 1000 < Date.now()) {
21 this.memory.delete(guild);
pineafan6702cef2022-06-13 17:52:37 +010022 }
23 }
pineafan63fc5e22022-08-04 22:04:10 +010024 }, 1000 * 60 * 30);
Skyler Greyda16adf2023-03-05 10:22:12 +000025 }
pineafane625d782022-05-09 18:04:32 +010026
Skyler Grey75ea9172022-08-06 10:22:23 +010027 async readGuildInfo(guild: string): Promise<GuildData> {
28 if (!this.memory.has(guild)) {
pineafan63fc5e22022-08-04 22:04:10 +010029 const guildData = await client.database.guilds.read(guild);
Skyler Grey75ea9172022-08-06 10:22:23 +010030 this.memory.set(guild, {
pineafan6702cef2022-06-13 17:52:37 +010031 lastUpdated: Date.now(),
pineafane625d782022-05-09 18:04:32 +010032 filters: guildData.filters,
33 logging: guildData.logging,
PineaFana34d04b2023-01-03 22:05:42 +000034 tickets: guildData.tickets,
TheCodedProfbaee2c12023-02-18 16:11:06 -050035 tags: guildData.tags,
36 autoPublish: guildData.autoPublish
Skyler Grey75ea9172022-08-06 10:22:23 +010037 });
pineafan63fc5e22022-08-04 22:04:10 +010038 }
Skyler Grey75ea9172022-08-06 10:22:23 +010039 return this.memory.get(guild)!;
Skyler Greyda16adf2023-03-05 10:22:12 +000040 }
PineaFana34d04b2023-01-03 22:05:42 +000041
42 async forceUpdate(guild: string) {
43 if (this.memory.has(guild)) this.memory.delete(guild);
pineafane625d782022-05-09 18:04:32 +010044 }
45}
46
pineafan6702cef2022-06-13 17:52:37 +010047export default Memory;