fork(1) download
  1. local CATEGORY_NAME = "Utility"
  2.  
  3. CreateConVar("ulx_warnkick_num", 3, FCVAR_ARCHIVE, "Number of warnings a player needs before they are kicked/banned.")
  4. CreateConVar("ulx_warnkick_decayrate", 30, FCVAR_ARCHIVE, "Minutes a player needs to be connected before their warning count is decreased by 1.")
  5. CreateConVar("ulx_warnkick_ban", 0, FCVAR_ARCHIVE, "If this is set to 1, the script will issue a temp ban instead of a kick.")
  6. CreateConVar("ulx_warnkick_bantime", 30, FCVAR_ARCHIVE, "How long in minutes a player is banned if ulx_warkick_ban is set to 1.")
  7.  
  8. if !file.Exists( "ulx", "DATA" ) then
  9. file.CreateDir( "ulx" )
  10. end
  11.  
  12. if !file.Exists( "ulx/Warnings", "DATA" ) then
  13. file.CreateDir( "ulx/Warnings" )
  14. end
  15.  
  16. --[[
  17. ulx.warn( calling_ply, target_ply, reason )
  18. calling_ply : PlayerObject : Player who ran the command.
  19. target_ply : PlayerObject : Player who is being warned.
  20. reason : String : Reason player is being warned.
  21.  
  22. This function is the ULX function that allows for a warning of a player.
  23. ]]
  24. function ulx.warn( calling_ply, target_ply, reason )
  25. if reason and reason ~= "" then
  26. ulx.fancyLogAdmin( calling_ply, "#A warned #T (#s)", target_ply, reason )
  27. else
  28. reason = nil
  29. ulx.fancyLogAdmin( calling_ply, "#A warned #T", target_ply )
  30. end
  31. ulx.AddWarning( target_ply, calling_ply, reason )
  32. end
  33. local warn = ulx.command( CATEGORY_NAME, "ulx warn", ulx.warn, "!warn" )
  34. warn:addParam{ type=ULib.cmds.PlayerArg }
  35. warn:addParam{ type=ULib.cmds.StringArg, hint="reason", ULib.cmds.optional, ULib.cmds.takeRestOfLine }
  36. warn:defaultAccess( ULib.ACCESS_ADMIN )
  37. warn:help( "Warn a player." )
  38.  
  39. --[[
  40. ulx.AddWarning( target_ply, calling_ply, reason )
  41. target_ply : PlayerObject : Player who is being warned.
  42. calling_ply : PlayerObject : Admin or player who did the warning.
  43. reason : String : Reason player is being warned.
  44.  
  45. This helper function is what adds the warning to the player's table and calls the save helper function.
  46.  
  47. ]]
  48. function ulx.AddWarning( target_ply, calling_ply, reason )
  49.  
  50. if target_ply.warntable == nil then
  51. target_ply.warntable = {}
  52. end
  53.  
  54. if target_ply.warntable["wcount"] == nil then
  55. target_ply.warntable["wcount"] = 0
  56. end
  57.  
  58. if target_ply.warntable["warnings"] == nil then
  59. target_ply.warntable["warnings"] = {}
  60. end
  61.  
  62. table.insert(target_ply.warntable["warnings"], {os.date(), calling_ply:Nick(), reason})
  63.  
  64. target_ply.warntable["wcount"] = target_ply.warntable["wcount"] + 1
  65. ULib.tsayColor(target_ply, Color(0,0,0,255), "AWarn: " , Color(255,255,255,255), "You were warned by ", Color(0,0,0,255), "(", Color(0,255,0,255), calling_ply:Nick(), Color(0,0,0,255), ")", Color(255,255,255,255), " for: ", Color(255,0,0,255), reason)
  66.  
  67.  
  68.  
  69. if target_ply.warntable["wcount"] >= GetConVarNumber( "ulx_warnkick_num" ) then
  70. if GetConVarNumber( "ulx_warnkick_ban" ) == 0 then
  71. target_ply.warntable["wcount"] = target_ply.warntable["wcount"] - 1
  72. ulx.WarningSave( target_ply )
  73. ULib.kick( target_ply, "Warning threshold exceeded" )
  74. else
  75. local btime = tostring( GetConVarNumber( "ulx_warnkick_bantime" ) )
  76. target_ply.warntable["wcount"] = target_ply.warntable["wcount"] - 1
  77. ulx.WarningSave( target_ply )
  78. ULib.kickban( target_ply, GetConVarNumber( "ulx_warnkick_bantime" ), "Warning threshold exceededm Banned for (" .. btime .. ") minutes.", calling_ply )
  79. end
  80. else
  81. ulx.WarningSave( target_ply )
  82. end
  83.  
  84.  
  85. end
  86.  
  87. --[[
  88. ulx.DecayWarnings()
  89.  
  90. This function runs on a timer and removes 1 active warning from a players warning count. The player needs to be playing on the server
  91. when this is called to have a warning removed.
  92. ]]
  93.  
  94. function ulx.DecayWarnings()
  95. print("Decay timer running")
  96.  
  97. for _, pl in pairs ( player.GetAll() ) do
  98. if pl.timewarned == 0 then pl.timewarned = 1 end -- prevents the line bellow from being 0 ever
  99. if pl:TimeConnected( ) <= GetConVarNumber( "ulx_warnkick_decayrate" ) * 60 * pl.timewarned then continue end
  100. if pl.warntable == nil then continue end
  101. if pl.warntable["wcount"] == nil then continue end
  102. if pl.warntable["wcount"] <= 0 then continue end
  103.  
  104. pl.warntable["wcount"] = pl.warntable["wcount"] - 1
  105. ulx.WarningSave( pl )
  106. pl.timewarned = pl.timewarned + 1
  107. ULib.tsayColor(pl, Color(0,0,0,255), "AWarn: " , Color(255,255,255,255), "Your total warning count has been reduced by ", Color(255,0,0,255), "1")
  108.  
  109. end
  110. timer.Create( "ULX_DecayTimer", 60, 1, ulx.DecayWarnings )
  111. --timer.Create( "ULX_DecayTimer", GetConVarNumber( "ulx_warnkick_decayrate" ) * 60, 1, ulx.DecayWarnings ) -- keeping as a backup
  112. end
  113. timer.Create( "ULX_DecayTimer", 1, 1, ulx.DecayWarnings )
  114.  
  115.  
  116. --[[
  117. ulx.WarningSave( pl )
  118. pl : PlayerObject : Player whos warnings are being saved
  119.  
  120. This helper function saves the player's warnings to a text file for future use.
  121. ]]
  122. function ulx.WarningSave( pl )
  123.  
  124. local tbl = pl.warntable
  125. local SID = pl:SteamID64()
  126.  
  127. toencode = util.TableToJSON(tbl)
  128.  
  129. file.Write("ulx/Warnings/"..SID..".txt", toencode)
  130.  
  131. end
  132.  
  133. --[[
  134. ulx.WarningsLoad( pl )
  135. pl : PlayerObject : Player whos warnings are being loaded
  136.  
  137. This helper function loads a player's saved warnings from their file to their player object.
  138. ]]
  139. function ulx.WarningsLoad( pl )
  140.  
  141. local SID = pl:SteamID64()
  142. if file.Exists( "ulx/Warnings/" .. SID .. ".txt", "DATA" ) then
  143. local todecode = file.Read( "ulx/Warnings/" .. SID .. ".txt", "DATA" )
  144.  
  145. local tbl = util.JSONToTable( todecode )
  146. pl.warntable = tbl
  147.  
  148. end
  149.  
  150. end
  151. hook.Add( "PlayerAuthed", "WarningsLoad", ulx.WarningsLoad )
  152.  
  153. --[[
  154. ulx.seewarns( calling_ply, target_ply )
  155. calling_ply : PlayerObject : Admin or player who runs the command.
  156. target_ply : PlayerObject : Target player whos warnings are being displayed.
  157.  
  158. This function allows an admin or whoever is granted access to see the history of warnings on a target player.
  159. ]]
  160. function ulx.seewarns( calling_ply, target_ply )
  161.  
  162. if not IsValid(calling_ply) then return end
  163. if not IsValid(target_ply) then return end
  164.  
  165. if target_ply.warntable == nil then
  166. target_ply.warntable = {}
  167. end
  168.  
  169. if target_ply.warntable["warnings"] == nil then
  170. ULib.console( calling_ply, "Showing warning history for player: " .. target_ply:Nick() )
  171. ULib.console( calling_ply, "this player does not currently have any warnings." )
  172. else
  173. ULib.console( calling_ply, "Showing warning history for player: " .. target_ply:Nick() )
  174. ULib.console( calling_ply, "Date Admin Reason" )
  175. ULib.console( calling_ply, "-------------------------------------------------------------------------------------------" )
  176. for k, v in pairs( target_ply.warntable[ "warnings" ] ) do
  177. local date = v[1]
  178. local admin = v[2]
  179. local reason = v[3]
  180. line = date .. string.rep(" ", 25 - date:len()) .. admin .. string.rep(" ", 35 - admin:len()) .. reason
  181. ULib.console( calling_ply, line )
  182. end
  183. end
  184. end
  185. local seewarns = ulx.command( CATEGORY_NAME, "ulx seewarns", ulx.seewarns )
  186. seewarns:addParam{ type=ULib.cmds.PlayerArg }
  187. seewarns:defaultAccess( ULib.ACCESS_ADMIN )
  188. seewarns:help( "Lists all warnings for a player to console." )
  189.  
  190. --[[
  191. ulx.RemoveWarning( calling_ply, target_ply, warning_count )
  192. calling_ply : PlayerObject : Admin or player who runs the command.
  193. target_ply : PlayerObject : Target player whos warnings are being displayed.
  194. warning_count : Integer : Amount of active warnings to remove from the player.
  195.  
  196. This function will allow an admin to remove active warnings from a target player.
  197. ]]
  198. function ulx.RemoveWarning( calling_ply, target_ply, warning_count )
  199.  
  200. if not IsValid(calling_ply) then return end
  201. if not IsValid(target_ply) then return end
  202.  
  203. if target_ply.warntable == nil then
  204. target_ply.warntable = {}
  205. end
  206.  
  207. if target_ply.warntable["wcount"] == nil then
  208. ULib.console( calling_ply, "Player " .. target_ply:Nick() .. " does not currently have any active warnings.")
  209. return
  210. end
  211.  
  212. if target_ply.warntable["wcount"] == 0 then
  213. ULib.console( calling_ply, "Player " .. target_ply:Nick() .. " does not currently have any active warnings.")
  214. return
  215. end
  216.  
  217. local total_warnings = target_ply.warntable["wcount"]
  218. local to_remove = warning_count
  219.  
  220. if to_remove > total_warnings then
  221. to_remove = total_warnings
  222. end
  223.  
  224. target_ply.warntable["wcount"] = total_warnings - to_remove
  225. ulx.fancyLogAdmin( calling_ply, "#A removed active warnings from #T.", target_ply )
  226. ULib.console( calling_ply, "You removed (" .. to_remove .. ") warnings from " .. target_ply:Nick() .. ". Player current has (" .. target_ply.warntable["wcount"] .. ") active warnings remaining.")
  227.  
  228. end
  229. local removewarn = ulx.command( CATEGORY_NAME, "ulx removewarning", ulx.RemoveWarning )
  230. removewarn:addParam{ type=ULib.cmds.PlayerArg }
  231. removewarn:addParam{ type=ULib.cmds.NumArg, hint="active warnings to remove" }
  232. removewarn:defaultAccess( ULib.ACCESS_ADMIN )
  233. removewarn:help( "Removes active warnings from a player." )
  234.  
  235. --[[
  236. ulx.RemoveWarningHistory( calling_ply, target_ply )
  237. calling_ply : PlayerObject : Admin or player who runs the command.
  238. target_ply : PlayerObject : Target player whos warnings are being removed.
  239.  
  240. This function removes all warning history from a player.
  241. ]]
  242. function ulx.RemoveWarningHistory( calling_ply, target_ply )
  243. if not IsValid(calling_ply) then return end
  244. if not IsValid(target_ply) then return end
  245.  
  246. local SID = target_ply:SteamID64()
  247.  
  248. if file.Exists( "ulx/Warnings/"..SID..".txt", "DATA" ) then
  249. file.Delete( "ulx/Warnings/"..SID..".txt" )
  250. target_ply.warntable = nil
  251. ULib.console( calling_ply, "You removed all warning records for player: " .. target_ply:Nick() .. "." )
  252. ulx.fancyLogAdmin( calling_ply, "#A removed all warning records for player #T.", target_ply )
  253. else
  254. ULib.console( calling_ply, "Unable to find any warning records for player: " .. target_ply:Nick() .. "." )
  255. end
  256.  
  257.  
  258. end
  259. local deletewarnings = ulx.command( CATEGORY_NAME, "ulx deletewarnings", ulx.RemoveWarningHistory )
  260. deletewarnings:addParam{ type=ULib.cmds.PlayerArg }
  261. deletewarnings:defaultAccess( ULib.ACCESS_SUPERADMIN )
  262. deletewarnings:help( "Deletes all warning records from a target player." )
  263.  
  264. --[[
  265. ulx.ListAllWarnings( calling_ply )
  266. calling_ply : PlayerObject : Admin or player who runs the command.
  267.  
  268. Shows a list of all players on the server and how many total warnings and active warnings they have.
  269. ]]
  270. function ulx.ListAllWarnings( calling_ply )
  271.  
  272. ULib.console( calling_ply, "Showing total warnings for all players:" )
  273. ULib.console( calling_ply, "Player Name Total Warnings Active Warnings" )
  274. ULib.console( calling_ply, string.rep( "-", 75 ) )
  275.  
  276. for _, pl in pairs( player.GetAll() ) do
  277. if pl.warntable == nil then continue end
  278. if pl.warntable[ "wcount" ] == nil then continue end
  279. local totalwarns = tostring( table.Count( pl.warntable[ "warnings" ] ) )
  280. local activewarns = tostring( pl.warntable[ "wcount" ] )
  281. ULib.console( calling_ply, pl:Nick() .. string.rep(" ", 37 - pl:Nick():len()) .. totalwarns .. string.rep(" ", 23 - totalwarns:len()) .. activewarns )
  282. end
  283.  
  284. end
  285. local listwarnings = ulx.command( CATEGORY_NAME, "ulx listwarnings", ulx.ListAllWarnings )
  286. listwarnings:defaultAccess( ULib.ACCESS_ADMIN )
  287. listwarnings:help( "Returns a list of all connected players and their warning counts." )
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty