• Print

Author Topic: command time restrictions  (Read 9733 times)

0 Members and 1 Guest are viewing this topic.

Offline pants

  • Newbie
  • *
  • Posts: 33
  • Karma: 4
command time restrictions
« on: February 22, 2014, 11:56:54 am »
I'd really like to see an addon where I could set it by rank to where say
moderators - must wait 10 seconds between using commands
admins - must wait 5 seconds between using commands

etc. to help with my command spam problems.

those times aren't my final choice just an example

Offline pants

  • Newbie
  • *
  • Posts: 33
  • Karma: 4
Re: command time restrictions
« Reply #1 on: February 23, 2014, 01:21:16 am »
And cobalt delivers! http://pastebin.com/9q9ev2rW


Code: [Select]
-- add all usergroups here with cooldown times
local groups = {}
groups.admin = 10
groups.superadmin = 5
groups.owner = 3

-- groups who shouldnt be affected by a cooldown
local blacklist = { "owner", "superadmin" }

timer.Simple( 10, function()
hook.Add( "ULibCommandCalled", "CheckCommands", function( ply, cmd, args )
if table.HasValue( blacklist, ply:GetUserGroup() ) then
return
end
if not ply.cooldown or ply.cooldown <= 0 then
ply.cooldown = groups[ ply:GetUserGroup() ]
else
ply:ChatPrint( "Please wait " .. tostring( ply.cooldown ) .. " more seconds before you use a command again." )
return false
end
end )
end )

timer.Create( "Cooldown", 1, 0, function()
for k, v in next, player.GetAll() do
if v.cooldown then
if v.cooldown > 0 then
v.cooldown = v.cooldown - 1
if v.cooldown == 0 then
v.cooldown = nil
end
elseif v.cooldown <= 0 then
v.cooldown = nil
end
end
end
end )

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: command time restrictions
« Reply #2 on: February 23, 2014, 05:10:17 pm »
Excellent use of ULibCommandCalled. :D
Experiencing God's grace one day at a time.

Offline pants

  • Newbie
  • *
  • Posts: 33
  • Karma: 4
Re: command time restrictions
« Reply #3 on: February 24, 2014, 12:47:40 am »
I actually wasn't able to get it working, but I have really weird group names like "adept3+"
but cobalt said it worked for him, so it probably works.

Offline lynx

  • Jr. Member
  • **
  • Posts: 59
  • Karma: 15
Re: command time restrictions
« Reply #4 on: March 03, 2014, 11:12:27 am »
Here's an updated version that should work for you pants:
http://pastebin.com/atz2xHMV

This updated version updates how groups are defined (allowing characters such as + to be used), has a default if the group is not listed, and allows for both group and steam whitelisting. It also removes the timer that removed cooldown and instead uses CurTime so it will just see if CurTime is in the future or not. If you have a large server, the timer could possibly have affected the performance while it iterates through 50+ players.

Raw Code:
Code: Lua
  1. --[[----------------------------------
  2. ULX Command Cooldown
  3. Originally coded by: Cobalt
  4. Updated by: lynx
  5. Updated: March 3, 2014
  6. ----------------------------------]]--
  7. ulx.groupcooldown = {}
  8.  
  9. --[[ Begin Configuration Section ]]--
  10.  
  11. -- ulx.groupcooldown.default
  12. -- This sets the default cooldown for groups not listed in the grouplist.
  13. ulx.groupcooldown.default = 15
  14.  
  15. -- ulx.groupcooldown.grouplist
  16. -- This list defines the cooldown times for the groups. If a group is unlisted and not on the whitelist, it will default to default above.
  17. -- This is a table. Please follow the example when adding groups.
  18. ulx.groupcooldown.grouplist = {
  19.         -- All groups must be formatted as followed:
  20.         -- "superadmin" = 5,
  21.         "superadmin" = 3,
  22.         "admin" = 5,
  23.         "operator" = 10,
  24. }
  25.  
  26. -- ulx.groupcooldown.whitelist
  27. -- Groups and Steam ID's defined in this list are exempt from the command cooldown regardless if their group is listed above.
  28. ulx.groupcooldown.whitelist = { "owner", "developer", "superadmin", "STEAM_0:0:0", "STEAM_0:0:29798183" }
  29.  
  30. -- ulx.groupcooldown.message
  31. -- This is the message dispalyed to the user. A second message will inform them of the time they have remaining to wait.
  32. ulx.groupcooldown.message = "You are not allowed to do that right now."
  33.  
  34. --[[ End Configuration. Please do not modify anything below this line otherwise stuff will break. ]]--
  35.  
  36. function ulx.CmdCooldown(ply, cmd, args)
  37.         if table.HasValue( ulx.groupcooldown.whitelist, ply:SteamID() ) or table.HasValue( ulx.groupcooldown.whitelist, ply:GetUserGroup() ) then
  38.                 return
  39.         end
  40.        
  41.         if not ply.NextCommand or ply.NextCommand <= CurTime() then
  42.                 ply.NextCommand = CurTime() + ( ulx.groupcooldown.grouplist[ply:GetUserGroup()] or ulx.groupcooldown.default )
  43.         end
  44.        
  45.         if ply.NextCommand > CurTime() then
  46.                 local nextCmd = ply.NextCommand - CurTime()
  47.                 ULib.tsay( ply, ulx.groupcooldown.message )
  48.                 ULib.tsay( ply, string.format( "You have %s seconds to wait before you can use a command again.", math.floor(nextCmd) ) )
  49.                 return false
  50.         end
  51. end
  52.  
  53. timer.Simple( 3, function()
  54.         hook.Add( "ULibCommandCalled", "CheckCommands", ulx.CmdCooldown )
  55. end )

