A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

base_command.py 1.9 KiB

13 anni fa
13 anni fa
13 anni fa
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # -*- coding: utf-8 -*-
  2. import logging
  3. class BaseCommand(object):
  4. """A base class for commands on IRC.
  5. This docstring is reported to the user when they use !help <command>.
  6. """
  7. # This is the command's name, as reported to the user when they use !help:
  8. name = "base_command"
  9. # Hooks are "msg", "msg_private", "msg_public", and "join". "msg" is the
  10. # default behavior; if you wish to override that, change the value in your
  11. # command subclass:
  12. hooks = ["msg"]
  13. def __init__(self, connection):
  14. """Constructor for new commands.
  15. This is called once when the command is loaded (from
  16. commands._load_command()). `connection` is a Connection object,
  17. allowing us to do self.connection.say(), self.connection.send(), etc,
  18. from within a method.
  19. """
  20. self.connection = connection
  21. self.logger = logging.getLogger(".".join(("commands", name)))
  22. self.logger.setLevel(logging.DEBUG)
  23. def check(self, data):
  24. """Returns whether this command should be called in response to 'data'.
  25. Given a Data() instance, return True if we should respond to this
  26. activity, or False if we should ignore it or it doesn't apply to us.
  27. Most commands return True if data.command == self.name, otherwise they
  28. return False. This is the default behavior of check(); you need only
  29. override it if you wish to change that.
  30. """
  31. if data.is_command and data.command == self.name:
  32. return True
  33. return False
  34. def process(self, data):
  35. """Main entry point for doing a command.
  36. Handle an activity (usually a message) on IRC. At this point, thanks
  37. to self.check() which is called automatically by the command handler,
  38. we know this is something we should respond to, so (usually) something
  39. like 'if data.command != "command_name": return' is unnecessary.
  40. """
  41. pass