Clean. Get and Set

This commit is contained in:
yago
2014-11-17 20:47:03 +01:00
parent 88e0336227
commit dde09a6017
3 changed files with 52 additions and 63 deletions

27
plugins/get.lua Normal file
View File

@ -0,0 +1,27 @@
function get_value( value_name )
-- If there is not value name, return all the values.
if (value_name == nil ) then
local text = ""
for key,value in pairs(config.values) do
text = text..key.." = "..value.."\n"
end
return text
end
local value = config.values[value_name]
if ( value == nil) then
return "Can't find "..value_name
end
return value_name.." = "..value
end
function run(msg, matches)
local text = matches[1]
return text
end
return {
description = "retrieves variables saved with !set",
usage = "!get (value_name)",
patterns = {"^!get ?(%a+)?$"},
run = run
}

25
plugins/set.lua Normal file
View File

@ -0,0 +1,25 @@
function save_value( text )
var_name, var_value = string.match(text, "(%a+) (.+)")
if (var_name == nil or var_value == nil) then
return "Usage: !set var_name value"
end
config.values[var_name] = var_value
local json_text = json:encode_pretty(config)
file = io.open ("./bot/config.json", "w+")
file:write(json_text)
file:close()
return "Saved "..var_name.." = "..var_value
end
function run(msg, matches)
local text = save_value(msg.text)
return text
end
return {
description = "Set value",
usage = "!set [value_name] [data]",
patterns = {"^!set (%a+) (.+)$"},
run = run
}