Well firstly, if you wish to solve the range problem you can do 1 of 2 things.
1. If you have access to the SMP configs, bump up the range
2. Use OpenPeripheral, the terminal have unlimited range and can work cross-dimension too.
Now to the actual problem:
It is completely possible to parse a command and get the args from chat, firstly I would do this:
Using the method that Lyqyd posted we can extract the first word and the args like so
local cmd, args = chatInput:match("(%a+) (.+)")
Now this will actually parse all chat messages, which if not coded correctly, can become problematic with the computer. So we have it detect the ! at the start, if that doesn't exist, then it will return nil
local cmd, args = chatInput:match("!(%a+) (.+)")
if cmd then
--# we now have a valid format of request through chat
end
Now we can also build a database of commands with a table like so
local commands = {
["request"] = doRequest,
["other"] = doOther,
["another"] = doAnother,
}
Now the format is the command (must be one word) is the key for the table and a function pointer as the value. A function pointer if you don't know is the address to the location in memory that a function is stored, so for example
function test()
print("hello world")
end
is a function, and using `test` would get us the function pointer, using `print(test)` you can see this location in memory as "table: [location]" but we are straying from topic, so lets bring it back.
We then must define the function
local function doRequest( args )
--# process the args and perform the request
end
now we should also check that the users command was actually a valid one, for this we do the following
if commands[cmd] then
--# it was in the table, it is valid
else
--# invalid command
end
So if we put together what we have so far we get the following
local function doRequest( args )
--# process the args and perform the request
end
local commands = {
["request"] = doRequest,
}
local cmd, args = chatInput:match("!(%a+) (.+)")
if cmd then
if commands[cmd] then
commands[cmd]() --# call the function pointer, we have been given th
else
--# alert user that it was an invalid command
end
--# no else here, it wasn't a command for us
end
As for in the `doRequest` function, there are several methods of attack, this is the one I like to use
local function doRequest(args)
local qty, name = args:match("(%d+) (%a+)")
if not (qty and name) then
--# alert the user that they have mistyped the command and it must be "!request [qty] [name]"
return
end
--# valid command, process the request here
end
Now all you need to do is put the process of gathering and processing the command into a loop, actually listen for the command, and you're set to having a program that can respond to commands.
A great tutorial on Patterns in Lua and how they work can be found in the Tutorials section and also the
PIL.