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

155 lines
3.5 KiB
Lua
Raw Normal View History

2015-01-01 16:06:43 +01:00
-- Saves the number of messages from a user
2015-05-28 16:47:30 +02:00
-- Can check the number of messages with /stats
2015-01-01 16:06:43 +01:00
do
2015-05-28 16:47:30 +02:00
local NUM_MSG_MAX = 500
local TIME_CHECK = 4 -- seconds
local function user_print_name(user)
if user.print_name then
return user.print_name
end
local text = ''
if user.first_name then
text = user.last_name..' '
2014-12-31 17:45:52 +01:00
end
2015-05-28 16:47:30 +02:00
if user.lastname then
text = text..user.last_name
2014-12-31 17:45:52 +01:00
end
2015-05-28 16:47:30 +02:00
return text
2014-12-31 17:45:52 +01:00
end
2015-05-28 16:47:30 +02:00
-- Returns a table with `name` and `msgs`
local function get_msgs_user_chat(user_id, chat_id)
local user_info = {}
local uhash = 'user:'..user_id
local user = redis:hgetall(uhash)
local um_hash = 'msgs:'..user_id..':'..chat_id
user_info.msgs = tonumber(redis:get(um_hash) or 0)
2015-06-06 22:16:42 +02:00
--user_info.name = user_print_name(user)..' ('..user_id..')'
user_info.name = user_print_name(user)
2015-05-28 16:47:30 +02:00
return user_info
2014-12-31 17:45:52 +01:00
end
2015-05-28 16:47:30 +02:00
local function get_msg_num_stats(msg)
if msg.to.type == 'chat' then
local chat_id = msg.to.id
-- Users on chat
local hash = 'chat:'..chat_id..':users'
local users = redis:smembers(hash)
local users_info = {}
2015-01-01 16:06:43 +01:00
2015-05-28 16:47:30 +02:00
-- Get user info
for i = 1, #users do
local user_id = users[i]
local user_info = get_msgs_user_chat(user_id, chat_id)
table.insert(users_info, user_info)
end
2014-11-23 00:31:22 +01:00
2015-05-28 16:47:30 +02:00
-- Sort users by msgs number
table.sort(users_info, function(a, b)
if a.msgs and b.msgs then
return a.msgs > b.msgs
end
end)
2014-12-14 21:28:32 +01:00
2015-05-28 16:47:30 +02:00
local text = ''
for k,user in pairs(users_info) do
text = text..user.name..' => '..user.msgs..'\n'
2015-06-06 22:16:42 +02:00
text = string.gsub(text, "%_", " ")
2015-05-28 16:47:30 +02:00
end
return text
2015-02-11 22:00:00 +01:00
end
2015-05-28 16:47:30 +02:00
end
2015-02-02 23:00:08 +01:00
2015-05-28 16:47:30 +02:00
-- Save stats, ban user
local function pre_process(msg)
-- Save user on Redis
if msg.from.type == 'user' then
local hash = 'user:'..msg.from.id
if msg.from.print_name then
redis:hset(hash, 'print_name', msg.from.print_name)
2015-02-11 22:00:00 +01:00
end
2015-05-28 16:47:30 +02:00
if msg.from.first_name then
redis:hset(hash, 'first_name', msg.from.first_name)
2015-02-11 22:00:00 +01:00
end
2015-05-28 16:47:30 +02:00
if msg.from.last_name then
redis:hset(hash, 'last_name', msg.from.last_name)
end
end
-- Save stats on Redis
if msg.to.type == 'chat' then
-- User is on chat
local hash = 'chat:'..msg.to.id..':users'
redis:sadd(hash, msg.from.id)
2015-02-11 22:00:00 +01:00
end
2015-05-28 16:47:30 +02:00
-- Total user msgs
local hash = 'msgs:'..msg.from.id..':'..msg.to.id
redis:incr(hash)
-- Check flood
if msg.from.type == 'user' then
local hash = 'user:'..msg.from.id..':msgs'
local msgs = tonumber(redis:get(hash) or 0)
if msgs > NUM_MSG_MAX then
print('User '..msg.from.id..'is flooding '..msgs)
msg = nil
end
redis:setex(hash, TIME_CHECK, msgs+1)
end
return msg
end
local function get_bot_stats()
local redis_scan = [[
local cursor = '0'
local count = 0
repeat
local r = redis.call("SCAN", cursor, "MATCH", KEYS[1])
cursor = r[1]
count = count + #r[2]
until cursor == '0'
return count]]
-- Users
local hash = 'msgs:*:'..our_id
local r = redis:eval(redis_scan, 1, hash)
local text = 'User: '..r
2015-05-28 16:47:30 +02:00
hash = 'chat:*:users'
r = redis:eval(redis_scan, 1, hash)
text = text..'\nGruppen: '..r
2015-05-28 16:47:30 +02:00
2015-02-11 22:00:00 +01:00
return text
2015-05-28 16:47:30 +02:00
2014-11-22 20:17:33 +01:00
end
2015-01-01 16:06:43 +01:00
local function run(msg, matches)
2015-05-28 16:47:30 +02:00
if matches[1]:lower() == "stats" then
if msg.to.type == 'chat' then
return get_msg_num_stats(msg)
elseif is_sudo(msg) then
return get_bot_stats()
2015-04-11 19:27:09 +02:00
else
2015-06-06 22:16:42 +02:00
return 'Das funktioniert hier nicht'
2015-04-11 19:27:09 +02:00
end
2015-02-11 22:00:00 +01:00
end
2014-12-31 17:45:52 +01:00
end
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 = {"^#([Ss]tats)$"},
2015-04-12 23:53:42 +02:00
run = run,
2015-05-28 16:47:30 +02:00
pre_process = pre_process
2015-01-01 16:06:43 +01:00
}
2015-05-28 16:47:30 +02:00
end