From aa7501c3097a790265e4ea624d07c4a9c3c1b908 Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Mon, 14 Apr 2025 19:39:19 +0700 Subject: [PATCH] fix(bots/discord): fix sticky messages logic again --- bots/discord/src/context.ts | 2 +- .../messageCreate/stickyMessageReset.ts | 33 ++++++---------- bots/discord/src/events/discord/ready.ts | 38 ++++++++----------- 3 files changed, 27 insertions(+), 46 deletions(-) diff --git a/bots/discord/src/context.ts b/bots/discord/src/context.ts index c63468b..0a4d1e2 100644 --- a/bots/discord/src/context.ts +++ b/bots/discord/src/context.ts @@ -101,7 +101,7 @@ export const discord = { timerActive: boolean timerMs: number forceTimerMs?: number - send: (forced?: boolean) => Promise + send: () => Promise currentMessage?: Message timer?: NodeJS.Timeout forceTimer?: NodeJS.Timeout diff --git a/bots/discord/src/events/discord/messageCreate/stickyMessageReset.ts b/bots/discord/src/events/discord/messageCreate/stickyMessageReset.ts index fa59926..20b4263 100644 --- a/bots/discord/src/events/discord/messageCreate/stickyMessageReset.ts +++ b/bots/discord/src/events/discord/messageCreate/stickyMessageReset.ts @@ -7,29 +7,18 @@ withContext(on, 'messageCreate', async ({ discord, logger }, msg) => { const store = discord.stickyMessages[msg.guildId]?.[msg.channelId] if (!store) return - if (store.timerActive) { - // Timer is already active, so we try to start the force timer - if (store.forceTimerMs && !store.forceTimerActive) { - // Force timer isn't active, so we start it - logger.debug( - `Channel ${msg.channelId} in guild ${msg.guildId} is active, starting force send timer and clearing existing timer`, - ) + // (Re)start the timer + store.timerActive = true + if (store.timer) store.timer.refresh() + else store.timer = setTimeout(store.send, store.timerMs) as NodeJS.Timeout - // Clear the timer - clearTimeout(store.timer) - store.timerActive = false + // Timer is already active, and force timer isn't active, so we start the latter + if (store.timerActive && store.forceTimerMs && !store.forceTimerActive) { + logger.debug(`Channel ${msg.channelId} in guild ${msg.guildId} is active, starting force send timer`) - // Start the force timer - store.forceTimerActive = true - // biome-ignore lint/suspicious/noAssignInExpressions: This works fine - ;(store.forceTimer ??= setTimeout(store.send, store.forceTimerMs)).refresh() - } - } else if (store.forceTimerActive) { - // Force timer is already active, so force send the message, and clear the force timer - store.send(true) - } else { - // Both timers aren't active, so we start the timer - store.timerActive = true - if (!store.timer) store.timer = setTimeout(store.send, store.timerMs) as NodeJS.Timeout + // (Re)start the force timer + store.forceTimerActive = true + if (store.forceTimer) store.forceTimer.refresh() + else store.forceTimer = setTimeout(store.send, store.forceTimerMs) } }) diff --git a/bots/discord/src/events/discord/ready.ts b/bots/discord/src/events/discord/ready.ts index 05122b2..f204c50 100644 --- a/bots/discord/src/events/discord/ready.ts +++ b/bots/discord/src/events/discord/ready.ts @@ -25,39 +25,31 @@ export default withContext(on, 'ready', async ({ config, discord, logger }, clie `Channel ${channelId} in guild ${guildId} is not a text channel, sticky messages will not be sent`, ) - const send = async (forced = false) => { + const send = async () => { + const store = discord.stickyMessages[guildId]![channelId] + if (!store) return + try { - const msg = await channel.send({ + const oldMsg = store.currentMessage + + store.currentMessage = await channel.send({ ...message, embeds: message.embeds?.map(it => applyCommonEmbedStyles(it, true, true, true)), }) - const store = discord.stickyMessages[guildId]![channelId] - if (!store) return - - await store.currentMessage?.delete().catch() - store.currentMessage = msg - - // Clear any remaining timers - clearTimeout(store.timer) - clearTimeout(store.forceTimer) - store.forceTimerActive = store.timerActive = false - - if (!forced) - logger.debug( - `Timer ended for sticky message in channel ${channelId} in guild ${guildId}, channel is inactive`, - ) - else - logger.debug( - `Force timer for sticky message in channel ${channelId} in guild ${guildId} hasn't ended but a message was sent, channel is too active`, - ) - - logger.debug(`Sent sticky message to channel ${channelId} in guild ${guildId}`) + await oldMsg?.delete() } catch (e) { logger.error( `Error while sending sticky message to channel ${channelId} in guild ${guildId}:`, e, ) + } finally { + // Clear any remaining timers + clearTimeout(store.timer) + clearTimeout(store.forceTimer) + store.forceTimerActive = store.timerActive = false + + logger.debug(`Sent sticky message to channel ${channelId} in guild ${guildId}`) } }