2020-08-12 17:08:37 -04:00
const moment = require ( "moment" ) ;
2018-03-11 17:17:14 -04:00
const threads = require ( "../data/threads" ) ;
2020-08-12 17:08:37 -04:00
const utils = require ( "../utils" ) ;
const config = require ( "../cfg" ) ;
2019-03-06 14:37:36 -05:00
2020-08-12 17:08:37 -04:00
const { THREAD _STATUS } = require ( "../data/constants" ) ;
2018-03-11 17:17:14 -04:00
2019-08-13 13:34:46 -04:00
module . exports = ( { bot , knex , config , commands } ) => {
2019-03-06 14:37:36 -05:00
// Check for threads that are scheduled to be suspended and suspend them
async function applyScheduledSuspensions ( ) {
const threadsToBeSuspended = await threads . getThreadsThatShouldBeSuspended ( ) ;
for ( const thread of threadsToBeSuspended ) {
if ( thread . status === THREAD _STATUS . OPEN ) {
await thread . suspend ( ) ;
await thread . postSystemMessage ( ` **Thread suspended** as scheduled by ${ thread . scheduled _suspend _name } . This thread will act as closed until unsuspended with \` !unsuspend \` ` ) ;
}
}
}
async function scheduledSuspendLoop ( ) {
try {
await applyScheduledSuspensions ( ) ;
} catch ( e ) {
console . error ( e ) ;
}
setTimeout ( scheduledSuspendLoop , 2000 ) ;
}
scheduledSuspendLoop ( ) ;
2020-08-12 17:08:37 -04:00
commands . addInboxThreadCommand ( "suspend cancel" , [ ] , async ( msg , args , thread ) => {
2019-06-16 15:27:30 -04:00
// Cancel timed suspend
if ( thread . scheduled _suspend _at ) {
await thread . cancelScheduledSuspend ( ) ;
2020-08-12 17:08:37 -04:00
thread . postSystemMessage ( "Cancelled scheduled suspension" ) ;
2019-06-16 15:27:30 -04:00
} else {
2020-08-12 17:08:37 -04:00
thread . postSystemMessage ( "Thread is not scheduled to be suspended" ) ;
2019-06-16 15:27:30 -04:00
}
} ) ;
2019-03-06 14:37:36 -05:00
2020-08-12 17:08:37 -04:00
commands . addInboxThreadCommand ( "suspend" , "[delay:delay]" , async ( msg , args , thread ) => {
2019-06-16 15:27:30 -04:00
if ( args . delay ) {
2020-08-12 17:08:37 -04:00
const suspendAt = moment . utc ( ) . add ( args . delay , "ms" ) ;
await thread . scheduleSuspend ( suspendAt . format ( "YYYY-MM-DD HH:mm:ss" ) , msg . author ) ;
2019-03-06 14:37:36 -05:00
2019-06-16 15:27:30 -04:00
thread . postSystemMessage ( ` Thread will be suspended in ${ utils . humanizeDelay ( args . delay ) } . Use \` ${ config . prefix } suspend cancel \` to cancel. ` ) ;
2019-03-06 14:37:36 -05:00
2019-06-16 15:27:30 -04:00
return ;
2019-03-06 14:37:36 -05:00
}
2018-03-11 17:17:14 -04:00
await thread . suspend ( ) ;
2020-08-12 17:08:37 -04:00
thread . postSystemMessage ( "**Thread suspended!** This thread will act as closed until unsuspended with `!unsuspend`" ) ;
2018-03-11 17:17:14 -04:00
} ) ;
2020-08-12 17:08:37 -04:00
commands . addInboxServerCommand ( "unsuspend" , [ ] , async ( msg , args , thread ) => {
2019-06-16 15:27:30 -04:00
if ( thread ) {
2020-08-12 17:08:37 -04:00
thread . postSystemMessage ( "Thread is not suspended" ) ;
2019-06-16 15:27:30 -04:00
return ;
}
thread = await threads . findSuspendedThreadByChannelId ( msg . channel . id ) ;
if ( ! thread ) {
2020-08-12 17:08:37 -04:00
msg . channel . createMessage ( "Not in a thread" ) ;
2019-06-16 15:27:30 -04:00
return ;
}
2018-03-11 17:17:14 -04:00
const otherOpenThread = await threads . findOpenThreadByUserId ( thread . user _id ) ;
if ( otherOpenThread ) {
thread . postSystemMessage ( ` Cannot unsuspend; there is another open thread with this user: <# ${ otherOpenThread . channel _id } > ` ) ;
return ;
}
await thread . unsuspend ( ) ;
2020-08-12 17:08:37 -04:00
thread . postSystemMessage ( "**Thread unsuspended!**" ) ;
2018-03-11 17:17:14 -04:00
} ) ;
} ;