The KISS Twitch bot
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

163 satır
4.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. 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)