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.

81 lines
3.2 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  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. from platform import python_version
  23. import re
  24. from earwigbot import __version__
  25. from earwigbot.commands import Command
  26. class Help(Command):
  27. """Displays information about the bot."""
  28. name = "help"
  29. commands = ["help", "version"]
  30. def check(self, data):
  31. if data.is_command:
  32. if data.command in self.commands:
  33. return True
  34. if not data.command and data.trigger == data.my_nick:
  35. return True
  36. return False
  37. def process(self, data):
  38. if not data.command:
  39. self.do_hello(data)
  40. elif data.command == "version":
  41. self.do_version(data)
  42. elif data.args:
  43. self.do_command_help(data)
  44. else:
  45. self.do_main_help(data)
  46. def do_main_help(self, data):
  47. """Give the user a general help message with a list of all commands."""
  48. msg = "Hi, I'm a bot! I have {0} commands loaded: {1}. You can get help for any command with '!help <command>'."
  49. cmnds = sorted([cmnd.name for cmnd in self.bot.commands])
  50. msg = msg.format(len(cmnds), ', '.join(cmnds))
  51. self.reply(data, msg)
  52. def do_command_help(self, data):
  53. """Give the user help for a specific command."""
  54. target = data.args[0]
  55. for command in self.bot.commands:
  56. if command.name == target or target in command.commands:
  57. if command.__doc__:
  58. doc = command.__doc__.replace("\n", "")
  59. doc = re.sub(r"\s\s+", " ", doc)
  60. msg = 'Help for command \x0303{0}\x0F: "{1}"'
  61. self.reply(data, msg.format(target, doc))
  62. return
  63. msg = "Sorry, no help for \x0303{0}\x0F.".format(target)
  64. self.reply(data, msg)
  65. def do_hello(self, data):
  66. self.say(data.chan, "Yes, {0}?".format(data.nick))
  67. def do_version(self, data):
  68. vers = "EarwigBot v{bot} on Python {python}: https://github.com/earwig/earwigbot"
  69. self.reply(data, vers.format(bot=__version__, python=python_version()))