1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2026-04-28 21:04:39 +02:00
Files
ComfyFactorio/utils/admin_handler.lua
T

163 lines
4.2 KiB
Lua
Raw Normal View History

2021-12-05 22:15:49 +01:00
local Event = require 'utils.event'
local Server = require 'utils.server'
2024-02-25 13:58:12 +01:00
local Discord = require 'utils.discord_handler'
2025-10-22 07:41:10 +02:00
local commands =
{
2025-09-08 23:39:31 +02:00
['editor'] = true,
['open'] = true,
['cheat'] = true,
['permissions'] = true,
['banlist'] = true,
['config'] = true,
['command'] = true,
['silent-command'] = true,
['sc'] = true,
['debug'] = true
}
2025-10-22 07:41:10 +02:00
local title_to_command =
{
2025-10-19 21:20:03 +02:00
['editor'] = 'Editor',
['open'] = 'Open',
['cheat'] = 'Cheat',
['permissions'] = 'Permissions',
['banlist'] = 'Banlist',
['config'] = 'Config',
['command'] = 'Command',
['silent-command'] = 'Silent Command',
['sc'] = 'Silent Command',
['debug'] = 'Debug'
2025-09-08 23:39:31 +02:00
}
2021-12-05 22:15:49 +01:00
local function on_console_command(event)
local cmd = event.command
2024-02-25 13:58:12 +01:00
if not commands[cmd] then
2021-12-05 22:15:49 +01:00
return
end
2025-09-08 23:39:31 +02:00
-- Handle player vs server executor
local player, executor
2024-02-25 13:58:12 +01:00
if event.player_index then
2025-09-08 23:39:31 +02:00
player = game.get_player(event.player_index)
if not (player and player.admin) then
2024-02-25 13:58:12 +01:00
return
end
2025-09-08 23:39:31 +02:00
executor = player.name
2021-12-05 22:15:49 +01:00
else
2025-10-19 21:20:03 +02:00
executor = 'Server'
2021-12-05 22:15:49 +01:00
end
2025-09-08 23:39:31 +02:00
2025-10-19 21:20:03 +02:00
local param = (event.parameters and event.parameters ~= '' and event.parameters) or 'No parameters'
local server_name = Server.get_server_name() or 'CommandHandler'
2025-09-08 23:39:31 +02:00
Discord.send_notification(
{
title = title_to_command[cmd],
2025-10-19 21:20:03 +02:00
description = '/' .. cmd .. ' was used',
color = 'warning',
2025-10-22 07:41:10 +02:00
fields =
{
2025-09-08 23:39:31 +02:00
{
2025-10-19 21:20:03 +02:00
title = 'Server',
2025-09-08 23:39:31 +02:00
description = server_name,
2025-10-19 21:20:03 +02:00
inline = 'false'
2025-09-08 23:39:31 +02:00
},
{
2025-10-19 21:20:03 +02:00
title = 'By',
2025-09-08 23:39:31 +02:00
description = executor,
2025-10-19 21:20:03 +02:00
inline = 'true'
2025-09-08 23:39:31 +02:00
},
{
2025-10-19 21:20:03 +02:00
title = 'Details',
2025-09-08 23:39:31 +02:00
description = param,
2025-10-19 21:20:03 +02:00
inline = 'true'
2025-09-08 23:39:31 +02:00
}
}
2025-10-19 21:20:03 +02:00
}
)
2021-12-05 22:15:49 +01:00
end
Event.add(defines.events.on_console_command, on_console_command)
2024-02-25 13:58:12 +01:00
Event.add(
defines.events.on_player_promoted,
2025-10-22 07:41:10 +02:00
function (event)
2024-10-25 21:56:34 +02:00
local admins = Server.get_admins_data()
2024-02-25 13:58:12 +01:00
local player = game.get_player(event.player_index)
local server_name = Server.get_server_name() or 'CommandHandler'
2025-09-08 23:39:31 +02:00
Discord.send_notification(
{
2025-10-19 21:20:03 +02:00
title = 'Admin promotion',
description = player.name .. ' was promoted.',
color = 'success',
2025-10-22 07:41:10 +02:00
fields =
{
2025-09-08 23:39:31 +02:00
{
2025-10-19 21:20:03 +02:00
title = 'Server',
2025-09-08 23:39:31 +02:00
description = server_name,
2025-10-19 21:20:03 +02:00
inline = 'false'
2025-09-08 23:39:31 +02:00
}
}
2025-10-19 21:20:03 +02:00
}
)
2025-09-08 23:39:31 +02:00
2024-10-25 21:56:34 +02:00
if not game.is_multiplayer() then
return
end
if not admins[player.name] then
player.admin = false
return
end
2024-02-25 13:58:12 +01:00
end
)
Event.add(
defines.events.on_player_demoted,
2025-10-22 07:41:10 +02:00
function (event)
2024-02-25 13:58:12 +01:00
local player = game.get_player(event.player_index)
local server_name = Server.get_server_name() or 'CommandHandler'
2025-09-08 23:39:31 +02:00
Discord.send_notification(
{
2025-10-19 21:20:03 +02:00
title = 'Admin demotion',
description = player.name .. ' was demoted.',
color = 'warning',
2025-10-22 07:41:10 +02:00
fields =
{
2025-09-08 23:39:31 +02:00
{
2025-10-19 21:20:03 +02:00
title = 'Server',
2025-09-08 23:39:31 +02:00
description = server_name,
2025-10-19 21:20:03 +02:00
inline = 'false'
2025-09-08 23:39:31 +02:00
}
}
2025-10-19 21:20:03 +02:00
}
)
2024-02-25 13:58:12 +01:00
end
)
Event.add(
defines.events.on_player_kicked,
2025-10-22 07:41:10 +02:00
function (event)
2024-02-25 13:58:12 +01:00
local player = game.get_player(event.player_index)
local server_name = Server.get_server_name() or 'CommandHandler'
2025-09-08 23:39:31 +02:00
Discord.send_notification(
{
2025-10-19 21:20:03 +02:00
title = 'Player kicked',
description = player.name .. ' was kicked.',
color = 'danger',
2025-10-22 07:41:10 +02:00
fields =
{
2025-09-08 23:39:31 +02:00
{
2025-10-19 21:20:03 +02:00
title = 'Server',
2025-09-08 23:39:31 +02:00
description = server_name,
2025-10-19 21:20:03 +02:00
inline = 'false'
2025-09-08 23:39:31 +02:00
}
}
2025-10-19 21:20:03 +02:00
}
)
2024-02-25 13:58:12 +01:00
end
)