2024-01-19 12:01:34 +00:00
// @ts-check
const DiscordTypes = require ( "discord-api-types/v10" )
const passthrough = require ( "../../passthrough" )
2024-02-07 01:52:12 +00:00
const { discord , select , db } = passthrough
2024-01-19 12:01:34 +00:00
const SPEEDBUMP _SPEED = 4000 // 4 seconds delay
const SPEEDBUMP _UPDATE _FREQUENCY = 2 * 60 * 60 // 2 hours
/** @type {Set<any>} */
const KNOWN _BOTS = new Set ( [
"466378653216014359" // PluralKit
] )
/ * *
* Fetch new speedbump data for the channel and put it in the database as cache
* @ param { string } channelID
2024-01-22 09:05:59 +00:00
* @ param { number ? } lastChecked
2024-01-19 12:01:34 +00:00
* /
2024-01-22 09:05:59 +00:00
async function updateCache ( channelID , lastChecked ) {
2024-01-19 12:01:34 +00:00
const now = Math . floor ( Date . now ( ) / 1000 )
2024-01-22 09:05:59 +00:00
if ( lastChecked && now - lastChecked < SPEEDBUMP _UPDATE _FREQUENCY ) return
2024-01-19 12:01:34 +00:00
const webhooks = await discord . snow . webhook . getChannelWebhooks ( channelID )
2024-01-20 11:54:18 +00:00
const found = webhooks . find ( b => KNOWN _BOTS . has ( b . application _id ) )
const foundApplication = found ? . application _id
const foundWebhook = found ? . id
db . prepare ( "UPDATE channel_room SET speedbump_id = ?, speedbump_webhook_id = ?, speedbump_checked = ? WHERE channel_id = ?" ) . run ( foundApplication , foundWebhook , now , channelID )
2024-01-19 12:01:34 +00:00
}
/** @type {Set<string>} set of messageID */
const bumping = new Set ( )
/ * *
* Slow down a message . After it passes the speedbump , return whether it 's okay or if it' s been deleted .
* @ param { string } messageID
2024-02-07 01:52:12 +00:00
* @ returns whether it was deleted
2024-01-19 12:01:34 +00:00
* /
async function doSpeedbump ( messageID ) {
bumping . add ( messageID )
await new Promise ( resolve => setTimeout ( resolve , SPEEDBUMP _SPEED ) )
return ! bumping . delete ( messageID )
}
2024-02-07 01:52:12 +00:00
/ * *
* Check whether to slow down a message , and do it . After it passes the speedbump , return whether it 's okay or if it' s been deleted .
* @ param { string } channelID
* @ param { string } messageID
* @ returns whether it was deleted , and data about the channel 's (not thread' s ) speedbump
* /
async function maybeDoSpeedbump ( channelID , messageID ) {
let row = select ( "channel_room" , [ "thread_parent" , "speedbump_id" , "speedbump_webhook_id" ] , { channel _id : channelID } ) . get ( )
if ( row ? . thread _parent ) row = select ( "channel_room" , [ "thread_parent" , "speedbump_id" , "speedbump_webhook_id" ] , { channel _id : row . thread _parent } ) . get ( ) // webhooks belong to the channel, not the thread
2024-02-12 10:07:55 +00:00
if ( ! row ? . speedbump _webhook _id ) return { affected : false , row : null } // not affected, no speedbump
2024-02-07 01:52:12 +00:00
const affected = await doSpeedbump ( messageID )
return { affected , row } // maybe affected, and there is a speedbump
}
2024-01-19 12:01:34 +00:00
/ * *
* @ param { string } messageID
* /
function onMessageDelete ( messageID ) {
bumping . delete ( messageID )
}
module . exports . updateCache = updateCache
module . exports . doSpeedbump = doSpeedbump
2024-02-07 01:52:12 +00:00
module . exports . maybeDoSpeedbump = maybeDoSpeedbump
2024-01-19 12:01:34 +00:00
module . exports . onMessageDelete = onMessageDelete