From 4ad7c337275015b7025d7d05efadb72a005b7e92 Mon Sep 17 00:00:00 2001 From: Adrian Malacoda Date: Mon, 26 Feb 2018 23:34:53 -0600 Subject: [PATCH] add lua module for sending a message on a certain day of the week --- lua/dailies.lua | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 lua/dailies.lua diff --git a/lua/dailies.lua b/lua/dailies.lua new file mode 100644 index 0000000..6a593a1 --- /dev/null +++ b/lua/dailies.lua @@ -0,0 +1,43 @@ +target_time = {hour=hour, min=minute, sec=second} + +function get_total_day_seconds (time_table) + return (((time_table.hour * 60) + (time_table.min)) * 60) + time_table.sec +end + +SECONDS_PER_DAY = get_total_day_seconds({hour=24, min=0, sec=0}) + +function get_sleep_duration_sec () + current_time = os.time() + current_time_table = os.date("*t", current_time) + current_day_seconds = get_total_day_seconds(current_time_table) + target_day_seconds = get_total_day_seconds(target_time) + difference = target_day_seconds - current_day_seconds + if difference > 0 then + return difference + else + return SECONDS_PER_DAY + difference + end +end + +function get_weekday () + return os.date("%a", current_time) +end + +function sleep (sec) + return os.execute("sleep " .. tonumber(sec)) +end + +while true do + sleep_duration = get_sleep_duration_sec() + print("sleep for " .. sleep_duration) + + if not sleep(sleep_duration) then + print("sleep exited abnormally - break") + break + end + + if get_weekday() == weekday then + print("send message " .. message) + sender:send({type = "message", channel = channel, message = message}) + end +end