A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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