bitbot-3.11-fork/Timer.py

40 lines
957 B
Python
Raw Normal View History

import time, uuid
2016-03-29 11:56:58 +00:00
class Timer(object):
def __init__(self, id, bot, events, event_name, delay,
next_due=None, **kwargs):
self.id = id
self.bot = bot
self.events = events
self.event_name = event_name
2016-03-29 11:56:58 +00:00
self.delay = delay
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
self.call_count = 0
2016-03-29 11:56:58 +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):
return self.next_due-time.time()
2016-03-29 11:56:58 +00:00
def call(self):
self._done = True
self.call_count +=1
self.events.on("timer").on(self.event_name).call(
timer=self, **self.kwargs)
2016-03-29 11:56:58 +00:00
def redo(self):
self._done = False
self.set_next_due()
2016-03-29 11:56:58 +00:00
def done(self):
return self._done