From 48b17bd5d0e1a5bfbdcff733d39e2808af3a9d27 Mon Sep 17 00:00:00 2001 From: yago Date: Sun, 23 Nov 2014 12:25:33 +0100 Subject: [PATCH] values.json and users.json are created if not exists --- .gitignore | 1 + bot/bot.lua | 19 ++++++++++++++----- plugins/get.lua | 14 +++++++++++--- res/.gitkeep | 0 res/users.json | 3 --- res/values.json | 3 --- 6 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 res/.gitkeep delete mode 100644 res/users.json delete mode 100644 res/values.json diff --git a/.gitignore b/.gitignore index e69de29..d6d2cd0 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +res/ \ No newline at end of file diff --git a/bot/bot.lua b/bot/bot.lua index dce382f..3126f44 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -3,7 +3,7 @@ URL = require("socket.url") json = (loadfile "./bot/JSON.lua")() - VERSION = 'v0.7.3' + VERSION = 'v0.7.4' -- taken from http://stackoverflow.com/a/11130774/3163199 function scandir(directory) @@ -16,7 +16,7 @@ end function on_msg_receive (msg) - --vardump(msg) + vardump(msg) if msg_valid(msg) == false then return @@ -240,9 +240,18 @@ end function load_user_stats() - local f = assert(io.open('./res/users.json', "r")) - local c = f:read "*a" - return json:decode(c) + local f = io.open('res/users.json', "r+") + -- If file doesn't exists + if f == nil then + f = io.open('res/users.json', "w+") + f:write("{}") -- Write empty table + f:close() + return {} + else + local c = f:read "*a" + f:close() + return json:decode(c) + end end function get_receiver(msg) diff --git a/plugins/get.lua b/plugins/get.lua index d42c4da..18c2765 100644 --- a/plugins/get.lua +++ b/plugins/get.lua @@ -1,6 +1,14 @@ -local f = assert(io.open('./res/values.json', "r+")) -local c = f:read "*a" -_values = json:decode(c) +local f = io.open('./res/values.json', "r+") +if f == nil then + f = io.open('./res/values.json', "w+") + f:write("{}") -- Write empty table + f:close() + _values = {} +else + local c = f:read "*a" + f:close() + _values = json:decode(c) +end function get_value( value_name ) -- If there is not value name, return all the values. diff --git a/res/.gitkeep b/res/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/res/users.json b/res/users.json deleted file mode 100644 index 0e0dcd2..0000000 --- a/res/users.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - -} \ No newline at end of file diff --git a/res/values.json b/res/values.json deleted file mode 100644 index 0e0dcd2..0000000 --- a/res/values.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - -} \ No newline at end of file