2020-10-22 16:06:28 -05:00
import BasePlugin from './base-plugin.js' ;
export default class AutoKickAFK extends BasePlugin {
static get description ( ) {
return 'The <code>AutoKickAFK</code> plugin will automatically kick players that are not in a squad after a specified ammount of time.' ;
}
static get defaultEnabled ( ) {
return true ;
}
static get optionsSpecification ( ) {
return {
warning : {
required : false ,
description :
2020-10-22 19:22:34 -05:00
'If enabled SquadJS will warn a player once before kicking them. To disable remove the message (`""`)' ,
2020-10-22 16:06:28 -05:00
default : 'Players not in a squad are unassigned and will be kicked in 3 minutes'
} ,
updateInterval : {
2020-10-22 19:22:34 -05:00
required : false ,
description : 'How frequently to check if players are AFK in minutes. If the warning is enabled a player will be kicked after 2x this value otherwise they will be kicked immediately.' ,
2020-10-22 16:06:28 -05:00
default : 3
2020-10-22 19:22:34 -05:00
} ,
playerThreshold : {
required : false ,
description : 'Player count required for Auto Kick to start kicking players to disable set to above max player count' ,
default : 93
} ,
queueThreshold : {
required : false ,
description : 'The number of players in the queue before Auto Kick starts kicking players set to -1 to disable' ,
default : - 1
} / * ,
ignoreAdmins : {
required : false ,
description : 'Whether or not admins will be auto kicked for being unassigned' ,
default : false
} * /
2020-10-22 16:06:28 -05:00
} ;
}
constructor ( server , options ) {
super ( ) ;
this . playerDict = { } ;
2020-10-22 19:22:34 -05:00
//for initial testing
this . auditMode = true ;
2020-10-22 16:06:28 -05:00
const intervalMS = options . updateInterval * 60 * 1000 ;
2020-10-22 19:22:34 -05:00
setInterval ( async ( ) => {
if ( server . players . count <= options . playerCountThreshold || ( server . publicQueue > options . queueThreshold > 0 ) ) {
// clear tracking vlaues so if the player count indreases/decreases past any threshold stale players arent counted again if they happen to be unassigned
this . playerDict = { } ;
return ;
}
2020-10-22 16:06:28 -05:00
2020-10-22 19:22:34 -05:00
for ( const player of server . players ) {
2020-10-22 16:06:28 -05:00
// marks player if not in a Squad
2020-10-22 19:22:34 -05:00
if ( player . squadID === null ) {
if ( player . steamID in this . playerDict ) {
// player in dict was already warned mark for kick
2020-10-22 16:06:28 -05:00
this . playerDict [ player . steamID ] += 1 ;
2020-10-22 19:22:34 -05:00
} else {
// player not in dict is added for warning
2020-10-22 16:06:28 -05:00
this . playerDict [ player . steamID ] = 0 ;
}
2020-10-22 19:22:34 -05:00
} else if ( player . steamID in this . playerDict ) {
2020-10-22 16:06:28 -05:00
// remove player from list if they joined a squad
delete this . playerDict [ player . steamID ] ;
}
}
2020-10-22 19:22:34 -05:00
console . log ( this . playerDict ) ;
2020-10-22 16:06:28 -05:00
const copy = Object . assign ( { } , this . playerDict ) ;
2020-10-22 19:22:34 -05:00
for ( const [ steamID , warnings ] of Object . entries ( copy ) ) {
if ( warnings >= 1 || options . warning === '' ) {
if ( this . auditMode ) {
console . log ( ` [AUTO AFK] kick ${ steamID } for AFK ` )
} else {
// kick player that has been warned
await server . rcon . kick ( steamID , 'UNASSIGNED - automatically removed' ) ;
delete this . playerDict [ steamID ] ;
}
} else {
if ( this . auditMode ) {
console . log ( ` [AUTO AFK] warn player ${ steamID } for AFK ` ) ;
} else {
server . rcon . warn ( steamID , options . warning ) ;
}
2020-10-22 16:06:28 -05:00
}
}
2020-10-22 19:22:34 -05:00
} , intervalMS ) ;
//clean up every 20 minutes, removes players no longer on the server that may be stuck in the tracking dict
const cleanupMS = 20 * 60 * 1000 ;
setInterval ( ( ) => {
for ( steamID of Object . keys ( this . playerDict ) )
if ( ! steamID in server . players . map ( p => p . steamID ) )
delete this . playerDict [ steamID ] ;
} , cleanupMS ) ;
2020-10-22 16:06:28 -05:00
}
}