From 8b2a554c911ba0ec1ec857fe16a3c96a0fac64be Mon Sep 17 00:00:00 2001 From: ITOH Date: Thu, 27 May 2021 16:38:34 +0200 Subject: [PATCH] add: message sweeper --- src/cache.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/cache.ts b/src/cache.ts index 02f30ca09..b93bd7773 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -14,7 +14,7 @@ export const cache = { /** All of the channel objects the bot has access to, mapped by their Ids */ channels: new Collection(), /** All of the message objects the bot has cached since the bot acquired `READY` state, mapped by their Ids */ - messages: new Collection(), + messages: new Collection([], { sweeper: { filter: messageSweeper, interval: 300000 } }), /** All of the member objects that have been cached since the bot acquired `READY` state, mapped by their Ids */ members: new Collection(), /** All of the unavailable guilds, mapped by their Ids (id, timestamp) */ @@ -33,6 +33,16 @@ export const cache = { }, }; +function messageSweeper(message: DiscordenoMessage) { + // DM messages aren't needed + if (!message.guildId) return true; + + // Only delete messages older than 10 minutes + if (Date.now() - message.timestamp > 600000) return true; + + return false; +} + export let cacheHandlers = { /** Deletes all items from the cache */ async clear(table: TableName) {