V2 Commands

This commit is contained in:
Alex Orid 2021-04-21 16:10:54 -04:00
parent c590443de3
commit ffdd19f46a
4 changed files with 69 additions and 8 deletions

View File

@ -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

View File

@ -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 <user_message> and rest is " + rest
#print(returnString)
return returnString
def get_help(self):
return self.help

View File

@ -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__':

View File

@ -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)