add lua module for sending a message on a certain day of the week
This commit is contained in:
parent
4e8e290730
commit
4ad7c33727
43
lua/dailies.lua
Normal file
43
lua/dailies.lua
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user