This commit is contained in:
deathbybandaid 2022-02-22 11:03:30 -05:00
parent c5e9d8f23c
commit 3d5e5af76b
5 changed files with 216 additions and 190 deletions

View File

@ -1,77 +1,9 @@
import functools
from sopel_SpiceBot_Core_1 import sb
class ComRun():
def __init__(self):
pass
def dispatch_multi():
"""
This splits the given command by `&&` and re-dispatches it internally to the bot.
"""
def actual_decorator(function):
@functools.wraps(function)
def internal_dispatch_multi(bot, trigger, *args, **kwargs):
bot.say("dispatch")
# Get list of trigger command(s)
commands = get_commands(bot, trigger)
# If more than 1 trigger command, dispatch
if len(commands) > 1:
for trigger_dict in commands:
sb.commands.dispatch(bot, trigger, trigger_dict)
return
function(bot, trigger, *args, **kwargs)
return internal_dispatch_multi
return actual_decorator
def dispatch_pipes():
"""
This splits the given command by ` | ` and re-dispatches it internally to the bot.
This allows the output of a command to be sent
"""
def actual_decorator(function):
@functools.wraps(function)
def internal_dispatch_pipe(bot, trigger, *args, **kwargs):
bot.say("dispatch")
# Get list of trigger command(s)
pipes = get_pipes(bot, trigger)
bot.say(str(pipes))
function(bot, trigger, *args, **kwargs)
return internal_dispatch_pipe
return actual_decorator
def command_args():
"""This Detects --arguments to commands."""
def actual_decorator(function):
@functools.wraps(function)
def internal_command_args(bot, trigger, *args, **kwargs):
bot.say("command_args")
function(bot, trigger, *args, **kwargs)
return internal_command_args
return actual_decorator
from .comrun import comrun_create
from .dispatch_multi import dispatch_multi
from .pipe_split import pipe_split
from .command_args import command_args
def prerun():
@ -79,130 +11,16 @@ def prerun():
def actual_decorator(function):
@dispatch_pipes()
@comrun_create()
@dispatch_multi()
# @command_args()
@pipe_split()
@command_args()
@functools.wraps(function)
def internal_prerun(bot, trigger, *args, **kwargs):
def internal_prerun(bot, trigger, comrun, *args, **kwargs):
bot.say("prerun")
comrun = ComRun()
function(bot, trigger, comrun, *args, **kwargs)
print(comrun.test)
return internal_prerun
return actual_decorator
def get_pipes(bot, trigger):
# Get && split for multiple commands
if "|" in trigger.args[1]:
pipes = [x.strip() for x in trigger.args[1].split("|")]
else:
pipes = [trigger.args[1]]
return pipes
def get_commands(bot, trigger):
commands = []
# Get && split for multiple commands
if "&&" in trigger.args[1]:
triggers = [x.strip() for x in trigger.args[1].split("&&")]
else:
triggers = [trigger.args[1]]
first_trigger = triggers[0]
del triggers[0]
if first_trigger.startswith(tuple(sb.config.prefix_list)):
first_trigger_type = "command"
first_trigger_prefix = first_trigger[0]
first_trigger = first_trigger[1:]
elif first_trigger.startswith(bot.nick):
first_trigger_type = "nickname_command"
first_trigger_prefix = str(first_trigger.split(" ")[0])
first_trigger = " ".join(first_trigger.split(" ")[1:])
elif "intent" in trigger.tags:
if trigger.tags["intent"] == "ACTION":
first_trigger_type = "action_command"
first_trigger_prefix = "ACTION"
else:
bot.say("what kind of rule command is this")
first_trigger_type = "rule"
first_trigger_prefix = None
first_command = first_trigger.split(" ")[0]
commands.append({
"trigger_type": first_trigger_type,
"trigger_prefix": first_trigger_prefix,
"trigger_str": first_trigger,
"trigger_command": first_command
})
for trigger_str in triggers:
if trigger_str.startswith(tuple(sb.config.prefix_list)):
trigger_type = "command"
trigger_prefix = trigger_str[0]
trigger_str = trigger_str[1:]
trigger_command = trigger_str.split(" ")[0]
elif trigger_str.startswith(bot.nick):
trigger_type = "nickname_command"
trigger_prefix = str(trigger_str.split(" ")[0])
trigger_str = " ".join(trigger_str.split(" ")[1:])
trigger_command = trigger_str.split(" ")[0]
elif trigger_str.startswith(tuple(["ACTION", "/me"])):
trigger_type = "action_command"
trigger_prefix = "ACTION"
trigger_str = " ".join(trigger_str.split(" ")[1:])
trigger_command = trigger_str.split(" ")[0]
else:
trigger_command = trigger_str.split(" ")[0]
command_types = ["command", "nickname_command", "action_command"]
# Assume same command type until proven otherwise
assumed_trigger_type = first_trigger_type
assumed_trigger_prefix = first_trigger_prefix
# Still under the assumption that the command is most likely the same type as first command
command_types.remove(assumed_trigger_type)
command_types.insert(0, assumed_trigger_type)
found = []
for command_type in command_types:
if command_type == "command":
commands_list = sb.commands.valid_sopel_commands
elif command_type == "nickname_command":
commands_list = sb.commands.valid_sopel_nickname_commands
elif command_type == "action_command":
commands_list = sb.commands.valid_sopel_action_commands
if trigger_command in commands_list:
found.append(command_type)
if len(found):
trigger_type = found[0]
if trigger_type == "command":
trigger_prefix = sb.config.prefix_list[0]
elif trigger_type == "nickname_command":
trigger_prefix = "%s," % bot.nick
elif trigger_type == "action_command":
trigger_prefix = "ACTION"
else:
trigger_type = assumed_trigger_type
trigger_prefix = assumed_trigger_prefix
commands.append({
"trigger_type": trigger_type,
"trigger_prefix": trigger_prefix,
"trigger_str": trigger_str,
"trigger_command": trigger_command
})
return commands

