mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-11 13:56:15 +00:00
feat(bots/discord): delete and send sticky msg concurrently, add more logging
This commit is contained in:
@@ -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`,
|
`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
|
// Set up the store
|
||||||
discord.stickyMessages[guildId]![channelId] = {
|
// biome-ignore lint/suspicious/noAssignInExpressions: don't care
|
||||||
|
const store = (discord.stickyMessages[guildId]![channelId] = {
|
||||||
forceTimerActive: false,
|
forceTimerActive: false,
|
||||||
timerActive: false,
|
timerActive: false,
|
||||||
forceTimerMs: forceSendTimeout,
|
forceTimerMs: forceSendTimeout,
|
||||||
timerMs: timeout,
|
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
|
// 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
|
// Send a new sticky message immediately, as well as deleting the old/outdated message, if it exists
|
||||||
await send()
|
await store.send()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user