From ebd4a30c705ac2c708d772181367b0b2926e44e9 Mon Sep 17 00:00:00 2001 From: Jinks Date: Sat, 24 Oct 2015 03:47:44 +0200 Subject: [PATCH] Seen plugin --- luameat.lua | 24 +++++++++++++++++++++++- misc.lua | 29 +++++++++++++++++++++++++++++ plugin/seen.lua | 46 ++++++++++++++++++++++++++++++++++++++++++++++ plugin/twitch.lua | 18 +----------------- 4 files changed, 99 insertions(+), 18 deletions(-) create mode 100644 misc.lua create mode 100644 plugin/seen.lua diff --git a/luameat.lua b/luameat.lua index edfe21d..35b84e5 100644 --- a/luameat.lua +++ b/luameat.lua @@ -1,10 +1,14 @@ require "luarocks.loader" local irc = require 'irc' -require('socket.http') +sqlite3 = require "lsqlite3" plugin = {} callback = {} +on_join = {} +on_part = {} + +DB = sqlite3.open("bot.db") require('config') @@ -29,6 +33,23 @@ irc.register_callback("channel_msg", function(channel, from, message) end end) +irc.register_callback("join", function(channel, from) + for k,v in pairs(on_join) do + if type(v) == "function" then + v(channel.name, from) + end + end +end) + +irc.register_callback("part", function(channel, from, message) + for k,v in pairs(on_part) do + if type(v) == "function" then + v(channel.name, from) + end + end +end) + + -- irc.register_callback("private_msg", function(from, message) -- local is_cmd, cmd, arg = message:match("^(!)(%w+) (.*)$") -- if is_cmd and plugin[cmd] then @@ -44,5 +65,6 @@ end) irc.register_callback("nick_change", function(from, old_nick) end) --]] + irc.connect{network = network, port = port, nick = nick, username = username, realname = realname, pass = password} diff --git a/misc.lua b/misc.lua new file mode 100644 index 0000000..b3e92fc --- /dev/null +++ b/misc.lua @@ -0,0 +1,29 @@ + +local function pluralize (val, str) + if val > 1 or val == 0 then + return val.." "..str.."s" + else + return val.." "..str + end +end + +function elapsed (datestring) + local pattern = "(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)Z" + local year,month,day,hour,min,sec = datestring:match(pattern) + local start = os.time({year = year, month = month, day = day, hour = hour, min = min, sec = sec, isdst = false }) + local utcnow = os.time(os.date("!*t")) + local diff = utcnow-start + local days = math.floor(diff/86400) + local hours = math.floor(diff/3600 - days*24) + local mins = math.floor(diff/60 - hours*60 - days*24) + local rval = "" + if days > 0 then + rval = pluralize(days, "day").." " + end + if hours > 0 then + rval = rval..pluralize(hours,"hour").." and "..pluralize(mins,"minute") + else + rval = rval..pluralize(mins,"minute") + end + return rval +end diff --git a/plugin/seen.lua b/plugin/seen.lua new file mode 100644 index 0000000..f50f565 --- /dev/null +++ b/plugin/seen.lua @@ -0,0 +1,46 @@ +DB:exec([[CREATE TABLE IF NOT EXISTS seen ( + nick VARCHAR(100) PRIMARY KEY, + channel VARCHAR(100) NOT NULL, + action VARCHAR(20) NOT NULL, + time INTEGER NOT NULL)]]) + +local function update (channel, from, action) + local stmt = DB:prepare("INSERT OR REPLACE INTO seen VALUES (?, ?, ?, ?)") + stmt:bind_values(from, channel, action, os.time()) + stmt:step() + stmt:finalize() +end + +on_join.seen = function(target, from) + update(target, from, "joining") +end + +on_part.seen = function (target, from, msg) + update(target, from, "leaving") +end + +callback.seen = function (target, from, msg) + update(target, from, "talking") +end + +plugin.seen = function (target, from, arg) + if not arg or arg == "" then + irc.say(target, "Give me a name plz!") + return + end + local stmt = DB:prepare("SELECT * FROM seen WHERE NICK = ?") + stmt:bind_values(arg:lower()) + local status = stmt:step() + local result = nil + if status == sqlite3.ROW then result = stmt:get_named_values() end + stmt:finalize() + if status and result then + local c = " " + if result.action == "talking" then + c = " in " + end + irc.say(target, result.nick.." was last seen "..result.action..c..result.channel.." "..elapsed(os.date("!%Y-%m-%dT%H:%M:%SZ",result.time)).." ago.") + else + irc.say(target, "I cannot find anything about "..arg) + end +end diff --git a/plugin/twitch.lua b/plugin/twitch.lua index b741246..1da6fb2 100644 --- a/plugin/twitch.lua +++ b/plugin/twitch.lua @@ -3,6 +3,7 @@ local json = require("json") local https = require("ssl.https") local ltn12 = require("ltn12") +local msc = require("misc") local function fetch_data (endpoint) local response = {} @@ -12,23 +13,6 @@ local function fetch_data (endpoint) return json.decode(table.concat(response)) end -local function elapsed (datestring) - local pattern = "(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)Z" - local year,month,day,hour,min,sec = datestring:match(pattern) - local start = os.time({year = year, month = month, day = day, hour = hour, min = min, sec = sec, isdst = false }) - local utcnow = os.time(os.date("!*t")) - local diff = utcnow-start - local hours = math.floor(diff/3600) - local mins = math.floor(diff/60 - hours*60) - h = hours > 1 and " hours" or " hour" - m = mins > 1 and " minutes" or " minute" - if hours > 0 then - return hours..h.." and "..mins..m - else - return mins..m - end -end - plugin.uptime = function (target, from, arg) local channel = string.sub(target, 2) if arg and arg ~= "" then