Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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