ULX

Author Topic: How does ULib.sayCmds work?  (Read 2292 times)

0 Members and 1 Guest are viewing this topic.

Offline JasonMan

  • Newbie
  • *
  • Posts: 12
  • Karma: -1
How does ULib.sayCmds work?
« on: February 22, 2016, 04:23:26 AM »
I have a code that automatically detects all chat messages that have "!" or "/" as their first character, and redirects them to a command. If a command doesn't exist, it says "blah is not a registered command. For a list of registered commands, please type !commands".
Now, ulx has it's own set of chat commands, so I wanted to just skip the code if the command is registered.
So I did this:

Code: [Select]
function CCheckForChatCommand( ply, text )
local args = string.Split( text, " " )
if table.HasValue( table.GetKeys( ULib.sayCmds ), args[1] ) then return end
--Rest of code
end

But, for some reason, it always returns false.
So I wanted to check it with lua_run and a string I put myself.


Why does this not work?

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: How does ULib.sayCmds work?
« Reply #1 on: February 22, 2016, 08:10:29 AM »
You're splitting the string at every space. If you type !command, string.Split will return { "!command" } (a table with one value, the chat message). If you type !command argument, string.Split will return { "!command", "argument" }. I don't know exactly how ULib.sayCmds is set up, but I assume that its keys (if the keys are the command names) do not contain the exclamation mark at the front. If they do, then I guess you're having another problem.

edit:

After looking at the ULib repo, it looks like ULib.sayCmds's keys are commands and they are the exact same as the command, so if the command is !command then ULib.sayCmds["!command"] should exist. The only reason I could think of for them not matching is trailing or leading spaces. string.Trim() both the command input and the ULib.sayCmds key and see if it works then.
« Last Edit: February 22, 2016, 08:18:07 AM by roastchicken »
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline JasonMan

  • Newbie
  • *
  • Posts: 12
  • Karma: -1
Re: How does ULib.sayCmds work?
« Reply #2 on: February 22, 2016, 09:05:13 AM »
So apparently ULib.sayCmds stores the keys with a space at the end of them ("!armor " and not "!armor"), so that worked.
Thanks