From ffdd19f46a2d9ba92e75668439c7e4bcbb3290ea Mon Sep 17 00:00:00 2001 From: Alex Orid Date: Wed, 21 Apr 2021 16:10:54 -0400 Subject: [PATCH] V2 Commands --- commands/command_base.py | 11 ++++++-- commands/implemented/Command_test_v2.py | 25 ++++++++++++++++++ standalone_command.py | 34 +++++++++++++++++++++---- twitch_script_standalone.py | 7 ++++- 4 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 commands/implemented/Command_test_v2.py diff --git a/commands/command_base.py b/commands/command_base.py index 14f441d..27cf842 100644 --- a/commands/command_base.py +++ b/commands/command_base.py @@ -17,6 +17,13 @@ class AbstractCommand(metaclass=ABCMeta): Praxis = auto() TWITCH = auto() DISCORD = auto() + Ver2 = auto() + + class CommandSource(Enum): + default = 0 + Praxis = 1 + Twitch = 2 + Discord = 3 def __init__(self, command: str, n_args: int = 0, command_type=CommandType.NONE, helpText:list=["No Help"], CommandEnabled = True): self.command = command @@ -46,5 +53,5 @@ class AbstractCommand(metaclass=ABCMeta): return self.isCommandEnabled @abstractmethod - def do_command(self, bot, twitch_message): - pass + def do_command(self, bot, command, rest): + pass \ No newline at end of file diff --git a/commands/implemented/Command_test_v2.py b/commands/implemented/Command_test_v2.py new file mode 100644 index 0000000..1c910b3 --- /dev/null +++ b/commands/implemented/Command_test_v2.py @@ -0,0 +1,25 @@ +from abc import ABCMeta + +from commands.command_base import AbstractCommand + +import utilities_script as utility + +class Command_test_v2(AbstractCommand, metaclass=ABCMeta): + """ + this is the test command. + """ + command = "testerino" + + def __init__(self): + super().__init__(Command_test_v2.command, n_args=1, command_type=AbstractCommand.CommandType.Ver2) + self.help = ["This is a test command.", + "\nExample:","testerino"] + self.isCommandEnabled = True + + def do_command(self, source = AbstractCommand.CommandSource.default, command = "", rest = ""): + returnString = command + " is and rest is " + rest + #print(returnString) + return returnString + + def get_help(self): + return self.help \ No newline at end of file diff --git a/standalone_command.py b/standalone_command.py index 05b195f..9419921 100644 --- a/standalone_command.py +++ b/standalone_command.py @@ -1,29 +1,50 @@ import flask from flask import request +import commands.loader as command_loader +from commands.command_base import AbstractCommand + api = flask.Flask(__name__) # enable/disable this to get web pages of crashes returned api.config["DEBUG"] = True +loadedCommands = {} def init(): # todo load entire command library and cache it here - pass + load_commands() + + +def load_commands(): + global loadedCommands + loadedCommands = command_loader.load_commands_new(AbstractCommand.CommandType.Ver2) def is_command(command: str) -> bool: + #print(command) + for cmd in loadedCommands: + #print(cmd) + if command == cmd: + return True + if command == "!echo": return True else: return False - -def handle_command(command, rest): +def handle_command(source, command, rest): if command == "!echo": message = "Got payload [%s]" % rest - print(message) + #print(message) return flask.make_response("{\"message\":\"%s\"}" % message, 200, {"Content-Type": "application/json"}) + cmd:AbstractCommand = loadedCommands[command] + if cmd is not None: + cmd_response = cmd.do_command(source, command, rest) + return flask.make_response("{\"message\":\"%s\"}" % cmd_response, 200, {"Content-Type": "application/json"}) + + #print("Doing a command") + @api.route('/api/v1/command', methods=['GET']) def command_check(): @@ -41,7 +62,10 @@ def exec_command(): if 'rest' not in request.args: return flask.make_response('{\"text\":"Argument \'rest\' not in request"}', 400) - return handle_command(request.args['command_name'], request.args['rest']) + if 'command_source' not in request.args: + return flask.make_response('{\"text\":"Argument \'command_source\' not in request"}', 400) + + return handle_command(request.args['command_source'], request.args['command_name'], request.args['rest']) if __name__ == '__main__': diff --git a/twitch_script_standalone.py b/twitch_script_standalone.py index 0d2dd52..e40c8b4 100644 --- a/twitch_script_standalone.py +++ b/twitch_script_standalone.py @@ -9,6 +9,7 @@ import twitch.chat import config as config import credentials from cooldowns import Cooldown_Module +import commands.command_base class Twitch_Module(): @@ -83,6 +84,9 @@ class Twitch_Module(): if start == -1: start = x + if idx == -1: + idx = len(message) + command = message[start:idx] rest = message[idx + 1:] return command, rest @@ -96,7 +100,7 @@ class Twitch_Module(): def exec_command(self, command: str, rest: str): # todo need to url-escape command and rest - params = urlencode({'command_name': command, 'rest': rest}) + params = urlencode({'command_source': commands.command_base.AbstractCommand.CommandSource.default, 'command_name': command, 'rest': rest}) url = "http://localhost:5000/api/v1/exec?%s" % params resp = requests.get(url) if resp.status_code == 200: @@ -116,6 +120,7 @@ class Twitch_Module(): def twitch_chat(self, message: twitch.chat.Message) -> None: print("[#" + message.channel + "](" + message.sender + ")> " + message.text) command, rest = self.parse_line(message.text) + is_actionable = self.is_command(command) if is_actionable: self.exec_command(command, rest)