V2 Commands
This commit is contained in:
parent
c590443de3
commit
ffdd19f46a
@ -17,6 +17,13 @@ class AbstractCommand(metaclass=ABCMeta):
|
|||||||
Praxis = auto()
|
Praxis = auto()
|
||||||
TWITCH = auto()
|
TWITCH = auto()
|
||||||
DISCORD = 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):
|
def __init__(self, command: str, n_args: int = 0, command_type=CommandType.NONE, helpText:list=["No Help"], CommandEnabled = True):
|
||||||
self.command = command
|
self.command = command
|
||||||
@ -46,5 +53,5 @@ class AbstractCommand(metaclass=ABCMeta):
|
|||||||
return self.isCommandEnabled
|
return self.isCommandEnabled
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def do_command(self, bot, twitch_message):
|
def do_command(self, bot, command, rest):
|
||||||
pass
|
pass
|
||||||
25
commands/implemented/Command_test_v2.py
Normal file
25
commands/implemented/Command_test_v2.py
Normal 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
|
||||||
@ -1,29 +1,50 @@
|
|||||||
import flask
|
import flask
|
||||||
from flask import request
|
from flask import request
|
||||||
|
|
||||||
|
import commands.loader as command_loader
|
||||||
|
from commands.command_base import AbstractCommand
|
||||||
|
|
||||||
api = flask.Flask(__name__)
|
api = flask.Flask(__name__)
|
||||||
# enable/disable this to get web pages of crashes returned
|
# enable/disable this to get web pages of crashes returned
|
||||||
api.config["DEBUG"] = True
|
api.config["DEBUG"] = True
|
||||||
|
|
||||||
|
loadedCommands = {}
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
# todo load entire command library and cache it here
|
# 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:
|
def is_command(command: str) -> bool:
|
||||||
|
#print(command)
|
||||||
|
for cmd in loadedCommands:
|
||||||
|
#print(cmd)
|
||||||
|
if command == cmd:
|
||||||
|
return True
|
||||||
|
|
||||||
if command == "!echo":
|
if command == "!echo":
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def handle_command(source, command, rest):
|
||||||
def handle_command(command, rest):
|
|
||||||
if command == "!echo":
|
if command == "!echo":
|
||||||
message = "Got payload [%s]" % rest
|
message = "Got payload [%s]" % rest
|
||||||
print(message)
|
#print(message)
|
||||||
return flask.make_response("{\"message\":\"%s\"}" % message, 200, {"Content-Type": "application/json"})
|
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'])
|
@api.route('/api/v1/command', methods=['GET'])
|
||||||
def command_check():
|
def command_check():
|
||||||
@ -41,7 +62,10 @@ def exec_command():
|
|||||||
if 'rest' not in request.args:
|
if 'rest' not in request.args:
|
||||||
return flask.make_response('{\"text\":"Argument \'rest\' not in request"}', 400)
|
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__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import twitch.chat
|
|||||||
import config as config
|
import config as config
|
||||||
import credentials
|
import credentials
|
||||||
from cooldowns import Cooldown_Module
|
from cooldowns import Cooldown_Module
|
||||||
|
import commands.command_base
|
||||||
|
|
||||||
|
|
||||||
class Twitch_Module():
|
class Twitch_Module():
|
||||||
@ -83,6 +84,9 @@ class Twitch_Module():
|
|||||||
if start == -1:
|
if start == -1:
|
||||||
start = x
|
start = x
|
||||||
|
|
||||||
|
if idx == -1:
|
||||||
|
idx = len(message)
|
||||||
|
|
||||||
command = message[start:idx]
|
command = message[start:idx]
|
||||||
rest = message[idx + 1:]
|
rest = message[idx + 1:]
|
||||||
return command, rest
|
return command, rest
|
||||||
@ -96,7 +100,7 @@ class Twitch_Module():
|
|||||||
|
|
||||||
def exec_command(self, command: str, rest: str):
|
def exec_command(self, command: str, rest: str):
|
||||||
# todo need to url-escape command and rest
|
# 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
|
url = "http://localhost:5000/api/v1/exec?%s" % params
|
||||||
resp = requests.get(url)
|
resp = requests.get(url)
|
||||||
if resp.status_code == 200:
|
if resp.status_code == 200:
|
||||||
@ -116,6 +120,7 @@ class Twitch_Module():
|
|||||||
def twitch_chat(self, message: twitch.chat.Message) -> None:
|
def twitch_chat(self, message: twitch.chat.Message) -> None:
|
||||||
print("[#" + message.channel + "](" + message.sender + ")> " + message.text)
|
print("[#" + message.channel + "](" + message.sender + ")> " + message.text)
|
||||||
command, rest = self.parse_line(message.text)
|
command, rest = self.parse_line(message.text)
|
||||||
|
|
||||||
is_actionable = self.is_command(command)
|
is_actionable = self.is_command(command)
|
||||||
if is_actionable:
|
if is_actionable:
|
||||||
self.exec_command(command, rest)
|
self.exec_command(command, rest)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user