2015-01-06 21:04:45 +01:00
|
|
|
local _file_values = './data/values.lua'
|
|
|
|
|
|
|
|
function read_file_values( )
|
|
|
|
local f = io.open(_file_values, "r+")
|
|
|
|
-- If file doesn't exists
|
|
|
|
if f == nil then
|
|
|
|
-- Create a new empty table
|
|
|
|
print ('Created value file '.._file_values)
|
|
|
|
serialize_to_file({}, _file_values)
|
|
|
|
else
|
2015-02-15 12:12:03 +01:00
|
|
|
print ('Values loaded: '.._file_values)
|
2015-01-06 21:04:45 +01:00
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
return loadfile (_file_values)()
|
2014-11-23 12:25:33 +01:00
|
|
|
end
|
2014-11-23 00:31:22 +01:00
|
|
|
|
2015-01-06 21:04:45 +01:00
|
|
|
_values = read_file_values()
|
|
|
|
|
2014-12-26 12:13:27 +01:00
|
|
|
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
|
|
|
|
|
2014-12-14 21:44:19 +01:00
|
|
|
function get_value(chat, value_name)
|
|
|
|
|
|
|
|
-- If chat values is empty
|
|
|
|
if (_values[chat] == nil) then
|
|
|
|
return "There isn't any data"
|
|
|
|
end
|
|
|
|
|
2014-11-17 20:47:03 +01:00
|
|
|
-- If there is not value name, return all the values.
|
|
|
|
if (value_name == nil ) then
|
|
|
|
local text = ""
|
2014-12-14 21:44:19 +01:00
|
|
|
for key,value in pairs(_values[chat]) do
|
2014-11-17 20:47:03 +01:00
|
|
|
text = text..key.." = "..value.."\n"
|
|
|
|
end
|
|
|
|
return text
|
|
|
|
end
|
2014-12-14 21:44:19 +01:00
|
|
|
local value = _values[chat][value_name]
|
2014-11-17 20:47:03 +01:00
|
|
|
if ( value == nil) then
|
|
|
|
return "Can't find "..value_name
|
|
|
|
end
|
|
|
|
return value_name.." = "..value
|
|
|
|
end
|
|
|
|
|
|
|
|
function run(msg, matches)
|
2014-12-14 22:11:54 +01:00
|
|
|
local chat_id = tostring(msg.to.id)
|
2014-11-17 21:55:25 +01:00
|
|
|
if matches[1] == "!get" then
|
2014-12-14 22:11:54 +01:00
|
|
|
return get_value(chat_id, nil)
|
2014-11-17 21:55:25 +01:00
|
|
|
end
|
2014-12-14 22:11:54 +01:00
|
|
|
return get_value(chat_id, matches[1])
|
2014-11-17 20:47:03 +01:00
|
|
|
end
|
|
|
|
|
2014-12-26 12:13:27 +01:00
|
|
|
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
|
|
|
|
|
2014-11-17 20:47:03 +01:00
|
|
|
return {
|
2015-02-04 23:03:42 +01:00
|
|
|
description = "Retrieves variables saved with !set",
|
|
|
|
usage = "!get (value_name): Returns the value_name value.",
|
2014-11-17 21:55:25 +01:00
|
|
|
patterns = {
|
|
|
|
"^!get (%a+)$",
|
2015-02-04 23:03:42 +01:00
|
|
|
"^!get$"},
|
2014-12-26 12:13:27 +01:00
|
|
|
run = run,
|
|
|
|
lex = lex
|
2014-11-17 20:47:03 +01:00
|
|
|
}
|
2014-12-26 12:13:27 +01:00
|
|
|
|