Multiple plugins in one message
This commit is contained in:
54
bot/bot.lua
54
bot/bot.lua
@ -5,7 +5,7 @@ json = (loadfile "./libs/JSON.lua")()
|
||||
serpent = (loadfile "./libs/serpent.lua")()
|
||||
require("./bot/utils")
|
||||
|
||||
VERSION = '0.8.0'
|
||||
VERSION = '0.8.1'
|
||||
|
||||
function on_msg_receive (msg)
|
||||
vardump(msg)
|
||||
@ -14,7 +14,6 @@ function on_msg_receive (msg)
|
||||
return
|
||||
end
|
||||
|
||||
update_user_stats(msg)
|
||||
do_action(msg)
|
||||
|
||||
mark_read(get_receiver(msg), ok_cb, false)
|
||||
@ -30,7 +29,6 @@ function on_binlog_replay_end ()
|
||||
-- See plugins/ping.lua as an example for cron
|
||||
|
||||
_config = load_config()
|
||||
_users = load_user_stats()
|
||||
|
||||
-- load plugins
|
||||
plugins = {}
|
||||
@ -63,16 +61,15 @@ function do_action(msg)
|
||||
for name, desc in pairs(plugins) do
|
||||
-- print("Trying module", name)
|
||||
for k, pattern in pairs(desc.patterns) do
|
||||
-- print("Trying", text, "against", pattern)
|
||||
print("Trying", text, "against", pattern)
|
||||
matches = { string.match(text, pattern) }
|
||||
if matches[1] then
|
||||
print(" matches",pattern)
|
||||
print(" matches", pattern)
|
||||
if desc.run ~= nil then
|
||||
result = desc.run(msg, matches)
|
||||
print(" sending", result)
|
||||
if (result) then
|
||||
print(" sending", result)
|
||||
_send_msg(receiver, result)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -105,6 +102,7 @@ function save_config( )
|
||||
})
|
||||
file:write(serialized)
|
||||
file:close()
|
||||
print ('saved config into ./bot/config.lua')
|
||||
end
|
||||
|
||||
|
||||
@ -150,47 +148,7 @@ function create_config( )
|
||||
})
|
||||
file:write(serialized)
|
||||
file:close()
|
||||
end
|
||||
|
||||
function update_user_stats(msg)
|
||||
-- Save user to _users table
|
||||
local from_id = tostring(msg.from.id)
|
||||
local to_id = tostring(msg.to.id)
|
||||
local user_name = get_name(msg)
|
||||
-- If last name is nil dont save last_name.
|
||||
local user_last_name = msg.from.last_name
|
||||
local user_print_name = msg.from.print_name
|
||||
if _users[to_id] == nil then
|
||||
_users[to_id] = {}
|
||||
end
|
||||
if _users[to_id][from_id] == nil then
|
||||
_users[to_id][from_id] = {
|
||||
name = user_name,
|
||||
last_name = user_last_name,
|
||||
print_name = user_print_name,
|
||||
msg_num = 1
|
||||
}
|
||||
else
|
||||
local actual_num = _users[to_id][from_id].msg_num
|
||||
_users[to_id][from_id].msg_num = actual_num + 1
|
||||
-- And update last_name
|
||||
_users[to_id][from_id].last_name = user_last_name
|
||||
end
|
||||
end
|
||||
|
||||
function load_user_stats()
|
||||
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
|
||||
print ('saved config into ./bot/config.lua')
|
||||
end
|
||||
|
||||
function on_our_id (id)
|
||||
|
Reference in New Issue
Block a user