lexical replacements

This commit is contained in:
David Muñoz
2014-12-26 12:13:27 +01:00
parent 8ca367e83b
commit d40346b481
2 changed files with 57 additions and 4 deletions

View File

@ -10,6 +10,17 @@ else
_values = json:decode(c)
end
function fetch_value(chat, value_name)
if (_values[chat] == nil) then
return nil
end
if (value_name == nil ) then
return nil
end
local value = _values[chat][value_name]
return value
end
function get_value(chat, value_name)
-- If chat values is empty
@ -40,11 +51,27 @@ function run(msg, matches)
return get_value(chat_id, matches[1])
end
function lex(msg, text)
local chat_id = tostring(msg.to.id)
local s, e = text:find("%$%a+")
if (s == nil) then
return nil
end
local var = text:sub(s + 1, e)
local value = fetch_value(chat_id, var)
if (value == nil) then
value = "(unknown value " .. var .. ")"
end
return text:sub(0, s - 1) .. value .. text:sub(e + 1)
end
return {
description = "retrieves variables saved with !set",
usage = "!get (value_name)",
patterns = {
"^!get (%a+)$",
"^!get$"},
run = run
run = run,
lex = lex
}