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.
 
 

94 regels
3.0 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 get_config, 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. print(target)
  48. for command in self.config.commands:
  49. if ('%s ' % data.lower()).startswith('%s%s ' % (self.config.command_prefix, command.name.lower())):
  50. print('%s: %s%s' % (mask, self.config.command_prefix, command.name))
  51. self.bot.privmsg(target, self._parse_variables(command.message, mask))
  52. break
  53. self.nb_messages_since_timer += 1
  54. self.play_timer()
  55. def play_timer(self):
  56. print(self.messages_stack)
  57. if not self.messages_stack:
  58. print('Filling the timer messages stack in')
  59. self.messages_stack = self.config.timer.messages.copy()
  60. if self.config.timer.strategy == TimerStrategy.SHUFFLE:
  61. print('Shuffle!')
  62. shuffle(self.messages_stack)
  63. if self.nb_messages_since_timer < self.config.timer.msgs_between or \
  64. datetime.now() < self.last_timer_date + timedelta(minutes=self.config.timer.time_between):
  65. return
  66. message = self.messages_stack.pop(0)
  67. print("Timer: %s" % message)
  68. self.bot.privmsg('#%s' % self.config.channel, message)
  69. self.nb_messages_since_timer = 0
  70. self.last_timer_date = datetime.now()
  71. @irc3.event(irc3.rfc.JOIN)
  72. def on_join(self, mask, channel, **_):
  73. print('JOINED %s as %s' % (channel, mask))