The KISS Twitch bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

77 lines
2.3 KiB

  1. import irc3
  2. from .config import get_config, TimerStrategy
  3. from random import shuffle
  4. from datetime import datetime, timedelta
  5. config = None
  6. @irc3.plugin
  7. class TwitchBot:
  8. def __init__(self, bot):
  9. self.config = config
  10. self.messages_stack = []
  11. self.bot = bot
  12. self.log = self.bot.log
  13. self.last_timer_date = datetime.now()
  14. self.nb_messages_since_timer = 0
  15. def connection_made(self):
  16. print('connected')
  17. def server_ready(self):
  18. print('ready')
  19. def connection_lost(self):
  20. print('connection lost')
  21. @staticmethod
  22. def _parse_variables(in_str: str, mask: str = None):
  23. variables = {
  24. 'author': mask.split('!')[0]
  25. }
  26. for key in variables:
  27. value = variables[key]
  28. in_str = in_str.replace('{%s}' % key, value)
  29. return in_str
  30. @irc3.event(irc3.rfc.PRIVMSG)
  31. def on_msg(self, target, mask, data, **_):
  32. print(target)
  33. for command in self.config.commands:
  34. if ('%s ' % data.lower()).startswith('%s%s ' % (self.config.command_prefix, command.name.lower())):
  35. print('%s: %s%s' % (mask, self.config.command_prefix, command.name))
  36. self.bot.privmsg(target, self._parse_variables(command.message, mask))
  37. break
  38. self.nb_messages_since_timer += 1
  39. self.play_timer()
  40. def play_timer(self):
  41. print(self.messages_stack)
  42. if not self.messages_stack:
  43. print('Filling the timer messages stack in')
  44. self.messages_stack = self.config.timer.messages.copy()
  45. if self.config.timer.strategy == TimerStrategy.SHUFFLE:
  46. print('Shuffle!')
  47. shuffle(self.messages_stack)
  48. if self.nb_messages_since_timer < self.config.timer.msgs_between or \
  49. datetime.now() < self.last_timer_date + timedelta(minutes=self.config.timer.time_between):
  50. return
  51. message = self.messages_stack.pop(0)
  52. print("Timer: %s" % message)
  53. self.bot.privmsg('#%s' % self.config.channel, message)
  54. self.nb_messages_since_timer = 0
  55. self.last_timer_date = datetime.now()
  56. @irc3.event(irc3.rfc.JOIN)
  57. def on_join(self, mask, channel, **_):
  58. print('JOINED %s as %s' % (channel, mask))