Skip to content

Commit

Permalink
remind plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidVentura committed May 31, 2015
1 parent d27c6d5 commit 891047b
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions plugins/remind.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
local filename='data/remind.lua'
local cronned = load_from_file(filename)

local function save_cron(msg, text,date)
local origin = get_receiver(msg)
if not cronned[date] then
cronned[date] = {}
end
local arr = { origin, text } ;
table.insert(cronned[date], arr)
serialize_to_file(cronned, filename)
return 'Saved!'
end

local function delete_cron(date)
for k,v in pairs(cronned) do
if k == date then
cronned[k]=nil
end
end
serialize_to_file(cronned, filename)
end

local function cron()
for date, values in pairs(cronned) do
if date < os.time() then --time's up
send_msg(values[1][1], "Time's up:"..values[1][2], ok_cb, false)
delete_cron(date) --TODO: Maybe check for something else? Like user
end

end
end

local function actually_run(msg, delay,text)
if (not delay or not text) then
return "Usage: !remind [delay: 2h3m1s] text"
end
save_cron(msg, text,delay)
return "I'll remind you on " .. os.date("%x at %H:%M:%S",delay) .. " about '" .. text .. "'"
end

local function run(msg, matches)
local sum = 0
for i = 1, #matches-1 do
local b,_ = string.gsub(matches[i],"[a-zA-Z]","")
if string.find(matches[i], "s") then
sum=sum+b
end
if string.find(matches[i], "m") then
sum=sum+b*60
end
if string.find(matches[i], "h") then
sum=sum+b*3600
end
end

local date=sum+os.time()
local text = matches[#matches]

local text = actually_run(msg, date, text)
return text
end

return {
description = "remind plugin",
usage = {
"!remind [delay: 2hms] text",
"!remind [delay: 2h3m] text",
"!remind [delay: 2h3m1s] text"
},
patterns = {
"^!remind ([0-9]+[hmsdHMSD]) (.+)$",
"^!remind ([0-9]+[hmsdHMSD])([0-9]+[hmsdHMSD]) (.+)$",
"^!remind ([0-9]+[hmsdHMSD])([0-9]+[hmsdHMSD])([0-9]+[hmsdHMSD]) (.+)$"
},
run = run,
cron = cron
}

0 comments on commit 891047b

Please sign in to comment.