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

View File

@ -64,7 +64,6 @@
-- Where magic happens -- Where magic happens
function do_action(msg) function do_action(msg)
local receiver = get_receiver(msg) local receiver = get_receiver(msg)
--local text = msg.text:sub(2) -- removes the '!'
local text = msg.text local text = msg.text
print("Received msg", text) print("Received msg", text)
for name, desc in pairs(plugins) do for name, desc in pairs(plugins) do
@ -117,24 +116,8 @@
-- return -- return
-- end -- end
-- if string.starts(msg.text, '!set') then
-- local text = save_value(msg.text:sub(5,-1))
-- send_msg(receiver, text, ok_cb, false)
-- return
-- end
-- if string.starts(msg.text, '!get') then
-- local text = get_value(msg.text:sub(6,-1))
-- send_msg(receiver, text, ok_cb, false)
-- return
-- end
-- end -- end
function string.starts(String,Start)
return string.sub(String,1,string.len(Start))==Start
end
function load_config() function load_config()
local f = assert(io.open('./bot/config.json', "r")) local f = assert(io.open('./bot/config.json', "r"))
local c = f:read "*a" local c = f:read "*a"
@ -150,35 +133,6 @@
return config return config
end end
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 get_value( value_name )
-- If there is not value name, return all the values.
if (value_name == "" ) 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 "Cant find "..value_name
end
return value_name.." = "..value
end
function is_sudo(msg) function is_sudo(msg)
local var = false local var = false
-- Check users id in config -- Check users id in config
@ -275,23 +229,6 @@
return str; return str;
end end
function string.get_extension_from_filename( filename )
return filename:match( "%.([^%.]+)$" )
end
function string.get_last_word( words )
local splitted = split_by_space ( words )
return splitted[#splitted]
end
function split_by_space ( text )
words = {}
for word in string.gmatch(text, "[^%s]+") do
table.insert(words, word)
end
return words
end
function string:split(sep) function string:split(sep)
local sep, fields = sep or ":", {} local sep, fields = sep or ":", {}
local pattern = string.format("([^%s]+)", sep) local pattern = string.format("([^%s]+)", sep)

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
}