From bfdda46882aac08a2a0324826e491d5592ac5aca Mon Sep 17 00:00:00 2001
From: Linventif <linventif@gmail.com>
Date: Thu, 28 Dec 2023 02:57:30 +0100
Subject: [PATCH] add log bot and add give

---
 lua/gmod_integration/client/cl_gui.lua | 12 ++++++++
 lua/gmod_integration/server/sv_log.lua | 38 +++++++++++++++++++++-----
 lua/gmod_integration/sv_config.lua     |  1 +
 3 files changed, 44 insertions(+), 7 deletions(-)

diff --git a/lua/gmod_integration/client/cl_gui.lua b/lua/gmod_integration/client/cl_gui.lua
index 63b3693..6c128ec 100644
--- a/lua/gmod_integration/client/cl_gui.lua
+++ b/lua/gmod_integration/client/cl_gui.lua
@@ -51,6 +51,18 @@ local possibleConfig = {
         end,
         ["category"] = "Main"
     },
+    ["logBotActions"] = {
+        ["label"] = "Log Bot Actions",
+        ["description"] = "Activate or deactivate logs for bot actions.",
+        ["type"] = "checkbox",
+        ["value"] = function(setting, value)
+            return value
+        end,
+        ["onEdit"] = function(setting, value)
+            saveConfig(setting, value == "Enabled" && true || false)
+        end,
+        ["category"] = "Main"
+    },
     ["filterOnBan"] = {
         ["label"] = "Block Discord Ban Player",
         ["description"] = "Block players banned on the discord server.",
diff --git a/lua/gmod_integration/server/sv_log.lua b/lua/gmod_integration/server/sv_log.lua
index b2c73bf..bf4e91f 100644
--- a/lua/gmod_integration/server/sv_log.lua
+++ b/lua/gmod_integration/server/sv_log.lua
@@ -55,12 +55,21 @@ local function logDisable()
     return !gmInte.config.sendLog
 end
 
+local function validLogAndPlayers(players)
+    if (logDisable()) then return false end
+    for _, ply in pairs(players) do
+        if (!IsValid(ply)) then return false end
+        if (!ply:IsBot() && !gmInte.config.logBotActions) then return false end
+    end
+    return true
+end
+
 //
 // Posts
 //
 
 function gmInte.postLogPlayerSay(ply, text, teamChat)
-    if (logDisable() || ply:IsBot()) then return end
+    if (!validLogAndPlayers({ply})) then return end
 
     gmInte.post("/server/log/playerSay",
         {
@@ -72,7 +81,7 @@ function gmInte.postLogPlayerSay(ply, text, teamChat)
 end
 
 function gmInte.postLogPlayerDeath(ply, inflictor, attacker)
-    if (logDisable() || ply:IsBot() || attacker:IsBot()) then return end
+    if (!validLogAndPlayers({ply, attacker})) then return end
 
     gmInte.post("/server/log/playerDeath",
         {
@@ -84,7 +93,7 @@ function gmInte.postLogPlayerDeath(ply, inflictor, attacker)
 end
 
 function gmInte.postLogPlayerInitialSpawn(ply)
-    if (logDisable() || ply:IsBot()) then return end
+    if (!validLogAndPlayers({ply})) then return end
 
     gmInte.post("/server/log/playerInitialSpawn",
         {
@@ -94,7 +103,7 @@ function gmInte.postLogPlayerInitialSpawn(ply)
 end
 
 function gmInte.postLogPlayerHurt(ply, attacker, healthRemaining, damageTaken)
-    if (logDisable() || ply:IsBot() || attacker:IsBot()) then return end
+    if (!validLogAndPlayers({ply, attacker})) then return end
 
     // Wait a second to see if the player is going to be hurt again
     ply.gmodInteLastHurt = ply.gmodInteLastHurt || {}
@@ -120,7 +129,7 @@ function gmInte.postLogPlayerHurt(ply, attacker, healthRemaining, damageTaken)
 end
 
 function gmInte.postLogPlayerSpawnedSomething(object, ply, ent, model)
-    if (logDisable() || ply:IsBot()) then return end
+    if (!validLogAndPlayers({ply})) then return end
 
     gmInte.post("/server/log/playerSpawnedSomething",
         {
@@ -133,7 +142,7 @@ function gmInte.postLogPlayerSpawnedSomething(object, ply, ent, model)
 end
 
 function gmInte.postLogPlayerSpawn(ply)
-    if (logDisable() || ply:IsBot()) then return end
+    if (!validLogAndPlayers({ply})) then return end
 
     gmInte.post("/server/log/playerSpawn",
         {
@@ -143,7 +152,7 @@ function gmInte.postLogPlayerSpawn(ply)
 end
 
 function gmInte.postLogPlayerDisconnect(ply)
-    if (logDisable() || ply:IsBot()) then return end
+    if (!validLogAndPlayers({ply})) then return end
 
     gmInte.post("/server/log/playerDisconnect",
         {
@@ -165,6 +174,18 @@ function gmInte.postLogPlayerConnect(data)
     )
 end
 
+function gmInte.postLogPlayerGivet(ply, class, swep)
+    if (!validLogAndPlayers({ply})) then return end
+
+    gmInte.post("/server/log/playerGive",
+        {
+            ["ply"] = logFormatPlayer(ply),
+            ["class"] = class,
+            ["swep"] = swep
+        }
+    )
+end
+
 //
 // Hooks
 //
@@ -184,6 +205,9 @@ end)
 hook.Add("PlayerDisconnected", "gmInte:Log:PlayerDisconnected", function(ply)
     gmInte.postLogPlayerDisconnect(ply)
 end)
+hook.Add("PlayerGiveSWEP", "gmInte:Log:PlayerSWEPs", function( ply, class, swep )
+    gmInte.postLogPlayerGivet(ply, class, swep)
+end)
 
 // Sandbox - Server Events
 hook.Add("player_connect", "gmInte:Log:PlayerConnect", function(data)
diff --git a/lua/gmod_integration/sv_config.lua b/lua/gmod_integration/sv_config.lua
index 0f35145..3bb98ed 100644
--- a/lua/gmod_integration/sv_config.lua
+++ b/lua/gmod_integration/sv_config.lua
@@ -65,6 +65,7 @@ gmInte.config.chatTrigger = {
 gmInte.config.forcePlayerLink = false // If true, the addon will force the players to link their discord account to their steam account before playing
 gmInte.config.supportLink = "" // The link of your support (shown when a player do not have the requiments to join the server)
 gmInte.config.debug = false // If true, the addon will show debug informations in the console
+gmInte.config.logBotActions = false // If true, the addon will log the messages of the bot in the console
 
 //
 // Syncronization