mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-11 05:46:16 +00:00
fix(bots/discord): fix sticky messages logic again
This commit is contained in:
@@ -101,7 +101,7 @@ export const discord = {
|
||||
timerActive: boolean
|
||||
timerMs: number
|
||||
forceTimerMs?: number
|
||||
send: (forced?: boolean) => Promise<void>
|
||||
send: () => Promise<void>
|
||||
currentMessage?: Message<true>
|
||||
timer?: NodeJS.Timeout
|
||||
forceTimer?: NodeJS.Timeout
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -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}`)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user