A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

76 строки
3.1 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009, 2010, 2011 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 logging
  23. __all__ = ["BaseCommand"]
  24. class BaseCommand(object):
  25. """A base class for commands on IRC.
  26. This docstring is reported to the user when they use !help <command>.
  27. """
  28. # This is the command's name, as reported to the user when they use !help:
  29. name = None
  30. # Hooks are "msg", "msg_private", "msg_public", and "join". "msg" is the
  31. # default behavior; if you wish to override that, change the value in your
  32. # command subclass:
  33. hooks = ["msg"]
  34. def __init__(self, connection):
  35. """Constructor for new commands.
  36. This is called once when the command is loaded (from
  37. commands._load_command()). `connection` is a Connection object,
  38. allowing us to do self.connection.say(), self.connection.send(), etc,
  39. from within a method.
  40. """
  41. self.connection = connection
  42. logger_name = ".".join(("earwigbot", "commands", self.name))
  43. self.logger = logging.getLogger(logger_name)
  44. self.logger.setLevel(logging.DEBUG)
  45. def check(self, data):
  46. """Returns whether this command should be called in response to 'data'.
  47. Given a Data() instance, return True if we should respond to this
  48. activity, or False if we should ignore it or it doesn't apply to us.
  49. Most commands return True if data.command == self.name, otherwise they
  50. return False. This is the default behavior of check(); you need only
  51. override it if you wish to change that.
  52. """
  53. if data.is_command and data.command == self.name:
  54. return True
  55. return False
  56. def process(self, data):
  57. """Main entry point for doing a command.
  58. Handle an activity (usually a message) on IRC. At this point, thanks
  59. to self.check() which is called automatically by the command handler,
  60. we know this is something we should respond to, so (usually) something
  61. like 'if data.command != "command_name": return' is unnecessary.
  62. """
  63. pass