A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

remind.py 1.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # -*- coding: utf-8 -*-
  2. import threading
  3. import time
  4. from classes import BaseCommand
  5. class Command(BaseCommand):
  6. """Set a message to be repeated to you in a certain amount of time."""
  7. name = "remind"
  8. def check(self, data):
  9. if data.is_command and data.command in ["remind", "reminder"]:
  10. return True
  11. return False
  12. def process(self, data):
  13. if not data.args:
  14. msg = "please specify a time (in seconds) and a message in the following format: !remind <time> <msg>."
  15. self.connection.reply(data, msg)
  16. return
  17. try:
  18. wait = int(data.args[0])
  19. except ValueError:
  20. msg = "the time must be given as an integer, in seconds."
  21. self.connection.reply(data, msg)
  22. return
  23. message = ' '.join(data.args[1:])
  24. if not message:
  25. msg = "what message do you want me to give you when time is up?"
  26. self.connection.reply(data, msg)
  27. return
  28. end = time.localtime(time.time() + wait)
  29. end_time = time.strftime("%b %d %H:%M:%S", end)
  30. end_time_with_timezone = time.strftime("%b %d %H:%M:%S %Z", end)
  31. msg = 'Set reminder for "{0}" in {1} seconds (ends {2}).'
  32. msg = msg.format(message, wait, end_time_with_timezone)
  33. self.connection.reply(data, msg)
  34. t_reminder = threading.Thread(target=self.reminder,
  35. args=(data, message, wait))
  36. t_reminder.name = "reminder " + end_time
  37. t_reminder.daemon = True
  38. t_reminder.start()
  39. def reminder(self, data, message, wait):
  40. time.sleep(wait)
  41. self.connection.reply(data, message)