diff --git a/utils/datastore/banhandler.lua b/utils/datastore/banhandler.lua index b4e4d7e4..624d16fc 100644 --- a/utils/datastore/banhandler.lua +++ b/utils/datastore/banhandler.lua @@ -2,16 +2,12 @@ local Event = require 'utils.event' local Server = require 'utils.server' local Token = require 'utils.token' -local len = string.len -local gmatch = string.gmatch -local insert = table.insert +local Public = {} + local ban_by_join_enabled = false local try_get_ban = Server.try_get_ban ---- Jail dataset. -local jailed_data_set = 'jailed' - local try_get_is_banned_token = Token.register( function(data) @@ -56,91 +52,8 @@ Event.add( Event.add( defines.events.on_console_command, function(event) - local cmd = event.command - - local user = event.parameters - if not user then - return - end - - if len(user) <= 2 then - return - end - - local player_index - local reason - local str = '' - - local t = {} - for i in gmatch(user, '%S+') do - insert(t, i) - end - - player_index = t[1] - - for i = 2, #t do - str = str .. t[i] .. ' ' - reason = str - end - - if not player_index then - return print('[on_console_command] - player_index was undefined.') - end - - local target - if game.get_player(player_index) then - target = game.get_player(player_index) - else - return - end - - if event.player_index then - local player = game.get_player(event.player_index) - if player and player.valid and player.admin then - -- if target.index == player.index then - -- return - -- end - - local data = Server.build_embed_data() - data.username = target.name - data.admin = player.name - - if cmd == 'ban' then - Server.set_data(jailed_data_set, target.name, nil) -- this is added here since we don't want to clutter the jail dataset. - if not reason then - data.reason = 'Not specified.' - Server.to_banned_embed(data) - return - else - data.reason = reason - Server.to_banned_embed(data) - return - end - elseif cmd == 'unban' then - Server.to_unbanned_embed(data) - return - end - end - else - local data = Server.build_embed_data() - data.username = target.name - data.admin = '' - - if cmd == 'ban' then - Server.set_data(jailed_data_set, target.name, nil) -- this is added here since we don't want to clutter the jail dataset. - if not reason then - data.reason = 'Not specified.' - Server.to_banned_embed(data) - return - else - data.reason = reason - Server.to_banned_embed(data) - return - end - elseif cmd == 'unban' then - Server.to_unbanned_embed(data) - return - end - end + Server.ban_handler(event) end ) + +return Public diff --git a/utils/server.lua b/utils/server.lua index f090f048..0f6306b0 100644 --- a/utils/server.lua +++ b/utils/server.lua @@ -12,6 +12,8 @@ local concat = table.concat local serialize = serpent.serialize local remove = table.remove local tostring = tostring +local len = string.len +local gmatch = string.gmatch local raw_print = Print.raw_print local minutes_to_ticks = 60 * 60 @@ -92,6 +94,9 @@ local player_leave_tag = '[PLAYER-LEAVE]' Public.raw_print = raw_print +--- Jail dataset. +local jailed_data_set = 'jailed' + local data_set_handlers = {} local function assert_non_empty_string_and_no_spaces(str, argument_name) @@ -1317,6 +1322,98 @@ function Public.query_online_players() raw_print(message) end +function Public.ban_handler(event) + local cmd = event.command + + local user = event.parameters + if not user then + return + end + + if len(user) <= 2 then + return + end + + local player_index + local reason + local str = '' + + local t = {} + for i in gmatch(user, '%S+') do + insert(t, i) + end + + player_index = t[1] + + for i = 2, #t do + str = str .. t[i] .. ' ' + reason = str + end + + if not player_index then + return print('[on_console_command] - player_index was undefined.') + end + + local target + if game.get_player(player_index) then + target = game.get_player(player_index) + if not target or not target.valid then + return + end + else + return + end + + if event.player_index then + local player = game.get_player(event.player_index) + if player and player.valid and player.admin then + local data = Public.build_embed_data() + data.username = target.name + data.admin = player.name + + if cmd == 'ban' then + Public.set_data(jailed_data_set, target.name, nil) -- this is added here since we don't want to clutter the jail dataset. + if not reason then + data.reason = 'Not specified.' + Public.to_banned_embed(data) + return + else + data.reason = reason + Public.to_banned_embed(data) + return + end + elseif cmd == 'unban' then + Public.to_unbanned_embed(data) + return + end + end + else + local data = Public.build_embed_data() + data.username = target.name + data.admin = '' + + if event.user_override then + data.admin = event.user_override + end + + if cmd == 'ban' then + Public.set_data(jailed_data_set, target.name, nil) -- this is added here since we don't want to clutter the jail dataset. + if not reason then + data.reason = 'Not specified.' + Public.to_banned_embed(data) + return + else + data.reason = reason + Public.to_banned_embed(data) + return + end + elseif cmd == 'unban' then + Public.to_unbanned_embed(data) + return + end + end +end + local function command_handler(callback, ...) if type(callback) == 'function' then local success, err = pcall(callback, ...) diff --git a/utils/server_commands.lua b/utils/server_commands.lua index 1df6291e..11cdf10f 100644 --- a/utils/server_commands.lua +++ b/utils/server_commands.lua @@ -37,6 +37,7 @@ ServerCommands.export_stats = Server.export_stats ServerCommands.set_start_data = Server.set_start_data ServerCommands.set_instances = Server.set_instances ServerCommands.query_online_players = Server.query_online_players +ServerCommands.ban_handler = Server.ban_handler local SC_Interface = { get_ups = function()