From 8613dd9f4d7ed225bca900272fa2e24df015ec9a Mon Sep 17 00:00:00 2001 From: Essem Date: Sun, 7 Jun 2026 17:46:43 -0500 Subject: [PATCH] collections: Apply map timeout fix from API to TimedMap --- src/classes/mediaCommand.ts | 6 ++---- src/utils/collections.ts | 23 ++++++++++++++++++++--- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/classes/mediaCommand.ts b/src/classes/mediaCommand.ts index d77316d5..fe9b8d66 100644 --- a/src/classes/mediaCommand.ts +++ b/src/classes/mediaCommand.ts @@ -42,10 +42,8 @@ class MediaCommand extends Command { : (this.message?.createdAt ?? new Date()); // check if this command has already been run in this channel with the same arguments, and we are awaiting its result // if so, don't re-run it - if ( - runningCommands.has(this.author?.id) && - runningCommands.get(this.author?.id).getTime() - timestamp.getTime() < 5000 - ) { + const running = runningCommands.get(this.author?.id); + if (running && running.getTime() - timestamp.getTime() < 5000) { return this.getString("image.slowDown"); } // before awaiting the command result, add this command to the set of running commands diff --git a/src/utils/collections.ts b/src/utils/collections.ts index d0cecc35..1fbaf375 100644 --- a/src/utils/collections.ts +++ b/src/utils/collections.ts @@ -13,19 +13,36 @@ export const collectors = new Map(); export const locales = new Map(); -class TimedMap extends Map { +class TimedMap extends Map { time: number; + private timeouts: Map> = new Map(); + constructor(time: number) { super(); this.time = time; } + set(key: K, value: V) { super.set(key, value); - setTimeout(() => { - if (super.has(key)) super.delete(key); + const oldHandle = this.timeouts.get(key); + if (oldHandle) clearTimeout(oldHandle); + + const handle = setTimeout(() => { + this.delete(key); }, this.time); + this.timeouts.set(key, handle); return this; } + + delete(key: K) { + const out = super.delete(key); + const handle = this.timeouts.get(key); + if (handle) { + clearTimeout(handle); + this.timeouts.delete(key); + } + return out; + } } export const runningCommands = new TimedMap(5000); -- 2.51.2