A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot

49 рядки
1.6 KiB

  1. # -*- coding: utf-8 -*-
  2. import re
  3. from classes import BaseCommand, Data
  4. import commands
  5. class Command(BaseCommand):
  6. """Displays help information."""
  7. name = "help"
  8. def process(self, data):
  9. self.cmnds = commands.get_all()
  10. if not data.args:
  11. self.do_main_help(data)
  12. else:
  13. self.do_command_help(data)
  14. def do_main_help(self, data):
  15. """Give the user a general help message with a list of all commands."""
  16. msg = "Hi, I'm a bot! I have {0} commands loaded: {1}. You can get help for any command with '!help <command>'."
  17. cmnds = sorted(self.cmnds.keys())
  18. msg = msg.format(len(cmnds), ', '.join(cmnds))
  19. self.connection.reply(data, msg)
  20. def do_command_help(self, data):
  21. """Give the user help for a specific command."""
  22. command = data.args[0]
  23. # Create a dummy message to test which commands pick up the user's
  24. # input:
  25. dummy = Data(":foo!bar@example.com PRIVMSG #channel :msg!".split())
  26. dummy.command = command.lower()
  27. dummy.is_command = True
  28. for cmnd in self.cmnds.values():
  29. if not cmnd.check(dummy):
  30. continue
  31. if cmnd.__doc__:
  32. doc = cmnd.__doc__.replace("\n", "")
  33. doc = re.sub("\s\s+", " ", doc)
  34. msg = "info for command \x0303{0}\x0301: \"{1}\""
  35. self.connection.reply(data, msg.format(command, doc))
  36. return
  37. break
  38. msg = "sorry, no help for \x0303{0}\x0301.".format(command)
  39. self.connection.reply(data, msg)