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.
 
 

74 lines
2.1 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. from abc import ABC, abstractmethod
  17. from enum import Enum
  18. from typing import Union
  19. class ModerationDecision(Enum):
  20. ABSTAIN = -1
  21. DELETE_MSG = 0
  22. TIMEOUT_USER = 1
  23. class Moderator(ABC):
  24. message: str
  25. decision: ModerationDecision
  26. def __init__(self, message: str, timeout_duration: Union[None, int]):
  27. self.message = message
  28. self.timeout_duration = timeout_duration
  29. @abstractmethod
  30. def get_name(self) -> str:
  31. pass
  32. @abstractmethod
  33. def vote(self, msg) -> ModerationDecision:
  34. pass
  35. class CapsLockModerator(Moderator):
  36. def __init__(self, message: str, min_size: int, threshold: int, decision: ModerationDecision, timeout_duration: Union[None, int]):
  37. super().__init__(message, timeout_duration)
  38. self.min_size = min_size
  39. self.threshold = threshold / 100
  40. self.decision = decision
  41. def get_name(self) -> str:
  42. return 'Caps Lock'
  43. def vote(self, msg: str) -> ModerationDecision:
  44. msg = ''.join(filter(str.isalpha, msg))
  45. if len(msg) < self.min_size:
  46. return ModerationDecision.ABSTAIN
  47. n = 0
  48. for char in msg:
  49. if char.strip() == '':
  50. continue
  51. if char == char.upper():
  52. n += 1
  53. if n / len(msg) >= self.threshold:
  54. return self.decision
  55. return ModerationDecision.ABSTAIN