From 3ed5bd11acc3b4fbd57b0d632c68eb9f77365b8a Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Thu, 17 Oct 2024 21:40:41 +0700 Subject: [PATCH] fix(bots/discord): attempt to fix stuck sticky message timeouts --- .../messageCreate/stickyMessageReset.ts | 40 +++---------------- bots/discord/src/events/discord/ready.ts | 4 +- 2 files changed, 7 insertions(+), 37 deletions(-) diff --git a/bots/discord/src/events/discord/messageCreate/stickyMessageReset.ts b/bots/discord/src/events/discord/messageCreate/stickyMessageReset.ts index d674f00..e55122b 100644 --- a/bots/discord/src/events/discord/messageCreate/stickyMessageReset.ts +++ b/bots/discord/src/events/discord/messageCreate/stickyMessageReset.ts @@ -7,41 +7,8 @@ withContext(on, 'messageCreate', async ({ discord, logger }, msg) => { const store = discord.stickyMessages[msg.guildId]?.[msg.channelId] if (!store) return - // TODO: Fix this by fixing the logic below - if (store.timerActive && store.forceTimerActive) { - logger.error( - `Both timers are active in sticky message store: ${msg.guildId}.${msg.channelId}, this should not happen!`, - ) - logger.info('Clearing the timer and the restarting the force timer...') - clearTimeout(store.timer) - store.timerActive = false - // If the force timer is active, it implies the force timer exists - store.forceTimer!.refresh() - } - - const timerPreviouslyActive = store.timerActive - // If there isn't a timer, start it up - store.timerActive = true - if (!store.timer) store.timer = setTimeout(store.send, store.timerMs) as NodeJS.Timeout - else { - /* - If: - - (negate carried) There's a timer - - The timer is not active - - The force timer is not active - Then: - - Restart the timer - */ - if (!timerPreviouslyActive && !store.forceTimerActive) store.timer.refresh() - /* - If: - - Any of: - - (negate carried) The timer is active - - (negate carried) The force timer is active - - The force timer is not active - Then: - - Start the force timer and clear the existing timer - */ else if (!store.forceTimerActive && store.forceTimerMs) { + if (store.timerActive) { + if (!store.forceTimerActive && store.forceTimerMs) { logger.debug( `Channel ${msg.channelId} in guild ${msg.guildId} is active, starting force send timer and clearing existing timer`, ) @@ -62,5 +29,8 @@ withContext(on, 'messageCreate', async ({ discord, logger }, msg) => { ) as NodeJS.Timeout else store.forceTimer.refresh() } + } else if (!store.forceTimerActive) { + store.timerActive = true + if (!store.timer) store.timer = setTimeout(store.send, store.timerMs) as NodeJS.Timeout } }) diff --git a/bots/discord/src/events/discord/ready.ts b/bots/discord/src/events/discord/ready.ts index f4f6dd9..a719d4b 100644 --- a/bots/discord/src/events/discord/ready.ts +++ b/bots/discord/src/events/discord/ready.ts @@ -60,7 +60,7 @@ export default withContext(on, 'ready', async ({ config, discord, logger }, clie ) } } - + // Set up the store discord.stickyMessages[guildId]![channelId] = { forceTimerActive: false, @@ -69,7 +69,7 @@ export default withContext(on, 'ready', async ({ config, discord, logger }, clie timerMs: timeout, send, // If the store exists before the configuration refresh, take its current message - currentMessage: oldStore?.[channelId]?.currentMessage + currentMessage: oldStore?.[channelId]?.currentMessage, } // Send a new sticky message immediately, as well as deleting the old/outdated message, if it exists