mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-11 13:56:15 +00:00
fix(bots/discord): fix sticky messages logic again
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -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
|
store.timerActive = true
|
||||||
if (store.forceTimerMs && !store.forceTimerActive) {
|
if (store.timer) store.timer.refresh()
|
||||||
// Force timer isn't active, so we start it
|
else store.timer = setTimeout(store.send, store.timerMs) as NodeJS.Timeout
|
||||||
logger.debug(
|
|
||||||
`Channel ${msg.channelId} in guild ${msg.guildId} is active, starting force send timer and clearing existing timer`,
|
|
||||||
)
|
|
||||||
|
|
||||||
// Clear the timer
|
// Timer is already active, and force timer isn't active, so we start the latter
|
||||||
clearTimeout(store.timer)
|
if (store.timerActive && store.forceTimerMs && !store.forceTimerActive) {
|
||||||
store.timerActive = false
|
logger.debug(`Channel ${msg.channelId} in guild ${msg.guildId} is active, starting force send timer`)
|
||||||
|
|
||||||
// Start the force timer
|
// (Re)start the force timer
|
||||||
store.forceTimerActive = true
|
store.forceTimerActive = true
|
||||||
// biome-ignore lint/suspicious/noAssignInExpressions: This works fine
|
if (store.forceTimer) store.forceTimer.refresh()
|
||||||
;(store.forceTimer ??= setTimeout(store.send, store.forceTimerMs)).refresh()
|
else store.forceTimer = setTimeout(store.send, store.forceTimerMs)
|
||||||
}
|
|
||||||
} 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
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -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}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user