2016-03-29 11:56:58 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
class Timer(object):
|
2016-04-19 11:21:27 +00:00
|
|
|
def __init__(self, bot, event_name, delay, **kwargs):
|
|
|
|
self.bot = bot
|
|
|
|
self.event_name = event_name
|
2016-03-29 11:56:58 +00:00
|
|
|
self.delay = delay
|
|
|
|
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
|
|
|
|
|
|
|
def set_started_time(self):
|
|
|
|
self.started_time = time.time()
|
|
|
|
|
|
|
|
def due(self):
|
|
|
|
return self.time_left() <= 0
|
|
|
|
|
|
|
|
def time_left(self):
|
|
|
|
return (self.started_time+self.delay)-time.time()
|
|
|
|
|
|
|
|
def call(self):
|
|
|
|
self._done = True
|
2016-03-31 12:20:03 +00:00
|
|
|
self.call_count +=1
|
2016-04-19 11:21:27 +00:00
|
|
|
self.bot.events.on("timer").on(self.event_name).call(
|
|
|
|
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-03-29 11:56:58 +00:00
|
|
|
self.set_started_time()
|
|
|
|
|
|
|
|
def done(self):
|
|
|
|
return self._done
|