A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
4.2 KiB

  1. # Copyright (C) 2009-2021 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. # SOFTWARE.
  20. from earwigbot.commands import Command
  21. class ChanOps(Command):
  22. """Voice, devoice, op, or deop users in the channel, or join or part from
  23. other channels."""
  24. name = "chanops"
  25. commands = [
  26. "chanops",
  27. "voice",
  28. "devoice",
  29. "op",
  30. "deop",
  31. "join",
  32. "part",
  33. "listchans",
  34. ]
  35. def process(self, data):
  36. if data.command == "chanops":
  37. msg = "Available commands are {0}."
  38. self.reply(
  39. data,
  40. msg.format(
  41. ", ".join("!" + cmd for cmd in self.commands if cmd != data.command)
  42. ),
  43. )
  44. return
  45. de_escalate = data.command in ["devoice", "deop"]
  46. if de_escalate and (not data.args or data.args[0] == data.nick):
  47. target = data.nick
  48. elif not self.config.irc["permissions"].is_admin(data):
  49. self.reply(data, "You must be a bot admin to use this command.")
  50. return
  51. if data.command == "join":
  52. self.do_join(data)
  53. elif data.command == "part":
  54. self.do_part(data)
  55. elif data.command == "listchans":
  56. self.do_list(data)
  57. else:
  58. # If it is just !op/!devoice/whatever without arguments, assume
  59. # they want to do this to themselves:
  60. if not data.args:
  61. target = data.nick
  62. else:
  63. target = data.args[0]
  64. command = data.command.upper()
  65. self.say("ChanServ", " ".join((command, data.chan, target)))
  66. log = "{0} requested {1} on {2} in {3}"
  67. self.logger.info(log.format(data.nick, command, target, data.chan))
  68. def do_join(self, data):
  69. if data.args:
  70. channel = data.args[0]
  71. if not channel.startswith("#"):
  72. channel = "#" + channel
  73. else:
  74. msg = "You must specify a channel to join or part from."
  75. self.reply(data, msg)
  76. return
  77. self.join(channel)
  78. log = f"{data.nick} requested JOIN to {channel}"
  79. self.logger.info(log)
  80. def do_part(self, data):
  81. channel = data.chan
  82. reason = None
  83. if data.args:
  84. if data.args[0].startswith("#"):
  85. # "!part #channel reason for parting"
  86. channel = data.args[0]
  87. if data.args[1:]:
  88. reason = " ".join(data.args[1:])
  89. else: # "!part reason for parting"; assume current channel
  90. reason = " ".join(data.args)
  91. msg = f"Requested by {data.nick}"
  92. log = f"{data.nick} requested PART from {channel}"
  93. if reason:
  94. msg += f": {reason}"
  95. log += f' ("{reason}")'
  96. self.part(channel, msg)
  97. self.logger.info(log)
  98. def do_list(self, data):
  99. chans = self.bot.frontend.channels
  100. if not chans:
  101. self.reply(data, "I am currently in no channels.")
  102. return
  103. self.reply(
  104. data,
  105. "I am currently in \x02{}\x0f channel{}: {}.".format(
  106. len(chans), "" if len(chans) == 1 else "s", ", ".join(sorted(chans))
  107. ),
  108. )