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", os.time()) end function sleep (sec) return os.execute("sleep " .. tonumber(sec)) end function run_dailies (dailies) 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 time_table = os.date("*t", os.time()) time_table.weekday = get_weekday() for i, fn in ipairs(dailies) do message = fn(time_table) if message then print("send message " .. message) sender:send({type = "message", channel = channel, message = message}) break end end end end