Additional IRC commands and bot tasks for EarwigBot 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.

124 lines
4.9 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2014 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 threading import Thread
  23. from time import sleep, time
  24. from earwigbot.commands import Command
  25. class Welcome(Command):
  26. """Welcome people who enter certain channels."""
  27. name = "welcome"
  28. commands = ["welcome", "greet"]
  29. hooks = ["join", "msg"]
  30. def setup(self):
  31. try:
  32. self.channels = self.config.commands[self.name]
  33. except KeyError:
  34. self.channels = {}
  35. self.disabled = []
  36. self._throttle = False
  37. self._last_join = 0
  38. def check(self, data):
  39. if data.is_command and data.command in self.commands:
  40. return True
  41. try:
  42. if data.line[1] != "JOIN":
  43. return False
  44. except IndexError:
  45. pass
  46. if data.chan in self.channels:
  47. return True
  48. return False
  49. def process(self, data):
  50. if data.is_command:
  51. self.process_command(data)
  52. return
  53. this_join = time()
  54. if this_join - self._last_join < 5:
  55. self._throttle = True
  56. else:
  57. self._throttle = False
  58. self._last_join = this_join
  59. if data.chan in self.disabled:
  60. return
  61. if not data.host.startswith("gateway/web/"):
  62. return
  63. t_id = "welcome-{0}-{1}".format(data.chan.replace("#", ""), data.nick)
  64. thread = Thread(target=self._callback, name=t_id, args=(data,))
  65. thread.daemon = True
  66. thread.start()
  67. def _callback(self, data):
  68. """Internal callback function."""
  69. sleep(2)
  70. if data.chan in self.disabled or self._throttle:
  71. return
  72. self.say(data.chan, self.channels[data.chan].format(nick=data.nick))
  73. def process_command(self, data):
  74. """Handle this when it is an explicit command, not a channel join."""
  75. if data.args:
  76. if not self.config.irc["permissions"].is_admin(data):
  77. msg = "You must be a bot admin to use this command."
  78. self.reply(data, msg)
  79. elif data.arg[0] == "disable":
  80. if len(data.arg) < 2:
  81. self.reply(data, "Which channel should I disable?")
  82. elif data.arg[1] in self.disabled:
  83. msg = "Welcoming in \x02{0}\x0F is already disabled."
  84. self.reply(data, msg.format(data.arg[1]))
  85. elif data.arg[1] not in self.channels:
  86. msg = ("I'm not welcoming people in \x02{0}\x0F. "
  87. "Only the bot owner can add new channels.")
  88. self.reply(data, msg.format(data.arg[1]))
  89. else:
  90. self.disabled.append(data.arg[1])
  91. msg = ("Disabled welcoming in \x02{0}\x0F. Re-enable with "
  92. "\x0306!welcome enable {0}\x0F.")
  93. self.reply(data, msg.format(data.arg[1]))
  94. elif data.arg[0] == "enable":
  95. if len(data.arg) < 2:
  96. self.reply(data, "Which channel should I enable?")
  97. elif data.arg[1] not in self.disabled:
  98. msg = ("I don't have welcoming disabled in \x02{0}\x0F. "
  99. "Only the bot owner can add new channels.")
  100. self.reply(data, msg.format(data.arg[1]))
  101. else:
  102. self.disabled.remove(data.arg[1])
  103. msg = "Enabled welcoming in \x02{0}\x0F."
  104. self.reply(data, msg.format(data.arg[1]))
  105. else:
  106. self.reply(data, "I don't understand that command.")
  107. else:
  108. msg = ("This command welcomes people who enter certain channels. "
  109. "I am welcoming people in: {0}. A bot admin can disable me "
  110. "with \x0306!welcome disable [channel]\x0F.")
  111. self.reply(data, msg.format(", ".join(self.channels.keys())))