The KISS Twitch bot
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

config.py 4.3 KiB

3 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. disabled: bool
  25. def __init__(self, name: str, message: str, aliases: [str] = None, disabled: bool = False):
  26. self.name = name
  27. self.message = message
  28. self.aliases = aliases if aliases is not None else []
  29. self.disabled = disabled
  30. @classmethod
  31. def from_dict(cls, params: dict):
  32. return Command(
  33. params.get('name'),
  34. params['message'],
  35. params.get('aliases', []),
  36. params.get('disabled', False)
  37. )
  38. class TimerStrategy(Enum):
  39. ROUND_ROBIN = "round-robin"
  40. SHUFFLE = "shuffle"
  41. class Timer:
  42. time_between: int
  43. msgs_between: int
  44. strategy: TimerStrategy
  45. pool: [Command]
  46. def __init__(
  47. self,
  48. time_between: int = 10,
  49. msgs_between: int = 10,
  50. strategy: TimerStrategy = TimerStrategy.ROUND_ROBIN,
  51. pool: [Command] = None
  52. ):
  53. self.time_between = time_between
  54. self.msgs_between = msgs_between
  55. self.strategy = strategy
  56. self.pool = pool if pool else []
  57. @classmethod
  58. def from_dict(cls, param: dict):
  59. pool = []
  60. for c in param.get('pool', []):
  61. pool.append(Command.from_dict(c))
  62. return Timer(
  63. time_between=param.get('between', {}).get('time', 10),
  64. msgs_between=param.get('between', {}).get('messages', 10),
  65. strategy=TimerStrategy(param.get('strategy', 'round-robin')),
  66. pool=pool
  67. )
  68. class Config:
  69. channel: str
  70. nickname: str
  71. token: str
  72. command_prefix: str
  73. commands: [Command]
  74. timer: Timer
  75. def __init__(
  76. self,
  77. channel: str,
  78. nickname: str,
  79. token: str,
  80. command_prefix: str,
  81. commands: [Command],
  82. timer: Timer
  83. ):
  84. self.nickname = nickname
  85. self.channel = channel
  86. self.token = token
  87. self.command_prefix = command_prefix
  88. self.commands = commands
  89. self.timer = timer
  90. @classmethod
  91. def from_dict(cls, params: dict, token: str):
  92. timer = Timer.from_dict(params.get('timer', {}))
  93. commands_prefix = params.get('command_prefix', '!')
  94. commands = []
  95. help_command = Command("help", "Voici les commandes disponibles : ")
  96. for command in params.get('commands', []):
  97. command = Command.from_dict(command)
  98. if command.disabled:
  99. continue
  100. commands.append(command)
  101. for command in timer.pool:
  102. if command.name is None:
  103. continue
  104. commands.append(command)
  105. # Generate help command
  106. if params.get('help', True):
  107. for command in commands:
  108. help_command.message = "%s %s%s" % (help_command.message, commands_prefix, command.name)
  109. commands.append(help_command)
  110. return Config(
  111. params.get('channel'),
  112. params.get('nickname'),
  113. token,
  114. commands_prefix,
  115. commands,
  116. timer
  117. )
  118. def find_command(self, command: str) -> Union[None, Command]:
  119. if command.startswith(self.command_prefix):
  120. command = command[1:]
  121. for c in self.commands:
  122. if c.name == command or command in c.aliases:
  123. return c
  124. return None
  125. def get_config(file_path: str):
  126. with open(file_path, 'r') as config_file:
  127. token = environ['TWITCH_TOKEN']
  128. return Config.from_dict(json.loads(config_file.read()), token)