A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
3.1 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. import re
  23. from earwigbot.commands import BaseCommand
  24. from earwigbot.irc import Data
  25. class Command(BaseCommand):
  26. """Displays help information."""
  27. name = "help"
  28. def check(self, data):
  29. if data.is_command:
  30. if data.command == "help":
  31. return True
  32. if not data.command and data.trigger == data.my_nick:
  33. return True
  34. return False
  35. def process(self, data):
  36. if not data.command:
  37. self.do_hello(data)
  38. elif data.args:
  39. self.do_command_help(data)
  40. else:
  41. self.do_main_help(data)
  42. def do_main_help(self, data):
  43. """Give the user a general help message with a list of all commands."""
  44. msg = "Hi, I'm a bot! I have {0} commands loaded: {1}. You can get help for any command with '!help <command>'."
  45. cmnds = sorted(self.bot.commands)
  46. msg = msg.format(len(cmnds), ', '.join(cmnds))
  47. self.reply(data, msg)
  48. def do_command_help(self, data):
  49. """Give the user help for a specific command."""
  50. command = data.args[0]
  51. # Create a dummy message to test which commands pick up the user's
  52. # input:
  53. msg = ":foo!bar@example.com PRIVMSG #channel :msg".split()
  54. dummy = Data(self.bot, msg)
  55. dummy.command = command.lower()
  56. dummy.is_command = True
  57. for cmnd_name in self.bot.commands:
  58. cmnd = self.bot.commands.get(cmnd_name)
  59. if not cmnd.check(dummy):
  60. continue
  61. if cmnd.__doc__:
  62. doc = cmnd.__doc__.replace("\n", "")
  63. doc = re.sub("\s\s+", " ", doc)
  64. msg = "help for command \x0303{0}\x0301: \"{1}\""
  65. self.reply(data, msg.format(command, doc))
  66. return
  67. break
  68. msg = "sorry, no help for \x0303{0}\x0301.".format(command)
  69. self.reply(data, msg)
  70. def do_hello(self, data):
  71. self.say(data.chan, "Yes, {0}?".format(data.nick))