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