General > Developers Corner

Running Function when Console Commands are Executed

(1/2) > >>

AKTS:
I'm editing a damagelog system to add all types of new logs. Everything I've added so far has been pretty simple: take the hooks for the event and make it append the logs with the relevant information when the event is executed. I've kind of run into a wall when I try to make something run when a console command is executed. I can't find any relevant hooks, so I tried to do something like this:


--- Code: ---// I didn't append anything just so I could get this to run and properly notify.
function allcp(msg)
for i, v in pairs(player.GetAll()) do
v:ChatPrint(msg)
end
end

local crun = concommand.Run

function concommand.Run(ply, cmd, args)
if !IsValid(ply) then return crun(ply,cmd,args) end
if !cmd then return crun(ply,cmd,args) end

if args and args ~= "" then
allcp(ply:Name() .. " has executed this command: " .. cmd .. " " .. tostring(args) .. ".")
else
allcp(ply:Name() .. " has executed this command: " .. cmd .. ".")
end
return crun(ply, cmd, args)

end

--- End code ---

This just doesn't work in general. To see where it breaks, I took out the returns and figured out the problem. Apparently ply is not valid, so it simply returns (I got an error that the method "Name" was a nil value). Oddly enough, this does notify if the command is not run from the console, but by a script running the command. For example, this appears in my chat automatically:
AKTS has executed this command: fas2_handrig_applynow

Does anyone have any suggestions as to how to go about doing this? I'm at an impasse at the moment.

Bytewave:
–Snip–

AKTS:

--- Quote from: Bytewave on February 11, 2015, 06:42:50 PM ---ply:Nick(), not ply:Name().
That's probably the script breaker right there.

--- End quote ---

First off, thanks for responding!

I've used :Name() as opposed to :Nick() for a while now, and it seems to work everywhere else from ULX commands to other logs within the system I use. Also the documentation
for GMOD says that ply:Nick(), ply:Name(), and ply:GetName() are all identical, so I never bothered to change my habit. Changing it from "Name" to "Nick" did not seem to resolve the problem.

JamminR:
In shared code, of course, Console can never be a player.
ULX uses static string "Console" for logging when ply !IsValid for commands run in shared space from server console.
Unless the actual addon/code is passing what player accessed the command through the consolecommand, I really don't think there's a way to show who ran the code that the serverside script then ran from console.
I could be wrong.

AKTS:

--- Quote from: JamminR on February 11, 2015, 08:55:58 PM ---In shared code, of course, Console can never be a player.
ULX uses static string "Console" for logging when ply !IsValid for commands run in shared space from server console.
Unless the actual addon/code is passing what player accessed the command through the consolecommand, I really don't think there's a way to show who ran the code that the serverside script then ran from console.
I could be wrong.

--- End quote ---

Darn, that's a shame. I suppose it's the least-needed log though, so there's really no harm in not having it. I've seen it done elsewhere, but I'm going to not bother with it any more if it's going to take this amount of time.

EDIT: Oops no cursies.

Navigation

[0] Message Index

[#] Next page

Go to full version