From de49addc95514607e4472c1bd57232093c72faea Mon Sep 17 00:00:00 2001 From: SeanWalsh95 Date: Thu, 22 Oct 2020 17:06:28 -0400 Subject: [PATCH] init afk plugin for v2 --- config.json | 6 +++ squad-server/plugins/auto-kick-afk.js | 69 +++++++++++++++++++++++++++ squad-server/plugins/index.js | 2 + 3 files changed, 77 insertions(+) create mode 100644 squad-server/plugins/auto-kick-afk.js diff --git a/config.json b/config.json index 012a4b1..7f22d92 100644 --- a/config.json +++ b/config.json @@ -66,6 +66,12 @@ "enabled": true, "message": "Please apologise for ALL TKs in ALL chat!" }, + { + "plugin": "AutoKickAFK", + "enabled": true, + "warning": "", + "updateInterval": 3 + }, { "plugin": "ChatCommands", "enabled": true, diff --git a/squad-server/plugins/auto-kick-afk.js b/squad-server/plugins/auto-kick-afk.js new file mode 100644 index 0000000..0526520 --- /dev/null +++ b/squad-server/plugins/auto-kick-afk.js @@ -0,0 +1,69 @@ +import BasePlugin from './base-plugin.js'; + +export default class AutoKickAFK extends BasePlugin { + static get description() { + return 'The AutoKickAFK 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: + 'If enabled SquadJS will warn a player once before kicking them. To disable remove the message `""`', + default: 'Players not in a squad are unassigned and will be kicked in 3 minutes' + }, + updateInterval: { + required: true, + description: 'How frequently to check if players are AFK in minutes.', + default: 3 + } + }; + } + + constructor(server, options) { + super(); + + this.playerDict = {}; + + const intervalMS = options.updateInterval * 60 * 1000; + + setInterval(async () => { + console.log(server.players); + const lookup = {}; + for (const player of server.players) { + lookup[player.steamID] = player; + + // marks player if not in a Squad + if (player.squadID === null) { + if (player.steamID in this.playerDict) { + this.playerDict[player.steamID] += 1; + } else { + this.playerDict[player.steamID] = 0; + } + } else if (player.steamID in this.playerDict) { + // remove player from list if they joined a squad + delete this.playerDict[player.steamID]; + } + } + + const copy = Object.assign({}, this.playerDict); + for (const [steamID, count] of Object.entries(copy)) { + if (count >= 1) { + await server.rcon.kick(steamID); + delete this.playerDict[steamID]; + } + if (count === 0 && options.warning !== '') { + await server.rcon.warn(steamID, options.warning); + } else { + await server.rcon.kick(steamID); + delete this.playerDict[steamID]; + } + } + }, intervalMS); + } +} diff --git a/squad-server/plugins/index.js b/squad-server/plugins/index.js index 5ddec05..4e97d7a 100644 --- a/squad-server/plugins/index.js +++ b/squad-server/plugins/index.js @@ -1,4 +1,5 @@ import AutoTKWarn from './auto-tk-warn.js'; +import AutoKickAFK from './auto-kick-afk.js'; import ChatCommands from './chat-commands.js'; import DiscordAdminBroadcast from './discord-admin-broadcast.js'; import DiscordAdminCamLogs from './discord-admin-cam-logs.js'; @@ -14,6 +15,7 @@ import SeedingMode from './seeding-mode.js'; const plugins = [ AutoTKWarn, + AutoKickAFK, ChatCommands, DiscordAdminBroadcast, DiscordAdminCamLogs,