ULX

Author Topic: Running Function when Console Commands are Executed  (Read 3047 times)

0 Members and 1 Guest are viewing this topic.

Offline AKTS

  • Newbie
  • *
  • Posts: 5
  • Karma: 1
Running Function when Console Commands are Executed
« on: February 11, 2015, 03:01:40 PM »
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: [Select]
// 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

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.


Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Running Function when Console Commands are Executed
« Reply #1 on: February 11, 2015, 06:42:50 PM »
–Snip–
« Last Edit: February 12, 2015, 05:38:52 AM by Bytewave »
bw81@ulysses-forums ~ % whoami
Homepage

Offline AKTS

  • Newbie
  • *
  • Posts: 5
  • Karma: 1
Re: Running Function when Console Commands are Executed
« Reply #2 on: February 11, 2015, 08:40:47 PM »
ply:Nick(), not ply:Name().
That's probably the script breaker right there.

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.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Running Function when Console Commands are Executed
« Reply #3 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.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline AKTS

  • Newbie
  • *
  • Posts: 5
  • Karma: 1
Re: Running Function when Console Commands are Executed
« Reply #4 on: February 11, 2015, 09:10:05 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.

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.
« Last Edit: February 11, 2015, 09:22:16 PM by AKTS »

Offline Bite That Apple

  • Hero Member
  • *****
  • Posts: 858
  • Karma: 416
  • Apple Innovations 2010®
    • Fun 4 Everyone Gaming
Re: Running Function when Console Commands are Executed
« Reply #5 on: February 12, 2015, 12:09:53 AM »
ply:Nick(), not ply:Name().
That's probably the script breaker right there.

Not to be rude, you're wrong though. Player:Name(), Player:Nick(), and with exceptions to Player:GetName() are all the same thing. It's just mainstream to use :Nick, instead of :Name, and :GetName is more strictly use for entities; which is sometimes also known as a player.
Quote from: John F. Kennedy 1963
A man may die, nations may rise and fall, but an idea lives on.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Running Function when Console Commands are Executed
« Reply #6 on: February 12, 2015, 05:38:21 AM »
Not to be rude, you're wrong though. Player:Name(), Player:Nick(), and with exceptions to Player:GetName() are all the same thing. It's just mainstream to use :Nick, instead of :Name, and :GetName is more strictly use for entities; which is sometimes also known as a player.
Was informed about that prior. Apologies—I'd been so used to Nick(). Note to self: don't help after coming home exhausted from high school.
bw81@ulysses-forums ~ % whoami
Homepage

Offline Bite That Apple

  • Hero Member
  • *****
  • Posts: 858
  • Karma: 416
  • Apple Innovations 2010®
    • Fun 4 Everyone Gaming
Re: Running Function when Console Commands are Executed
« Reply #7 on: February 12, 2015, 05:09:55 PM »
Was informed about that prior. Apologies—I'd been so used to Nick(). Note to self: don't help after coming home exhausted from high school.

Ahhh. High School, how I miss those days :( College is not as fun.
Quote from: John F. Kennedy 1963
A man may die, nations may rise and fall, but an idea lives on.