This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot/plugins/stats.lua

116 lines
2.8 KiB
Lua
Raw Normal View History

2015-01-01 16:06:43 +01:00
-- Saves the number of messages from a user
-- Can check the number of messages with !stats
do
local socket = require('socket')
2015-01-06 21:04:45 +01:00
local _file_stats = './data/stats.lua'
2015-01-01 16:06:43 +01:00
local _stats
2014-12-31 17:45:52 +01:00
function update_user_stats(msg)
2015-01-01 16:06:43 +01:00
-- Save user to stats table
2014-12-31 17:45:52 +01:00
local from_id = tostring(msg.from.id)
local to_id = tostring(msg.to.id)
local user_name = get_name(msg)
print ('New message from '..user_name..'['..from_id..']'..' to '..to_id)
2014-12-31 17:45:52 +01:00
-- 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
2015-01-01 16:06:43 +01:00
if _stats[to_id] == nil then
print ('New stats key to_id: '..to_id)
_stats[to_id] = {}
2014-12-31 17:45:52 +01:00
end
2015-01-01 16:06:43 +01:00
if _stats[to_id][from_id] == nil then
print ('New stats key from_id: '..to_id)
_stats[to_id][from_id] = {
2015-02-02 23:00:08 +01:00
user_id = from_id,
2014-12-31 17:45:52 +01:00
name = user_name,
last_name = user_last_name,
print_name = user_print_name,
msg_num = 1
}
else
2015-01-01 16:06:43 +01:00
print ('Updated '..to_id..' '..from_id)
local actual_num = _stats[to_id][from_id].msg_num
_stats[to_id][from_id].msg_num = actual_num + 1
2015-02-02 23:00:08 +01:00
_stats[to_id][from_id].user_id = from_id
2015-01-01 16:06:43 +01:00
_stats[to_id][from_id].last_name = user_last_name
2014-12-31 17:45:52 +01:00
end
end
2015-01-01 16:06:43 +01:00
function read_file_stats( )
local f = io.open(_file_stats, "r+")
2014-12-31 17:45:52 +01:00
-- If file doesn't exists
if f == nil then
2015-01-01 16:06:43 +01:00
-- Create a new empty table
print ('Created user stats file '.._file_stats)
serialize_to_file({}, _file_stats)
2014-12-31 17:45:52 +01:00
else
2015-01-01 16:06:43 +01:00
print ('Stats loaded: '.._file_stats)
f:close()
2014-12-31 17:45:52 +01:00
end
2015-01-01 16:06:43 +01:00
return loadfile (_file_stats)()
2014-12-31 17:45:52 +01:00
end
2015-01-01 16:06:43 +01:00
local function save_stats()
2015-02-11 22:00:00 +01:00
-- Save stats to file
serialize_to_file(_stats, _file_stats)
2014-12-31 17:45:52 +01:00
end
2014-11-23 00:31:22 +01:00
2015-01-01 16:06:43 +01:00
local function get_stats_status( msg )
2015-02-11 22:00:00 +01:00
-- vardump(stats)
local text = ""
2015-02-02 23:00:08 +01:00
local to_id = tostring(msg.to.id)
2015-02-11 22:00:00 +01:00
local rank = {}
2014-12-14 21:28:32 +01:00
2015-02-11 22:00:00 +01:00
for id, user in pairs(_stats[to_id]) do
table.insert(rank, user)
end
2015-02-02 23:00:08 +01:00
2015-02-11 22:00:00 +01:00
table.sort(rank, function(a, b)
if a.msg_num and b.msg_num then
return a.msg_num > b.msg_num
end
end
)
2015-02-02 23:00:08 +01:00
2015-02-11 22:00:00 +01:00
for id, user in pairs(rank) do
2015-02-02 23:00:08 +01:00
-- Previous versions didn't save that
user_id = user.user_id or ''
2015-02-11 22:00:00 +01:00
print(">> ", id, user.name)
if user.last_name == nil then
2015-04-15 18:44:42 +02:00
text = text..user.name..": "..user.msg_num.."\n"
2015-02-11 22:00:00 +01:00
else
2015-04-23 17:29:42 +02:00
--text = text..user.name.." "..user.last_name..": "..user.msg_num.."\n"
text = text..user.name..": "..user.msg_num.."\n"
2015-02-11 22:00:00 +01:00
end
end
print("usuarios: "..text)
return text
2014-11-22 20:17:33 +01:00
end
2015-01-01 16:06:43 +01:00
local function run(msg, matches)
2015-04-11 19:27:09 +02:00
if matches[1] == "stats" then
2015-04-12 17:56:44 +02:00
if msg.to.type == 'chat' or is_sudo(msg) then
2015-04-11 19:27:09 +02:00
return get_stats_status(msg)
else
return 'Stats works only chats'
end
2015-02-11 22:00:00 +01:00
else
update_user_stats(msg)
end
2014-12-31 17:45:52 +01:00
end
2015-01-01 16:06:43 +01:00
_stats = read_file_stats()
2014-12-31 17:45:52 +01:00
2014-11-22 20:17:33 +01:00
return {
2015-04-28 17:49:11 +02:00
description = "Zeigt wieviel ihr spamt",
usage = {"/stats"},
patterns = {"^/(stats)",},
2015-04-12 23:53:42 +02:00
run = run,
cron = save_stats
2015-01-01 16:06:43 +01:00
}
2015-03-20 14:27:38 +01:00
end