mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-18 00:33:59 +00:00
fix(bots/discord): correct sticky messages logic
This commit is contained in:
@@ -90,13 +90,20 @@ export const discord = {
|
|||||||
Record<
|
Record<
|
||||||
string,
|
string,
|
||||||
{
|
{
|
||||||
forceSendTimerActive?: boolean
|
/**
|
||||||
timeoutMs: number
|
* Chat is active, so force send timer is also active
|
||||||
forceSendMs?: number
|
*/
|
||||||
|
forceTimerActive: boolean
|
||||||
|
/**
|
||||||
|
* There was a message sent, so the timer is active
|
||||||
|
*/
|
||||||
|
timerActive: boolean
|
||||||
|
timerMs: number
|
||||||
|
forceTimerMs?: number
|
||||||
send: (forced?: boolean) => Promise<void>
|
send: (forced?: boolean) => Promise<void>
|
||||||
currentMessage?: Message<true>
|
currentMessage?: Message<true>
|
||||||
interval?: NodeJS.Timeout
|
timer?: NodeJS.Timeout
|
||||||
forceSendInterval?: NodeJS.Timeout
|
forceTimer?: NodeJS.Timeout
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
>,
|
>,
|
||||||
|
|||||||
@@ -7,24 +7,31 @@ 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.interval) store.interval = setTimeout(store.send, store.timeoutMs) as NodeJS.Timeout
|
// 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 {
|
else {
|
||||||
store.interval.refresh()
|
// If there is a timer, but it isn't active, restart it
|
||||||
|
if (!store.timerActive) store.timer.refresh()
|
||||||
|
// If there is a timer and it is active, but the force timer isn't active...
|
||||||
|
else if (!store.forceTimerActive && store.forceTimerMs) {
|
||||||
|
logger.debug(`Channel ${msg.channelId} in guild ${msg.guildId} is active, starting force send timer and clearing existing timer`)
|
||||||
|
|
||||||
if (!store.forceSendTimerActive && store.forceSendMs) {
|
// Clear the timer
|
||||||
logger.debug(`Channel ${msg.channelId} in guild ${msg.guildId} is active, starting force send timer`)
|
clearTimeout(store.timer)
|
||||||
|
store.timerActive = false
|
||||||
|
store.forceTimerActive = true
|
||||||
|
|
||||||
store.forceSendTimerActive = true
|
// (Re)start the force timer
|
||||||
|
if (!store.forceTimer)
|
||||||
if (!store.forceSendInterval)
|
store.forceTimer = setTimeout(
|
||||||
store.forceSendInterval = setTimeout(
|
|
||||||
() =>
|
() =>
|
||||||
store.send(true).then(() => {
|
store.send(true).then(() => {
|
||||||
store.forceSendTimerActive = false
|
store.forceTimerActive = false
|
||||||
}),
|
}),
|
||||||
store.forceSendMs,
|
store.forceTimerMs,
|
||||||
) as NodeJS.Timeout
|
) as NodeJS.Timeout
|
||||||
else store.forceSendInterval.refresh()
|
else store.forceTimer.refresh()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -14,11 +14,16 @@ export default withContext(on, 'ready', async ({ config, discord, logger }, clie
|
|||||||
if (config.stickyMessages)
|
if (config.stickyMessages)
|
||||||
for (const [guildId, channels] of Object.entries(config.stickyMessages)) {
|
for (const [guildId, channels] of Object.entries(config.stickyMessages)) {
|
||||||
const guild = await client.guilds.fetch(guildId)
|
const guild = await client.guilds.fetch(guildId)
|
||||||
|
// In case of configuration refresh, this will not be nullable
|
||||||
|
const oldStore = discord.stickyMessages[guildId]
|
||||||
discord.stickyMessages[guildId] = {}
|
discord.stickyMessages[guildId] = {}
|
||||||
|
|
||||||
for (const [channelId, { message, timeout, forceSendTimeout }] of Object.entries(channels)) {
|
for (const [channelId, { message, timeout, forceSendTimeout }] of Object.entries(channels)) {
|
||||||
const channel = await guild.channels.fetch(channelId)
|
const channel = await guild.channels.fetch(channelId)
|
||||||
if (!channel?.isTextBased()) return
|
if (!channel?.isTextBased())
|
||||||
|
return void logger.warn(
|
||||||
|
`Channel ${channelId} in guild ${guildId} is not a text channel, sticky messages will not be sent`,
|
||||||
|
)
|
||||||
|
|
||||||
const send = async (forced = false) => {
|
const send = async (forced = false) => {
|
||||||
try {
|
try {
|
||||||
@@ -33,17 +38,19 @@ export default withContext(on, 'ready', async ({ config, discord, logger }, clie
|
|||||||
await store.currentMessage?.delete().catch()
|
await store.currentMessage?.delete().catch()
|
||||||
store.currentMessage = msg
|
store.currentMessage = msg
|
||||||
|
|
||||||
if (!forced) {
|
// Clear any remaining timers
|
||||||
clearTimeout(store.forceSendInterval)
|
clearTimeout(store.timer)
|
||||||
|
clearTimeout(store.forceTimer)
|
||||||
|
store.forceTimerActive = store.timerActive = false
|
||||||
|
|
||||||
|
if (!forced)
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`Timeout ended for sticky message in channel ${channelId} in guild ${guildId}, channel is inactive`,
|
`Timeout ended for sticky message in channel ${channelId} in guild ${guildId}, channel is inactive`,
|
||||||
)
|
)
|
||||||
} else {
|
else
|
||||||
clearTimeout(store.interval)
|
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`Forced send timeout for sticky message in channel ${channelId} in guild ${guildId} ended, channel is too active`,
|
`Forced send timeout for sticky message in channel ${channelId} in guild ${guildId} ended, channel is too active`,
|
||||||
)
|
)
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug(`Sent sticky message to channel ${channelId} in guild ${guildId}`)
|
logger.debug(`Sent sticky message to channel ${channelId} in guild ${guildId}`)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -54,12 +61,19 @@ export default withContext(on, 'ready', async ({ config, discord, logger }, clie
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set up the store
|
||||||
discord.stickyMessages[guildId]![channelId] = {
|
discord.stickyMessages[guildId]![channelId] = {
|
||||||
forceSendMs: forceSendTimeout,
|
forceTimerActive: false,
|
||||||
timeoutMs: timeout,
|
timerActive: false,
|
||||||
|
forceTimerMs: forceSendTimeout,
|
||||||
|
timerMs: timeout,
|
||||||
send,
|
send,
|
||||||
forceSendTimerActive: false,
|
// 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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user