A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

108 行
4.6 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. __all__ = ["BaseCommand"]
  23. class BaseCommand(object):
  24. """
  25. EarwigBot's Base IRC Command
  26. This package provides built-in IRC "commands" used by the bot's front-end
  27. component. Additional commands can be installed as plugins in the bot's
  28. working directory.
  29. This class (import with `from earwigbot.commands import BaseCommand`),
  30. can be subclassed to create custom IRC commands.
  31. This docstring is reported to the user when they use !help <command>.
  32. """
  33. # This is the command's name, as reported to the user when they use !help:
  34. name = None
  35. # Hooks are "msg", "msg_private", "msg_public", and "join". "msg" is the
  36. # default behavior; if you wish to override that, change the value in your
  37. # command subclass:
  38. hooks = ["msg"]
  39. def __init__(self, bot):
  40. """Constructor for new commands.
  41. This is called once when the command is loaded (from
  42. commands._load_command()). `bot` is out base Bot object. Generally you
  43. shouldn't need to override this; if you do, call
  44. super(Command, self).__init__() first.
  45. """
  46. self.bot = bot
  47. self.config = bot.config
  48. self.logger = bot.commands.logger.getChild(self.name)
  49. # Convenience functions:
  50. self.say = lambda target, msg: self.bot.frontend.say(target, msg)
  51. self.reply = lambda data, msg: self.bot.frontend.reply(data, msg)
  52. self.action = lambda target, msg: self.bot.frontend.action(target, msg)
  53. self.notice = lambda target, msg: self.bot.frontend.notice(target, msg)
  54. self.join = lambda chan: self.bot.frontend.join(chan)
  55. self.part = lambda chan, msg=None: self.bot.frontend.part(chan, msg)
  56. self.mode = lambda t, level, msg: self.bot.frontend.mode(t, level, msg)
  57. self.pong = lambda target: self.bot.frontend.pong(target)
  58. def _wrap_check(self, data):
  59. """Check whether this command should be called, catching errors."""
  60. try:
  61. return self.check(data)
  62. except Exception:
  63. e = "Error checking command '{0}' with data: {1}:"
  64. self.logger.exception(e.format(self.name, data))
  65. def _wrap_process(self, data):
  66. """process() the message, catching and reporting any errors."""
  67. try:
  68. self.process(data)
  69. except Exception:
  70. e = "Error executing command '{0}':"
  71. self.logger.exception(e.format(data.command))
  72. def check(self, data):
  73. """Return whether this command should be called in response to 'data'.
  74. Given a Data() instance, return True if we should respond to this
  75. activity, or False if we should ignore it or it doesn't apply to us.
  76. Be aware that since this is called for each message sent on IRC, it
  77. should not be cheap to execute and unlikely to throw exceptions.
  78. Most commands return True if data.command == self.name, otherwise they
  79. return False. This is the default behavior of check(); you need only
  80. override it if you wish to change that.
  81. """
  82. return data.is_command and data.command == self.name
  83. def process(self, data):
  84. """Main entry point for doing a command.
  85. Handle an activity (usually a message) on IRC. At this point, thanks
  86. to self.check() which is called automatically by the command handler,
  87. we know this is something we should respond to, so something like
  88. `if data.command != "command_name": return` is usually unnecessary.
  89. Note that
  90. """
  91. pass