pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 1 | import client from "./client.js"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 2 | import type { GuildConfig } from "./database.js"; |
| 3 | |
| 4 | interface GuildData { |
| 5 | lastUpdated: number; |
| 6 | filters: GuildConfig["filters"]; |
| 7 | logging: GuildConfig["logging"]; |
| 8 | tickets: GuildConfig["tickets"]; |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 9 | tags: GuildConfig["tags"]; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 10 | autoPublish: GuildConfig["autoPublish"]; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 11 | } |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 12 | |
| 13 | class Memory { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 14 | memory: Map<string, GuildData>; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 15 | constructor() { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 16 | this.memory = new Map<string, GuildData>(); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 17 | |
| 18 | setInterval(() => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 19 | for (const [guild, guildData] of this.memory.entries()) { |
| 20 | if (guildData.lastUpdated + 15 * 60 * 1000 < Date.now()) { |
| 21 | this.memory.delete(guild); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 22 | } |
| 23 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 24 | }, 1000 * 60 * 30); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 25 | } |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 26 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 27 | async readGuildInfo(guild: string): Promise<GuildData> { |
| 28 | if (!this.memory.has(guild)) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 29 | const guildData = await client.database.guilds.read(guild); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 30 | this.memory.set(guild, { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 31 | lastUpdated: Date.now(), |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 32 | filters: guildData.filters, |
| 33 | logging: guildData.logging, |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 34 | tickets: guildData.tickets, |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 35 | tags: guildData.tags, |
| 36 | autoPublish: guildData.autoPublish |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 37 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 38 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 39 | return this.memory.get(guild)!; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 40 | } |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 41 | |
| 42 | async forceUpdate(guild: string) { |
| 43 | if (this.memory.has(guild)) this.memory.delete(guild); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 47 | export default Memory; |