uptime.py 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. import time
  2. from datetime import datetime, timedelta
  3. from event import Event
  4. try:
  5. from basemodule import BaseModule
  6. except ImportError:
  7. from modules.basemodule import BaseModule
  8. class Uptime(BaseModule):
  9. def post_init(self):
  10. uptime_event = Event("__.uptime__")
  11. uptime_event.define(msg_definition="^\.uptime")
  12. uptime_event.subscribe(self)
  13. # register ourself to our new custom event
  14. self.bot.register_event(uptime_event, self)
  15. starttime = time.time()
  16. localtime = time.localtime()
  17. if 'uptime' not in self.bot.mem_store:
  18. self.bot.mem_store['uptime'] = dict()
  19. self.bot.mem_store['uptime']['localtime'] = localtime
  20. self.bot.mem_store['uptime']['starttime'] = starttime
  21. def handle(self, event):
  22. self._uptime(event.channel)
  23. def _uptime(self, channel):
  24. #print timedelta(seconds=time.time() - self.starttime)
  25. self.say(channel,"I've been up " +str(timedelta(seconds=time.time() - self.bot.mem_store['uptime']['starttime'])).split(".")[0] + ", since "+time.strftime("%a, %d %b %Y %H:%M:%S -0800", self.bot.mem_store['uptime']['localtime']))