The KISS Twitch bot
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 

93 lignes
2.9 KiB

  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. print(self.messages_stack)
  55. if not self.messages_stack:
  56. print('Filling the timer messages stack in')
  57. self.messages_stack = self.config.timer.messages.copy()
  58. if self.config.timer.strategy == TimerStrategy.SHUFFLE:
  59. print('Shuffle!')
  60. shuffle(self.messages_stack)
  61. if self.nb_messages_since_timer < self.config.timer.msgs_between or \
  62. datetime.now() < self.last_timer_date + timedelta(minutes=self.config.timer.time_between):
  63. return
  64. message = self.messages_stack.pop(0)
  65. print("Timer: %s" % message)
  66. self.bot.privmsg('#%s' % self.config.channel, message)
  67. self.nb_messages_since_timer = 0
  68. self.last_timer_date = datetime.now()
  69. @irc3.event(irc3.rfc.JOIN)
  70. def on_join(self, mask, channel, **_):
  71. print('JOINED %s as %s' % (channel, mask))