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

help.py 1.8 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. # Generates help information.
  3. from irc.base_command import BaseCommand
  4. from irc.data import Data
  5. from irc import command_handler
  6. class Help(BaseCommand):
  7. def get_hooks(self):
  8. return ["msg"]
  9. def get_help(self, command):
  10. return "Generates help information."
  11. def check(self, data):
  12. if data.is_command and data.command == "help":
  13. return True
  14. return False
  15. def process(self, data):
  16. if not data.args:
  17. self.do_general_help(data)
  18. else:
  19. if data.args[0] == "list":
  20. self.do_list_help(data)
  21. else:
  22. self.do_command_help(data)
  23. def do_general_help(self, data):
  24. self.connection.reply(data, "I am a bot! You can get help for any command with '!help <command>', or a list of all loaded modules with '!help list'.")
  25. def do_list_help(self, data):
  26. commands = command_handler.get_commands()
  27. cmnds = map(lambda c: c.__class__.__name__, commands)
  28. pretty_cmnds = ', '.join(cmnds)
  29. self.connection.reply(data, "%s command classes loaded: %s." % (len(cmnds), pretty_cmnds))
  30. def do_command_help(self, data):
  31. command = data.args[0]
  32. commands = command_handler.get_commands()
  33. dummy = Data() # dummy message to test which command classes pick up this command
  34. dummy.command = command.lower() # lowercase command name
  35. dummy.is_command = True
  36. for cmnd in commands:
  37. if cmnd.check(dummy):
  38. help = cmnd.get_help(command)
  39. break
  40. try:
  41. self.connection.reply(data, "info for command \x0303%s\x0301: \"%s\"" % (command, help))
  42. except UnboundLocalError:
  43. self.connection.reply(data, "sorry, no help for \x0303%s\x0301." % command)