View File

@ -0,0 +1,17 @@
import functools
def command_args():
"""This Detects --arguments to commands."""
def actual_decorator(function):
@functools.wraps(function)
def internal_command_args(bot, trigger, comrun, *args, **kwargs):
bot.say("command_args")
function(bot, trigger, comrun, *args, **kwargs)
return internal_command_args
return actual_decorator

View File

@ -0,0 +1,23 @@
import functools
class ComRun():
def __init__(self):
pass
def comrun_create():
"""This Detects --arguments to commands."""
def actual_decorator(function):
@functools.wraps(function)
def internal_comrun_create(bot, trigger, comrun, *args, **kwargs):
bot.say("comrun_create")
comrun = ComRun()
function(bot, trigger, comrun, *args, **kwargs)
return internal_comrun_create
return actual_decorator

View File

@ -0,0 +1,131 @@
import functools
from sopel_SpiceBot_Core_1 import sb
def dispatch_multi():
"""
This splits the given command by `&&` and re-dispatches it internally to the bot.
"""
def actual_decorator(function):
@functools.wraps(function)
def internal_dispatch_multi(bot, trigger, comrun, *args, **kwargs):
bot.say("dispatch_multi")
# Get list of trigger command(s)
commands = get_commands(bot, trigger)
# If more than 1 trigger command, dispatch
if len(commands) > 1:
for trigger_dict in commands:
sb.commands.dispatch(bot, trigger, trigger_dict)
return
function(bot, trigger, comrun, *args, **kwargs)
return internal_dispatch_multi
return actual_decorator
def get_commands(bot, trigger):
commands = []
# Get && split for multiple commands
if "&&" in trigger.args[1]:
triggers = [x.strip() for x in trigger.args[1].split("&&")]
else:
triggers = [trigger.args[1]]
first_trigger = triggers[0]
del triggers[0]
if first_trigger.startswith(tuple(sb.config.prefix_list)):
first_trigger_type = "command"
first_trigger_prefix = first_trigger[0]
first_trigger = first_trigger[1:]
elif first_trigger.startswith(bot.nick):
first_trigger_type = "nickname_command"
first_trigger_prefix = str(first_trigger.split(" ")[0])
first_trigger = " ".join(first_trigger.split(" ")[1:])
elif "intent" in trigger.tags:
if trigger.tags["intent"] == "ACTION":
first_trigger_type = "action_command"
first_trigger_prefix = "ACTION"
else:
bot.say("what kind of rule command is this")
first_trigger_type = "rule"
first_trigger_prefix = None
first_command = first_trigger.split(" ")[0]
commands.append({
"trigger_type": first_trigger_type,
"trigger_prefix": first_trigger_prefix,
"trigger_str": first_trigger,
"trigger_command": first_command
})
for trigger_str in triggers:
if trigger_str.startswith(tuple(sb.config.prefix_list)):
trigger_type = "command"
trigger_prefix = trigger_str[0]
trigger_str = trigger_str[1:]
trigger_command = trigger_str.split(" ")[0]
elif trigger_str.startswith(bot.nick):
trigger_type = "nickname_command"
trigger_prefix = str(trigger_str.split(" ")[0])
trigger_str = " ".join(trigger_str.split(" ")[1:])
trigger_command = trigger_str.split(" ")[0]
elif trigger_str.startswith(tuple(["ACTION", "/me"])):
trigger_type = "action_command"
trigger_prefix = "ACTION"
trigger_str = " ".join(trigger_str.split(" ")[1:])
trigger_command = trigger_str.split(" ")[0]
else:
trigger_command = trigger_str.split(" ")[0]
command_types = ["command", "nickname_command", "action_command"]
# Assume same command type until proven otherwise
assumed_trigger_type = first_trigger_type
assumed_trigger_prefix = first_trigger_prefix
# Still under the assumption that the command is most likely the same type as first command
command_types.remove(assumed_trigger_type)
command_types.insert(0, assumed_trigger_type)
found = []
for command_type in command_types:
if command_type == "command":
commands_list = sb.commands.valid_sopel_commands
elif command_type == "nickname_command":
commands_list = sb.commands.valid_sopel_nickname_commands
elif command_type == "action_command":
commands_list = sb.commands.valid_sopel_action_commands
if trigger_command in commands_list:
found.append(command_type)
if len(found):
trigger_type = found[0]
if trigger_type == "command":
trigger_prefix = sb.config.prefix_list[0]
elif trigger_type == "nickname_command":
trigger_prefix = "%s," % bot.nick
elif trigger_type == "action_command":
trigger_prefix = "ACTION"
else:
trigger_type = assumed_trigger_type
trigger_prefix = assumed_trigger_prefix
commands.append({
"trigger_type": trigger_type,
"trigger_prefix": trigger_prefix,
"trigger_str": trigger_str,
"trigger_command": trigger_command
})
return commands

View File

@ -0,0 +1,37 @@
import functools
def pipe_split():
"""
This splits the given command by ` | ` and re-dispatches it internally to the bot.
This allows the output of a command to be sent
"""
def actual_decorator(function):
@functools.wraps(function)
def internal_pipe_split(bot, trigger, comrun, *args, **kwargs):
bot.say("pipe_split")
# Get list of trigger command(s)
pipes = get_pipes(bot, trigger)
bot.say(str(pipes))
function(bot, trigger, comrun, *args, **kwargs)
print(comrun.test)
return internal_pipe_split
return actual_decorator
def get_pipes(bot, trigger):
# Get && split for multiple commands
if "|" in trigger.args[1]:
pipes = [x.strip() for x in trigger.args[1].split("|")]
else:
pipes = [trigger.args[1]]
return pipes