Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

139 行
5.5 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", "part", "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. self._pending = []
  39. def check(self, data):
  40. if data.is_command and data.command in self.commands:
  41. return True
  42. try:
  43. if data.line[1] != "JOIN" and data.line[1] != "PART":
  44. return False
  45. except IndexError:
  46. pass
  47. if data.chan in self.channels:
  48. return True
  49. return False
  50. def process(self, data):
  51. if data.is_command:
  52. self.process_command(data)
  53. return
  54. if data.line[1] == "PART":
  55. if (data.chan, data.nick) in self._pending:
  56. self._pending.remove((data.chan, data.nick))
  57. return
  58. this_join = time()
  59. if this_join - self._last_join < 3:
  60. self._throttle = True
  61. else:
  62. self._throttle = False
  63. self._last_join = this_join
  64. if data.chan in self.disabled:
  65. return
  66. if not data.host.startswith("gateway/web/"):
  67. return
  68. t_id = "welcome-{0}-{1}".format(data.chan.replace("#", ""), data.nick)
  69. thread = Thread(target=self._callback, name=t_id, args=(data,))
  70. thread.daemon = True
  71. thread.start()
  72. def _callback(self, data):
  73. """Internal callback function."""
  74. self._pending.append((data.chan, data.nick))
  75. sleep(1.5)
  76. if data.chan in self.disabled or self._throttle:
  77. return
  78. if (data.chan, data.nick) not in self._pending:
  79. return
  80. self.say(data.chan, self.channels[data.chan].format(nick=data.nick))
  81. try:
  82. self._pending.remove((data.chan, data.nick))
  83. except ValueError:
  84. pass # Could be a race condition
  85. def process_command(self, data):
  86. """Handle this when it is an explicit command, not a channel join."""
  87. if data.args:
  88. if not self.config.irc["permissions"].is_admin(data):
  89. msg = "You must be a bot admin to use this command."
  90. self.reply(data, msg)
  91. elif data.args[0] == "disable":
  92. if len(data.args) < 2:
  93. self.reply(data, "Which channel should I disable?")
  94. elif data.args[1] in self.disabled:
  95. msg = "Welcoming in \x02{0}\x0F is already disabled."
  96. self.reply(data, msg.format(data.args[1]))
  97. elif data.args[1] not in self.channels:
  98. msg = ("I'm not welcoming people in \x02{0}\x0F. "
  99. "Only the bot owner can add new channels.")
  100. self.reply(data, msg.format(data.args[1]))
  101. else:
  102. self.disabled.append(data.args[1])
  103. msg = ("Disabled welcoming in \x02{0}\x0F. Re-enable with "
  104. "\x0306!welcome enable {0}\x0F.")
  105. self.reply(data, msg.format(data.args[1]))
  106. elif data.args[0] == "enable":
  107. if len(data.args) < 2:
  108. self.reply(data, "Which channel should I enable?")
  109. elif data.args[1] not in self.disabled:
  110. msg = ("I don't have welcoming disabled in \x02{0}\x0F. "
  111. "Only the bot owner can add new channels.")
  112. self.reply(data, msg.format(data.args[1]))
  113. else:
  114. self.disabled.remove(data.args[1])
  115. msg = "Enabled welcoming in \x02{0}\x0F."
  116. self.reply(data, msg.format(data.args[1]))
  117. else:
  118. self.reply(data, "I don't understand that command.")
  119. else:
  120. msg = ("This command welcomes people who enter certain channels. "
  121. "I am welcoming people in: {0}. A bot admin can disable me "
  122. "with \x0306!welcome disable [channel]\x0F.")
  123. self.reply(data, msg.format(", ".join(self.channels.keys())))