Ulysses
General => Developers Corner => Topic started by: Adult on January 10, 2013, 07:34:43 AM
-
Hey there, I was trying to make a new command, and I was getting a strange error involving ULib.cmds.TranslateCommand.
Here is the code I used:
local f = "ULib/restricted_concommands.txt"
local data = file.Read( f, "DATA" ) or {}
--------------- Helpers -----------------
local function saveFile()
file.Write( f, util.TableToKeyValues( data ) )
end
local function loadFile()
data = file.Read( f, "DATA" ) or {}
end
-- Important that you don't call this after making
-- changes to permissions without saving your changes.
local function loadFuncs()
local old_comms = concommand.GetTable()
loadFile()
for k,v in pairs( old_comms ) do
concommand.Remove( k ) -- Remove it first
-- Create a new function that calls the hook first
local new = function( ply, cmd, args, str )
local allow = hook.Call( "ConCommandRan", GAMEMODE, ply, cmd, args, str )
if not allow then return end
v( ply, cmd, args, str )
end
-- Use that function as the new console command.
concommand.Add( k, new )
end
end
-- Can the player use the command?
local function allowed( cmd, ply )
local group = ply:GetUserGroup()
local groupInfo = data[group]
return groupInfo.allow[cmd] ~= false
end
--------------------------------------------------
-- ulx --
function ulx.groupallowcmd( caller, group, cmd )
local groupInfo = data[group]
if not groupInfo.allow[cmd] then
ULib.tsayError( caller, "Group \'#s\' does not have access to #s", group, cmd )
return false
end
groupInfo.allow[cmd] = true
saveFile()
ulx.fancyLogAdmin( caller, false, "#A restricted command #s from group #s.", cmd, group )
end
local allow = ulx.command( "User Management", ulx.groupallowcmd, "ulx groupallowcmd" )
allow:addParam{ type=ULib.cmds.StringArg, completes=ulx.group_names, hint="group", error="invalid group \"%s\" specified", ULib.cmds.restrictToCompletes }
allow:addParam{ type=ULib.cmds.StringArg, hint="command" }
allow:defaultAccess( ULib.ACCESS_SUPERADMIN )
allow:help( "Allows a group's access to a command." )
function ulx.groupdenycmd( caller, group, cmd )
local groupInfo = data[group]
if not groupInfo.allow[cmd] then
ULib.tsayError( caller, "Group \'#s\' does not have access to #s", group, cmd )
return false
end
groupInfo.allow[cmd] = false
saveFile()
ulx.fancyLogAdmin( caller, false, "#A restricted command #s from group #s.", cmd, group )
end
local deny = ulx.command( "User Management", ulx.groupdenycmd, "ulx groupdenycmd" )
deny:addParam{ type=ULib.cmds.StringArg, completes=ulx.group_names, hint="group", error="invalid group \"%s\" specified", ULib.cmds.restrictToCompletes }
deny:addParam{ type=ULib.cmds.StringArg, hint="command" }
deny:defaultAccess( ULib.ACCESS_SUPERADMIN )
deny:help( "Denies a group's access to a command." )
-- hooks --
local function concommandRan( ply, cmd, args, str )
if not allowed( cmd, ply ) then
ULib.tsayError( ply, "You don't have access to this command, " .. ply:Name() )
return false
end
return true
end
hook.Add( "ConCommandRan", "ULX_RestrictConcommands", concommandRan )
local function init()
-- New hook, leave it blank for now. We'll right the ulx hook later.
-- return true to allow it to run, return false to deny.
function GAMEMODE:ConCommandRan( ply, cmd, args, str )
return true
end
loadFuncs()
end
hook.Add( "Initialize", "ULX_LoadConcommands", init )
and here is the error:
(http://i.imgur.com/4bifx.png)
To save you the trouble, the error is referring to this function:
function ulx.groupallowcmd( caller, group, cmd )
local groupInfo = data[group]
if not groupInfo.allow[cmd] then
ULib.tsayError( caller, "Group \'#s\' does not have access to #s", group, cmd )
return false
end
groupInfo.allow[cmd] = true
saveFile()
ulx.fancyLogAdmin( caller, false, "#A restricted command #s from group #s.", cmd, group )
end
-- THIS LINE SPECIFICALLY. ->>> local allow = ulx.command( "User Management", ulx.groupallowcmd, "ulx groupallowcmd" )
allow:addParam{ type=ULib.cmds.StringArg, completes=ulx.group_names, hint="group", error="invalid group \"%s\" specified", ULib.cmds.restrictToCompletes }
allow:addParam{ type=ULib.cmds.StringArg, hint="command" }
allow:defaultAccess( ULib.ACCESS_SUPERADMIN )
allow:help( "Allows a group's access to a command." )
As you can tell, the script is saved in ulx/lua/ulx/modules/sh
-
The format for ulx.command is:
ulx.command( (string) category, (string) consoleCommand, (function) callback, (string) chatCommand )
So the error is what I would expect to see -- it is expecting a string, but is given a function instead.
-
Oh my, how silly of me. Thank you.