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