Offline pants

  • Newbie
  • *
  • Posts: 33
  • Karma: 4
Re: command time restrictions
« Reply #5 on: March 15, 2014, 05:30:10 pm »
Thanks sorry this is so late I missed the post!
ulx.groupcooldown.grouplist = {
   -- All groups must be formatted as followed:
   -- "superadmin" = 5,
   "superadmin" = 0,
   "admin" = 0,
   "padawan2s" = 5,
   "padawan+" = 3,
   "adept3s" = 4,
   "adept+" = 2,
   "jedimaster4s" = 2,
   "jedimaster+" = 2
   
}
 yeilds [ERROR] lua/autorun/server/timerestrictions2.lua:21: '}' expected (to close '{' at line 18) near '='
  1. unknown - lua/autorun/server/timerestrictions2.lua:0

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: command time restrictions
« Reply #6 on: March 15, 2014, 08:34:43 pm »
I'm reasonably sure you can't put lua comments in a table, at least, I used to have tons of trouble doing it years ago.
Try placing the two -- lines outside of the table.
And for good measure, place a comma at the end of the last table entry ("jedimaster+" = 2,
« Last Edit: March 15, 2014, 08:37:55 pm by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline ActeonAk

  • Newbie
  • *
  • Posts: 4
  • Karma: 0
Re: command time restrictions
« Reply #7 on: April 01, 2014, 01:10:40 pm »
I tried the first script by Cobalt, it works for me except that whenever I try to do ulx commands through the console, it doesn't work and I get this error:

Quote
[ERROR] lua/autorun/cooldown.lua:12: attempt to call method 'GetUserGroup' (a nil value)
  1. fn - lua/autorun/cooldown.lua:12
   2. Call - addons/ulib/lua/ulib/shared/hook.lua:183
    3. unknown - addons/ulib/lua/ulib/shared/commands.lua:1294
     4. unknown - lua/includes/modules/concommand.lua:69

The updated script by lynx results in the same error pants got, even after taking out the lua comments from the table.

Quote
[ERROR] lua/autorun/cooldown.lua:19: '}' expected (to close '{' at line 18) near '='
  1. unknown - lua/autorun/cooldown.lua:0
« Last Edit: April 01, 2014, 03:53:26 pm by ActeonAk »

Offline funkster

  • Newbie
  • *
  • Posts: 5
  • Karma: 0
Re: command time restrictions
« Reply #8 on: July 02, 2014, 12:57:19 am »
Hi total noob here, where do I paste this script? Does it go into the garrysmod/lua/autorun folder?

edit: +1 to the previous commment, first script doesnt allow me to use console cmds, and second script gives that error posted even after removing comments.
« Last Edit: July 02, 2014, 04:53:45 am by funkster »

Offline funkster

  • Newbie
  • *
  • Posts: 5
  • Karma: 0
Re: command time restrictions
« Reply #9 on: July 03, 2014, 11:35:37 am »
Although I have no idea about code, I think it's meant to be like this???
Code: [Select]
--[[----------------------------------
ULX Command Cooldown
Originally coded by: Cobalt
Updated by: lynx
Updated: March 3, 2014
----------------------------------]]--
groupcooldown = {}

--[[ Begin Configuration Section ]]--

-- groupcooldown.default
-- This sets the default cooldown for groups not listed in the grouplist.
groupcooldown.default = 15

-- groupcooldown.grouplist
-- This list defines the cooldown times for the groups. If a group is unlisted and not on the whitelist, it will default to default above.
-- This is a table. Please follow the example when adding groups.
groupcooldown.grouplist = {
["superadmin"] = 10;
["admin"] = 5;
["operator"] = 3;
}

-- groupcooldown.whitelist
-- Groups and Steam ID's defined in this list are exempt from the command cooldown regardless if their group is listed above.
groupcooldown.whitelist = { "owner", "developer", "superadmin", "STEAM_0:0:0", "STEAM_0:0:29798183" }

-- groupcooldown.message
-- This is the message dispalyed to the user. A second message will inform them of the time they have remaining to wait.
groupcooldown.message = "You are not allowed to do that right now."

--[[ End Configuration. Please do not modify anything below this line otherwise stuff will break. ]]--

function CmdCooldown(ply, cmd, args)
if table.HasValue( groupcooldown.whitelist, ply:SteamID() ) or table.HasValue( groupcooldown.whitelist, ply:GetUserGroup() ) then
return
end

if not ply.NextCommand or ply.NextCommand <= CurTime() then
ply.NextCommand = CurTime() + ( groupcooldown.grouplist[ply:GetUserGroup()] or groupcooldown.default )
end

if ply.NextCommand > CurTime() then
local nextCmd = ply.NextCommand - CurTime()
ULib.tsay( ply, groupcooldown.message )
ULib.tsay( ply, string.format( "You have %s seconds to wait before you can use a command again.", math.floor(nextCmd) ) )
return false
end
end

timer.Simple( 3, function()
hook.Add( "ULibCommandCalled", "CheckCommands", CmdCooldown )
end )

It doesn't work even though it doesn't give any errors. It doesn't allow any commands at all, just displays wait times and you can't do this command.
« Last Edit: July 03, 2014, 11:38:23 am by funkster »

  • Print