Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

122 linhas
4.8 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. def setup(self):
  30. try:
  31. self.channels = self.config.commands[self.name]
  32. except KeyError:
  33. self.channels = {}
  34. self.disabled = []
  35. self._throttle = False
  36. self._last_join = 0
  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":
  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. this_join = time()
  53. if this_join - self._last_join < 5:
  54. self._throttle = True
  55. else:
  56. self._throttle = False
  57. self._last_join = this_join
  58. if data.chan in self.disabled:
  59. return
  60. if not data.host.startswith("gateway/web/"):
  61. return
  62. t_id = "welcome-{0}-{1}".format(data.chan.replace("#", ""), data.nick)
  63. thread = Thread(target=self._callback, name=t_id, args=(data,))
  64. thread.daemon = True
  65. thread.start()
  66. def _callback(self, data):
  67. """Internal callback function."""
  68. sleep(2)
  69. if data.chan in self.disabled or self._throttle:
  70. return
  71. self.say(data.chan, self.channels[data.chan].format(nick=data.nick))
  72. def process_command(self, data):
  73. """Handle this when it is an explicit command, not a channel join."""
  74. if data.args:
  75. if not self.config.irc["permissions"].is_admin(data):
  76. msg = "You must be a bot admin to use this command."
  77. self.reply(data, msg)
  78. elif data.arg[0] == "disable":
  79. if len(data.arg) < 2:
  80. self.reply(data, "Which channel should I disable?")
  81. elif data.arg[1] in self.disable:
  82. msg = "Welcoming in \x02{0}\x0F is already disabled."
  83. self.reply(data, msg.format(data.arg[1]))
  84. elif data.arg[1] not in self.channels:
  85. msg = "I'm not welcoming people in \x02{0}\x0F. "
  86. "Only the bot owner can add new channels."
  87. self.reply(data, msg.format(data.arg[1]))
  88. else:
  89. self.disable.append(data.arg[1])
  90. msg = "Disabled welcoming in \x02{0}\x0F."
  91. self.reply(data, msg.format(data.arg[1]))
  92. elif data.arg[0] == "enable":
  93. if len(data.arg) < 2:
  94. self.reply(data, "Which channel should I enable?")
  95. elif data.arg[1] not in self.disable:
  96. msg = "I don't have welcoming disabled in \x02{0}\x0F. "
  97. "Only the bot owner can add new channels."
  98. self.reply(data, msg.format(data.arg[1]))
  99. else:
  100. self.disable.remove(data.arg[1])
  101. msg = "Enabled welcoming in \x02{0}\x0F."
  102. self.reply(data, msg.format(data.arg[1]))
  103. else:
  104. self.reply(data, "I don't understand that command.")
  105. else:
  106. msg = "This command welcomes people who enter certain channels. "
  107. "I am welcoming people in: {0}. A bot admin can disable me "
  108. "with \x0306!welcome disable [channel]\x0F."
  109. self.reply(data, msg.format(", ".join(self.channels.keys())))