The KISS Twitch bot
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 

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