2016-04-19 12:25:50 +00:00
|
|
|
import time, uuid
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
class Timer(object):
|
2018-08-31 11:55:52 +00:00
|
|
|
def __init__(self, id, bot, events, event_name, delay,
|
|
|
|
next_due=None, **kwargs):
|
|
|
|
self.id = id
|
2016-04-19 11:21:27 +00:00
|
|
|
self.bot = bot
|
2018-08-31 11:55:52 +00:00
|
|
|
self.events = events
|
2016-04-19 11:21:27 +00:00
|
|
|
self.event_name = event_name
|
2016-03-29 11:56:58 +00:00
|
|
|
self.delay = delay
|
2016-04-19 12:25:50 +00:00
|
|
|
if next_due:
|
|
|
|
self.next_due = next_due
|
|
|
|
else:
|
|
|
|
self.set_next_due()
|
2016-03-29 11:56:58 +00:00
|
|
|
self.kwargs = kwargs
|
|
|
|
self._done = False
|
2016-03-31 12:20:03 +00:00
|
|
|
self.call_count = 0
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2016-04-19 12:25:50 +00:00
|
|
|
def set_next_due(self):
|
|
|
|
self.next_due = time.time()+self.delay
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
def due(self):
|
|
|
|
return self.time_left() <= 0
|
|
|
|
|
|
|
|
def time_left(self):
|
2016-04-19 12:25:50 +00:00
|
|
|
return self.next_due-time.time()
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
def call(self):
|
|
|
|
self._done = True
|
2016-03-31 12:20:03 +00:00
|
|
|
self.call_count +=1
|
2018-08-31 11:55:52 +00:00
|
|
|
self.events.on("timer").on(self.event_name).call(
|
2016-04-19 11:21:27 +00:00
|
|
|
timer=self, **self.kwargs)
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
def redo(self):
|
2016-04-03 12:20:05 +00:00
|
|
|
self._done = False
|
2016-04-19 12:25:50 +00:00
|
|
|
self.set_next_due()
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
def done(self):
|
|
|
|
return self._done
|