The KISS Twitch bot
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 

135 righe
3.7 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 json
  17. from os import environ
  18. from enum import Enum
  19. from typing import Union
  20. class Command:
  21. name: str
  22. message: str
  23. aliases: [str]
  24. def __init__(self, name: str, message: str, aliases: [str] = []):
  25. self.name = name
  26. self.message = message
  27. self.aliases = aliases
  28. @classmethod
  29. def from_dict(cls, params: dict):
  30. return Command(params['name'], params['message'], params.get('aliases', []))
  31. class TimerStrategy(Enum):
  32. ROUND_ROBIN = "round-robin"
  33. SHUFFLE = "shuffle"
  34. class Timer:
  35. time_between: int
  36. msgs_between: int
  37. strategy: TimerStrategy
  38. messages: [str]
  39. def __init__(
  40. self,
  41. time_between: int = 10,
  42. msgs_between: int = 10,
  43. strategy: TimerStrategy = TimerStrategy.ROUND_ROBIN,
  44. messages: [str] = None
  45. ):
  46. self.time_between = time_between
  47. self.msgs_between = msgs_between
  48. self.strategy = strategy
  49. self.messages = messages if messages else []
  50. @classmethod
  51. def from_dict(cls, param: dict):
  52. return Timer(
  53. time_between=param.get('between', {}).get('time', 10),
  54. msgs_between=param.get('between', {}).get('messages', 10),
  55. strategy=TimerStrategy(param.get('strategy', 'round-robin')),
  56. messages=param.get('messages', [])
  57. )
  58. class Config:
  59. channel: str
  60. nickname: str
  61. token: str
  62. command_prefix: str
  63. commands: [Command]
  64. timer: Timer
  65. def __init__(
  66. self,
  67. channel: str,
  68. nickname: str,
  69. token: str,
  70. command_prefix: str,
  71. commands: [Command],
  72. timer: Timer
  73. ):
  74. self.nickname = nickname
  75. self.channel = channel
  76. self.token = token
  77. self.command_prefix = command_prefix
  78. self.commands = commands
  79. self.timer = timer
  80. @classmethod
  81. def from_dict(cls, params: dict, token: str):
  82. commands_prefix = params.get('command_prefix', '!')
  83. commands = []
  84. help_command = Command("help", "Voici les commandes disponibles : ")
  85. for command in params.get('commands', []):
  86. commands.append(Command.from_dict(command))
  87. help_command.message = "%s %s%s" % (help_command.message, commands_prefix, command['name'])
  88. if params.get('help', True):
  89. commands.append(help_command)
  90. return Config(
  91. params.get('channel'),
  92. params.get('nickname'),
  93. token,
  94. commands_prefix,
  95. commands,
  96. Timer.from_dict(params.get('timer', {}))
  97. )
  98. def find_command(self, command: str) -> Union[None, Command]:
  99. if command.startswith(self.command_prefix):
  100. command = command[1:]
  101. for c in self.commands:
  102. if c.name == command or command in c.aliases:
  103. return c
  104. return None
  105. def get_config(file_path: str):
  106. with open(file_path, 'r') as config_file:
  107. token = environ['TWITCH_TOKEN']
  108. return Config.from_dict(json.loads(config_file.read()), token)