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.

50 righe
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 = "I am a bot! I have {0} commands loaded: {1}. You can get help for any command with '!help <command>'."
  17. cmnds = self.cmnds.keys()
  18. cmnds.sort()
  19. msg = msg.format(len(cmnds), ', '.join(cmnds))
  20. self.connection.reply(data, msg)
  21. def do_command_help(self, data):
  22. """Give the user help for a specific command."""
  23. command = data.args[0]
  24. # Create a dummy message to test which commands pick up the user's
  25. # input:
  26. dummy = Data("PRIVMSG #fake-channel :Fake messsage!")
  27. dummy.command = command.lower()
  28. dummy.is_command = True
  29. for cmnd in self.cmnds.values():
  30. if not cmnd.check(dummy):
  31. continue
  32. if cmnd.__doc__:
  33. doc = cmnd.__doc__.replace("\n", "")
  34. doc = re.sub("\s\s+", " ", doc)
  35. msg = "info for command \x0303{0}\x0301: \"{1}\""
  36. self.connection.reply(data, msg.format(command, doc))
  37. return
  38. break
  39. msg = "sorry, no help for \x0303{0}\x0301.".format(command)
  40. self.connection.reply(data, msg)