The KISS Twitch bot
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

147 行
4.0 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. 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['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. messages: [str]
  46. def __init__(
  47. self,
  48. time_between: int = 10,
  49. msgs_between: int = 10,
  50. strategy: TimerStrategy = TimerStrategy.ROUND_ROBIN,
  51. messages: [str] = None
  52. ):
  53. self.time_between = time_between
  54. self.msgs_between = msgs_between
  55. self.strategy = strategy
  56. self.messages = messages if messages else []
  57. @classmethod
  58. def from_dict(cls, param: dict):
  59. return Timer(
  60. time_between=param.get('between', {}).get('time', 10),
  61. msgs_between=param.get('between', {}).get('messages', 10),
  62. strategy=TimerStrategy(param.get('strategy', 'round-robin')),
  63. messages=param.get('messages', [])
  64. )
  65. class Config:
  66. channel: str
  67. nickname: str
  68. token: str
  69. command_prefix: str
  70. commands: [Command]
  71. timer: Timer
  72. def __init__(
  73. self,
  74. channel: str,
  75. nickname: str,
  76. token: str,
  77. command_prefix: str,
  78. commands: [Command],
  79. timer: Timer
  80. ):
  81. self.nickname = nickname
  82. self.channel = channel
  83. self.token = token
  84. self.command_prefix = command_prefix
  85. self.commands = commands
  86. self.timer = timer
  87. @classmethod
  88. def from_dict(cls, params: dict, token: str):
  89. commands_prefix = params.get('command_prefix', '!')
  90. commands = []
  91. help_command = Command("help", "Voici les commandes disponibles : ")
  92. for command in params.get('commands', []):
  93. c = Command.from_dict(command)
  94. if c.disabled:
  95. continue
  96. commands.append(c)
  97. help_command.message = "%s %s%s" % (help_command.message, commands_prefix, c.name)
  98. if params.get('help', True):
  99. commands.append(help_command)
  100. return Config(
  101. params.get('channel'),
  102. params.get('nickname'),
  103. token,
  104. commands_prefix,
  105. commands,
  106. Timer.from_dict(params.get('timer', {}))
  107. )
  108. def find_command(self, command: str) -> Union[None, Command]:
  109. if command.startswith(self.command_prefix):
  110. command = command[1:]
  111. for c in self.commands:
  112. if c.name == command or command in c.aliases:
  113. return c
  114. return None
  115. def get_config(file_path: str):
  116. with open(file_path, 'r') as config_file:
  117. token = environ['TWITCH_TOKEN']
  118. return Config.from_dict(json.loads(config_file.read()), token)