fix(bots/discord): attempt to fix stuck sticky message timeouts

This commit is contained in:
PalmDevs
2024-10-17 21:40:41 +07:00
parent 8ff6086028
commit 3ed5bd11ac
2 changed files with 7 additions and 37 deletions

View File

@@ -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
}
})

View File

@@ -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