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

113 行
4.7 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__ = ["Command"]
  23. class Command(object):
  24. """
  25. **EarwigBot: 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 Command``), can be
  30. subclassed to create custom IRC commands.
  31. This docstring is reported to the user when they type ``"!help
  32. <command>"``.
  33. """
  34. # The command's name, as reported to the user when they use !help:
  35. name = None
  36. # A list of names that will trigger this command. If left empty, it will
  37. # be triggered by the command's name and its name only:
  38. commands = []
  39. # Hooks are "msg", "msg_private", "msg_public", and "join". "msg" is the
  40. # default behavior; if you wish to override that, change the value in your
  41. # command subclass:
  42. hooks = ["msg"]
  43. def __init__(self, bot):
  44. """Constructor for new commands.
  45. This is called once when the command is loaded (from
  46. :py:meth:`commands.load() <earwigbot.managers._ResourceManager.load>`).
  47. *bot* is out base :py:class:`~earwigbot.bot.Bot` object. Don't override
  48. this directly; if you do, remember to place
  49. ``super(Command, self).__init()`` first. Use :py:meth:`setup` for
  50. typical command-init/setup needs.
  51. """
  52. self.bot = bot
  53. self.config = bot.config
  54. self.logger = bot.commands.logger.getChild(self.name)
  55. # Convenience functions:
  56. self.say = lambda target, msg: self.bot.frontend.say(target, msg)
  57. self.reply = lambda data, msg: self.bot.frontend.reply(data, msg)
  58. self.action = lambda target, msg: self.bot.frontend.action(target, msg)
  59. self.notice = lambda target, msg: self.bot.frontend.notice(target, msg)
  60. self.join = lambda chan: self.bot.frontend.join(chan)
  61. self.part = lambda chan, msg=None: self.bot.frontend.part(chan, msg)
  62. self.mode = lambda t, level, msg: self.bot.frontend.mode(t, level, msg)
  63. self.pong = lambda target: self.bot.frontend.pong(target)
  64. self.setup()
  65. def setup(self):
  66. """Hook called immediately after the command is loaded.
  67. Does nothing by default; feel free to override.
  68. """
  69. pass
  70. def check(self, data):
  71. """Return whether this command should be called in response to *data*.
  72. Given a :py:class:`~earwigbot.irc.data.Data` instance, return ``True``
  73. if we should respond to this activity, or ``False`` if we should ignore
  74. it and move on. Be aware that since this is called for each message
  75. sent on IRC, it should be cheap to execute and unlikely to throw
  76. exceptions.
  77. Most commands return ``True`` only if :py:attr:`data.command
  78. <earwigbot.irc.data.Data.command>` ``==`` :py:attr:`self.name <name>`,
  79. or :py:attr:`data.command <earwigbot.irc.data.Data.command>` is in
  80. :py:attr:`self.commands <commands>` if that list is overriden. This is
  81. the default behavior; you should only override it if you wish to change
  82. that.
  83. """
  84. if self.commands:
  85. return data.is_command and data.command in self.commands
  86. return data.is_command and data.command == self.name
  87. def process(self, data):
  88. """Main entry point for doing a command.
  89. Handle an activity (usually a message) on IRC. At this point, thanks
  90. to :py:meth:`check` which is called automatically by the command
  91. handler, we know this is something we should respond to. Place your
  92. command's body here.
  93. """
  94. pass