diff --git a/commands/command_base.py b/commands/command_base.py index 569f9a0..5e50e85 100644 --- a/commands/command_base.py +++ b/commands/command_base.py @@ -1,4 +1,9 @@ from abc import ABCMeta, abstractmethod +from enum import Enum, auto + +import twitch.chat + +from twitch_script import Twitch_Module class AbstractCommand(metaclass=ABCMeta): @@ -10,15 +15,25 @@ class AbstractCommand(metaclass=ABCMeta): Generally, it would be advisable to define the command (something like !so, !tts, !songrequest) as a variable of the class and to then call super().__init__(command) """ - def __init__(self, command: str): + + class CommandType(Enum): + NONE = auto() + TWITCH = auto() + DISCORD = auto() + + def __init__(self, command: str, n_args: int = 0, command_type=CommandType.NONE): self.command = command + self.n_args = n_args + self.command_type = command_type - def honk(self): - print("Success! %s" % self.command) + # no touch! + def get_args(self, text: str) -> list: + return text.split(" ")[0:self.n_args + 1] + # no touch! def get_command(self) -> str: return self.command @abstractmethod - def do_command(self, fulltext): + def do_command(self, bot: Twitch_Module, twitch_message: twitch.chat.Message): pass diff --git a/commands/implemented/command_test.py b/commands/implemented/command_test.py new file mode 100644 index 0000000..7004c6b --- /dev/null +++ b/commands/implemented/command_test.py @@ -0,0 +1,17 @@ +from abc import ABCMeta + +import twitch.chat + +from commands.command_base import AbstractCommand +from twitch_script import Twitch_Module + + +class CommandTest(AbstractCommand, metaclass=ABCMeta): + command = "!test" + + def __init__(self): + super().__init__(CommandTest.command, command_type=AbstractCommand.CommandType.TWITCH) + + def do_command(self, bot: Twitch_Module, twitch_message: twitch.chat.Message): + print("!test Detected") + bot.chat.send("test acknowledged") diff --git a/commands/implemented/command_tts.py b/commands/implemented/command_tts.py index c172cea..91abc71 100644 --- a/commands/implemented/command_tts.py +++ b/commands/implemented/command_tts.py @@ -1,15 +1,22 @@ from abc import ABCMeta +import twitch.chat + from commands.command_base import AbstractCommand +from twitch_script import Twitch_Module class CommandTTS(AbstractCommand, metaclass=ABCMeta): - command = "!tts" def __init__(self): - super().__init__(CommandTTS.command) - - def do_command(self, fulltext): - print(fulltext) + super().__init__(CommandTTS.command, n_args=1, command_type=AbstractCommand.CommandType.TWITCH) + def do_command(self, bot: Twitch_Module, twitch_message: twitch.chat.Message): + args = self.get_args(twitch_message.text) + if args[1] == "start": + bot.send_message("tts activated on #%s" % twitch_message.channel) + bot.tts_enabled = True + elif args[1] == "stop": + bot.send_message("tts deactivated") + bot.tts_enabled = False diff --git a/commands/loader.py b/commands/loader.py index 265c3d0..a3b0d9f 100644 --- a/commands/loader.py +++ b/commands/loader.py @@ -3,11 +3,12 @@ import importlib.util import inspect import os import sys +from typing import Dict from commands.command_base import AbstractCommand -def load_commands() -> dict: +def load_commands() -> Dict[str, AbstractCommand]: commands = compile_and_load() return commands @@ -27,7 +28,7 @@ def compile_and_load_file(path: str) -> (str, AbstractCommand): return "", None -def compile_and_load() -> dict: +def compile_and_load() -> Dict[str, AbstractCommand]: dic = {} implementations = get_implementations_dir() for dirName, subdirList, fileList in os.walk(implementations): @@ -69,4 +70,5 @@ def check_dir(path: str) -> str: if __name__ == "__main__": - load_commands() + cmds = load_commands() + cmds["!tts"].do_command("!tts let's do some shit") diff --git a/twitch_script.py b/twitch_script.py index ccc5156..5ccaabd 100644 --- a/twitch_script.py +++ b/twitch_script.py @@ -7,8 +7,11 @@ import twitch.chat import config as config import db import tts +import commands.loader as command_loader import credentials +from commands.command_base import AbstractCommand + class Twitch_Module(): def __init__(self): @@ -18,7 +21,7 @@ class Twitch_Module(): self.db_manager: db.db_module = db.db_module() self.chat: twitch.Chat - + self.commands = command_loader.load_commands() self.tts_enabled: bool = False self.tts_whitelist_enabled: bool = False self.links_allowed: bool = True @@ -64,22 +67,27 @@ class Twitch_Module(): self.tts_message(message) def eval_commands(self, message: twitch.chat.Message): - containsURL: bool = self.contains_url(message) + # containsURL: bool = self.contains_url(message) + command_text = message.text[0:message.text.index(' ')] - if message.text.startswith('!tts start'): - print("tts activated on #" + message.channel) - self.send_message("tts activated") - self.tts_enabled = True + command = self.commands[command_text] + if command is not None and command.command_type is AbstractCommand.CommandType.TWITCH: + command.do_command(self, message) - if message.text.startswith('!tts stop'): - print("tts deactivated on #" + message.channel) - self.send_message("tts deactivated") - self.tts_enabled = True - - if message.text.startswith('!test'): - print("!test Detected") - message.chat.send("test acknowledged") - # message.chat.send(f'@{message.user().display_name}, you have {message.user().view_count} views.') + # if message.text.startswith('!tts start'): + # print("tts activated on #" + message.channel) + # self.send_message("tts activated") + # self.tts_enabled = True + # + # if message.text.startswith('!tts stop'): + # print("tts deactivated on #" + message.channel) + # self.send_message("tts deactivated") + # self.tts_enabled = False + # + # if message.text.startswith('!test'): + # print("!test Detected") + # message.chat.send("test acknowledged") + # # message.chat.send(f'@{message.user().display_name}, you have {message.user().view_count} views.') if message.text.startswith('!roll'): try: