diff --git a/bot/bot.lua b/bot/bot.lua index 032c486..0f22156 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -64,7 +64,6 @@ -- Where magic happens function do_action(msg) local receiver = get_receiver(msg) - --local text = msg.text:sub(2) -- removes the '!' local text = msg.text print("Received msg", text) for name, desc in pairs(plugins) do @@ -117,24 +116,8 @@ -- return -- 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 - function string.starts(String,Start) - return string.sub(String,1,string.len(Start))==Start - end - function load_config() local f = assert(io.open('./bot/config.json', "r")) local c = f:read "*a" @@ -150,35 +133,6 @@ return config 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) local var = false -- Check users id in config @@ -275,23 +229,6 @@ return str; 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) local sep, fields = sep or ":", {} local pattern = string.format("([^%s]+)", sep) diff --git a/plugins/get.lua b/plugins/get.lua new file mode 100644 index 0000000..397992f --- /dev/null +++ b/plugins/get.lua @@ -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 +} diff --git a/plugins/set.lua b/plugins/set.lua new file mode 100644 index 0000000..c0c1420 --- /dev/null +++ b/plugins/set.lua @@ -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 +} +