From ec57c247a75617178448c14ab301fe62b2c0151b Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Wed, 15 Jun 2016 21:31:15 +0200 Subject: [PATCH] Portiere Stats-Plugin --- otouto/bot.lua | 2 +- otouto/plugins/stats.lua | 135 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 otouto/plugins/stats.lua diff --git a/otouto/bot.lua b/otouto/bot.lua index e02224b..a9302c5 100644 --- a/otouto/bot.lua +++ b/otouto/bot.lua @@ -150,7 +150,7 @@ end function pre_process_msg(self, msg) for number,plugin in ipairs(self.plugins) do if plugin.pre_process and msg then - print('Preprocess #'..number) + -- print('Preprocess #'..number) -- remove comment to restore old behaviour plugin:pre_process(msg, self) end end diff --git a/otouto/plugins/stats.lua b/otouto/plugins/stats.lua new file mode 100644 index 0000000..12503a7 --- /dev/null +++ b/otouto/plugins/stats.lua @@ -0,0 +1,135 @@ +local stats = {} + +local utilities = require('otouto.utilities') +local redis = (loadfile "./otouto/redis.lua")() + +function stats:init(config) + stats.triggers = { + "^/([Ss]tats)$", + "^/([Ss]tats) (chat) (%-%d+)", + "^/([Ss]tats) (chat) (%d+)" + } + stats.doc = [[* +]]..config.cmd_pat..[[stats*: Zeigt Stats an +*]]..config.cmd_pat..[[stats* _chat_ __: Stats für Chat-ID (nur Superuser) +]] +end + +stats.command = 'stats' + +function stats:user_print_name(user) + if user.name then + return user.name + end + + local text = '' + if user.first_name then + text = user.last_name..' ' + end + if user.lastname then + text = text..user.last_name + end + + return text +end + +-- Returns a table with `name` and `msgs` +function stats: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) + user_info.name = stats:user_print_name(user) + return user_info +end + +function stats:chat_stats(chat_id) + -- Users on chat + local hash = 'chat:'..chat_id..':users' + local users = redis:smembers(hash) + local users_info = {} + + -- Get user info + for i = 1, #users do + local user_id = users[i] + local user_info = stats:get_msgs_user_chat(user_id, chat_id) + table.insert(users_info, user_info) + end + + -- 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) + + local text = '' + for k,user in pairs(users_info) do + text = text..user.name..': '..user.msgs..'\n' + text = string.gsub(text, "%_", " ") -- Bot API doesn't use underscores anymore! Yippie! + end + if text:isempty() then return 'Keine Stats für diesen Chat verfügbar!'end + return text +end + +function stats:pre_process(msg, self) + -- Ignore service msg + if msg.service then -- check how Bot API handles service msgs, will update this + print('Service message') + return + end + + -- Save user on Redis + local hash = 'user:'..msg.from.id + -- print('Saving user', hash) -- remove comment to restore old behaviour + if msg.from.name then + redis:hset(hash, 'name', msg.from.name) + end + if msg.from.first_name then + redis:hset(hash, 'first_name', msg.from.first_name) + end + if msg.from.last_name then + redis:hset(hash, 'last_name', msg.from.last_name) + end + + -- Save stats on Redis + if msg.chat.type ~= 'private' then + -- User is on chat + local hash = 'chat:'..msg.chat.id..':users' + redis:sadd(hash, msg.from.id) + end + + -- Total user msgs + local hash = 'msgs:'..msg.from.id..':'..msg.chat.id + redis:incr(hash) + return +end + +function stats:action(msg, config, matches) + if matches[1]:lower() == "stats" then + + if not matches[2] then + if msg.chat.type == 'private' then + utilities.send_reply(self, msg, 'Stats funktionieren nur in Chats!') + return + else + local chat_id = msg.chat.id + utilities.send_reply(self, msg, stats:chat_stats(chat_id)) + return + end + end + + if matches[2] == "chat" then + if msg.from.id ~= config.admin then + utilities.send_reply(self, msg, config.errors.sudo) + return + else + utilities.send_reply(self, msg, stats:chat_stats(matches[3])) + return + end + end + end +end + +return stats \ No newline at end of file