fix(bots/discord): fix sticky messages logic again

This commit is contained in:
PalmDevs
2025-04-14 19:39:19 +07:00
parent 00118b4a1b
commit aa7501c309
3 changed files with 27 additions and 46 deletions

View File

@@ -101,7 +101,7 @@ export const discord = {
timerActive: boolean timerActive: boolean
timerMs: number timerMs: number
forceTimerMs?: number forceTimerMs?: number
send: (forced?: boolean) => Promise<void> send: () => Promise<void>
currentMessage?: Message<true> currentMessage?: Message<true>
timer?: NodeJS.Timeout timer?: NodeJS.Timeout
forceTimer?: NodeJS.Timeout forceTimer?: NodeJS.Timeout

View File

@@ -7,29 +7,18 @@ withContext(on, 'messageCreate', async ({ discord, logger }, msg) => {
const store = discord.stickyMessages[msg.guildId]?.[msg.channelId] const store = discord.stickyMessages[msg.guildId]?.[msg.channelId]
if (!store) return if (!store) return
if (store.timerActive) { // (Re)start the timer
// 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`,
)
// Clear the timer
clearTimeout(store.timer)
store.timerActive = false
// 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 store.timerActive = true
if (!store.timer) store.timer = setTimeout(store.send, store.timerMs) as NodeJS.Timeout if (store.timer) store.timer.refresh()
else store.timer = setTimeout(store.send, store.timerMs) as NodeJS.Timeout
// 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`)
// (Re)start the force timer
store.forceTimerActive = true
if (store.forceTimer) store.forceTimer.refresh()
else store.forceTimer = setTimeout(store.send, store.forceTimerMs)
} }
}) })

View File

@@ -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`, `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 { try {
const msg = await channel.send({ const oldMsg = store.currentMessage
store.currentMessage = await channel.send({
...message, ...message,
embeds: message.embeds?.map(it => applyCommonEmbedStyles(it, true, true, true)), embeds: message.embeds?.map(it => applyCommonEmbedStyles(it, true, true, true)),
}) })
const store = discord.stickyMessages[guildId]![channelId] await oldMsg?.delete()
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}`)
} catch (e) { } catch (e) {
logger.error( logger.error(
`Error while sending sticky message to channel ${channelId} in guild ${guildId}:`, `Error while sending sticky message to channel ${channelId} in guild ${guildId}:`,
e, 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}`)
} }
} }