2019-03-06 14:37:36 -05:00
const moment = require ( 'moment' ) ;
2018-03-11 17:17:14 -04:00
const threadUtils = require ( '../threadUtils' ) ;
const threads = require ( "../data/threads" ) ;
2019-03-06 14:37:36 -05:00
const utils = require ( '../utils' ) ;
const config = require ( '../config' ) ;
const { THREAD _STATUS } = require ( '../data/constants' ) ;
2018-03-11 17:17:14 -04:00
module . exports = bot => {
const addInboxServerCommand = ( ... args ) => threadUtils . addInboxServerCommand ( bot , ... args ) ;
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 ( ) ;
2018-03-11 17:17:14 -04:00
addInboxServerCommand ( 'suspend' , async ( msg , args , thread ) => {
if ( ! thread ) return ;
2019-03-06 14:37:36 -05:00
if ( args . length ) {
// Cancel timed suspend
if ( args . includes ( 'cancel' ) || args . includes ( 'c' ) ) {
// Cancel timed suspend
if ( thread . scheduled _suspend _at ) {
await thread . cancelScheduledSuspend ( ) ;
thread . postSystemMessage ( ` Cancelled scheduled suspension ` ) ;
}
return ;
}
// Timed suspend
const delayStringArg = args . find ( arg => utils . delayStringRegex . test ( arg ) ) ;
if ( delayStringArg ) {
const delay = utils . convertDelayStringToMS ( delayStringArg ) ;
if ( delay === 0 || delay === null ) {
thread . postSystemMessage ( ` Invalid delay specified. Format: "1h30m" ` ) ;
return ;
}
const suspendAt = moment . utc ( ) . add ( delay , 'ms' ) ;
await thread . scheduleSuspend ( suspendAt . format ( 'YYYY-MM-DD HH:mm:ss' ) , msg . author ) ;
thread . postSystemMessage ( ` Thread will be suspended in ${ utils . humanizeDelay ( delay ) } . Use \` ${ config . prefix } suspend cancel \` to cancel. ` ) ;
return ;
}
}
2018-03-11 17:17:14 -04:00
await thread . suspend ( ) ;
thread . postSystemMessage ( ` **Thread suspended!** This thread will act as closed until unsuspended with \` !unsuspend \` ` ) ;
} ) ;
addInboxServerCommand ( 'unsuspend' , async msg => {
const thread = await threads . findSuspendedThreadByChannelId ( msg . channel . id ) ;
if ( ! thread ) return ;
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 ( ) ;
thread . postSystemMessage ( ` **Thread unsuspended!** ` ) ;
} ) ;
} ;