A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.8 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 by Ben Kurtovic <ben.kurtovic@verizon.net>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. import threading
  23. import time
  24. from earwigbot.commands import BaseCommand
  25. class Command(BaseCommand):
  26. """Set a message to be repeated to you in a certain amount of time."""
  27. name = "remind"
  28. def check(self, data):
  29. if data.is_command and data.command in ["remind", "reminder"]:
  30. return True
  31. return False
  32. def process(self, data):
  33. if not data.args:
  34. msg = "please specify a time (in seconds) and a message in the following format: !remind <time> <msg>."
  35. self.connection.reply(data, msg)
  36. return
  37. try:
  38. wait = int(data.args[0])
  39. except ValueError:
  40. msg = "the time must be given as an integer, in seconds."
  41. self.connection.reply(data, msg)
  42. return
  43. message = ' '.join(data.args[1:])
  44. if not message:
  45. msg = "what message do you want me to give you when time is up?"
  46. self.connection.reply(data, msg)
  47. return
  48. end = time.localtime(time.time() + wait)
  49. end_time = time.strftime("%b %d %H:%M:%S", end)
  50. end_time_with_timezone = time.strftime("%b %d %H:%M:%S %Z", end)
  51. msg = 'Set reminder for "{0}" in {1} seconds (ends {2}).'
  52. msg = msg.format(message, wait, end_time_with_timezone)
  53. self.connection.reply(data, msg)
  54. t_reminder = threading.Thread(target=self.reminder,
  55. args=(data, message, wait))
  56. t_reminder.name = "reminder " + end_time
  57. t_reminder.daemon = True
  58. t_reminder.start()
  59. def reminder(self, data, message, wait):
  60. time.sleep(wait)
  61. self.connection.reply(data, message)