The KISS Twitch bot
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

twitchbot.py 2.9 KiB

hace 3 años
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Twason - The KISS Twitch bot
  2. # Copyright (C) 2021 Jérôme Deuchnord
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as
  6. # published by the Free Software Foundation, either version 3 of the
  7. # License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. import irc3
  17. from .config import TimerStrategy
  18. from random import shuffle
  19. from datetime import datetime, timedelta
  20. config = None
  21. @irc3.plugin
  22. class TwitchBot:
  23. def __init__(self, bot):
  24. self.config = config
  25. self.messages_stack = []
  26. self.bot = bot
  27. self.log = self.bot.log
  28. self.last_timer_date = datetime.now()
  29. self.nb_messages_since_timer = 0
  30. def connection_made(self):
  31. print('connected')
  32. def server_ready(self):
  33. print('ready')
  34. def connection_lost(self):
  35. print('connection lost')
  36. @staticmethod
  37. def _parse_variables(in_str: str, mask: str = None):
  38. variables = {
  39. 'author': mask.split('!')[0]
  40. }
  41. for key in variables:
  42. value = variables[key]
  43. in_str = in_str.replace('{%s}' % key, value)
  44. return in_str
  45. @irc3.event(irc3.rfc.PRIVMSG)
  46. def on_msg(self, target, mask, data, **_):
  47. command = self.config.find_command(data.lower().split(' ')[0])
  48. if command is not None:
  49. print('%s: %s%s' % (mask, self.config.command_prefix, command.name))
  50. self.bot.privmsg(target, self._parse_variables(command.message, mask))
  51. self.nb_messages_since_timer += 1
  52. self.play_timer()
  53. def play_timer(self):
  54. if not self.messages_stack:
  55. print('Filling the timer messages stack in')
  56. self.messages_stack = self.config.timer.pool.copy()
  57. if self.config.timer.strategy == TimerStrategy.SHUFFLE:
  58. print('Shuffle!')
  59. shuffle(self.messages_stack)
  60. if self.nb_messages_since_timer < self.config.timer.msgs_between or \
  61. datetime.now() < self.last_timer_date + timedelta(minutes=self.config.timer.time_between):
  62. return
  63. command = self.messages_stack.pop(0)
  64. print("Timer: %s" % command.message)
  65. self.bot.privmsg('#%s' % self.config.channel, command.message)
  66. self.nb_messages_since_timer = 0
  67. self.last_timer_date = datetime.now()
  68. @irc3.event(irc3.rfc.JOIN)
  69. def on_join(self, mask, channel, **_):
  70. print('JOINED %s as %s' % (channel, mask))