feat(bots/discord): delete and send sticky msg concurrently, add more logging

This commit is contained in:
PalmDevs
2025-04-14 21:00:24 +07:00
parent 0da3c989cd
commit 247a00f57f

View File

@@ -16,47 +16,53 @@ 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 () => {
const store = discord.stickyMessages[guildId]![channelId]
if (!store) return
try {
const oldMsg = store.currentMessage
store.currentMessage = await channel.send({
...message,
embeds: message.embeds?.map(it => applyCommonEmbedStyles(it, true, true, true)),
})
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}`)
}
}
// Set up the store
discord.stickyMessages[guildId]![channelId] = {
// biome-ignore lint/suspicious/noAssignInExpressions: don't care
const store = (discord.stickyMessages[guildId]![channelId] = {
forceTimerActive: false,
timerActive: false,
forceTimerMs: forceSendTimeout,
timerMs: timeout,
send,
async send() {
try {
await Promise.all([
channel
.send({
...message,
embeds: message.embeds?.map(it => applyCommonEmbedStyles(it, true, true, true)),
})
.then(msg => {
this.currentMessage = msg
logger.debug(`Sent sticky message to channel ${channelId} in guild ${guildId}`)
}),
this.currentMessage
?.delete()
?.then(() =>
logger.debug(
`Deleted old sticky message from channel ${channelId} in guild ${guildId}`,
),
),
])
} catch (e) {
logger.error(
`Error while managing sticky message of channel ${channelId} in guild ${guildId}:`,
e,
)
} finally {
// Clear any remaining timers
clearTimeout(this.timer)
clearTimeout(this.forceTimer)
this.forceTimerActive = this.timerActive = false
logger.debug(`Cleared sticky message timer for channel ${channelId} in guild ${guildId}`)
}
},
// If the store exists before the configuration refresh, take its current message
currentMessage: oldStore?.[channelId]?.currentMessage,
}
})
// Send a new sticky message immediately, as well as deleting the old/outdated message, if it exists
await send()
await store.send()
}
}
})