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.
 
 

105 lines
2.6 KiB

  1. import json
  2. from os import environ
  3. from enum import Enum
  4. class Command:
  5. name: str
  6. message: str
  7. def __init__(self, name: str, message: str):
  8. self.name = name
  9. self.message = message
  10. @classmethod
  11. def from_dict(cls, params: dict):
  12. return Command(params['name'], params['message'])
  13. class TimerStrategy(Enum):
  14. ROUND_ROBIN = "round-robin"
  15. SHUFFLE = "shuffle"
  16. class Timer:
  17. time_between: int
  18. msgs_between: int
  19. strategy: TimerStrategy
  20. messages: [str]
  21. def __init__(
  22. self,
  23. time_between: int = 10,
  24. msgs_between: int = 10,
  25. strategy: TimerStrategy = TimerStrategy.ROUND_ROBIN,
  26. messages: [str] = None
  27. ):
  28. self.time_between = time_between
  29. self.msgs_between = msgs_between
  30. self.strategy = strategy
  31. self.messages = messages if messages else []
  32. @classmethod
  33. def from_dict(cls, param: dict):
  34. return Timer(
  35. time_between=param.get('between', {}).get('time', 10),
  36. msgs_between=param.get('between', {}).get('messages', 10),
  37. strategy=TimerStrategy(param.get('strategy', 'round-robin')),
  38. messages=param.get('messages', [])
  39. )
  40. class Config:
  41. channel: str
  42. nickname: str
  43. token: str
  44. command_prefix: str
  45. commands: [Command]
  46. timer: Timer
  47. def __init__(
  48. self,
  49. channel: str,
  50. nickname: str,
  51. token: str,
  52. command_prefix: str,
  53. commands: [Command],
  54. timer: Timer
  55. ):
  56. self.nickname = nickname
  57. self.channel = channel
  58. self.token = token
  59. self.command_prefix = command_prefix
  60. self.commands = commands
  61. self.timer = timer
  62. @classmethod
  63. def from_dict(cls, params: dict, token: str):
  64. commands_prefix = params.get('command_prefix', '!')
  65. commands = []
  66. help_command = Command("help", "Voici les commandes disponibles : ")
  67. for command in params.get('commands', []):
  68. commands.append(Command.from_dict(command))
  69. help_command.message = "%s %s%s" % (help_command.message, commands_prefix, command['name'])
  70. if params.get('help', True):
  71. commands.append(help_command)
  72. return Config(
  73. params.get('channel'),
  74. params.get('nickname'),
  75. token,
  76. commands_prefix,
  77. commands,
  78. Timer.from_dict(params.get('timer', {}))
  79. )
  80. def get_config(file_path: str):
  81. with open(file_path, 'r') as config_file:
  82. token = environ['TWITCH_TOKEN']
  83. return Config.from_dict(json.loads(config_file.read()), token)