SpiceBot/sopel_SpiceBot_Core_Prerun/pipe_split.py
deathbybandaid 2b7071a5e0 test
2022-02-22 12:51:10 -05:00

60 lines
2.2 KiB
Python

import functools
from sopel_SpiceBot_Core_1 import sb
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):
if not comrun.continuerun:
return
# Get list of trigger command(s)
pipes = sb.commands.get_commands_split(trigger, "|")
comrun.trigger_dict = pipes[0]
del pipes[0]
function(bot, trigger, comrun, *args, **kwargs)
if len(pipes):
repipe_trigger_dict = reassemble_pipes(pipes, comrun.say)
sb.commands.dispatch(repipe_trigger_dict)
comrun.continuerun = False
return internal_pipe_split
return actual_decorator
def reassemble_pipes(pipes, comrunsay):
first_pipe = pipes[0]
del pipes[0]
repipe_trigger_dict = {
"trigger_type": first_pipe["trigger_type"],
"trigger_prefix": first_pipe["trigger_prefix"],
"trigger_str": first_pipe["trigger_str"],
"trigger_command": first_pipe["trigger_command"],
"trigger_hostmask": first_pipe["trigger_hostmask"],
"trigger_sender": first_pipe["trigger_sender"]
}
repipe_trigger_dict["trigger_str"] += " %s" % comrunsay
for trigger_dict in pipes:
if trigger_dict["trigger_type"] == "command":
repipe_trigger_dict["trigger_str"] += " | %s%s %s" % (trigger_dict["trigger_prefix"], trigger_dict["trigger_command"], trigger_dict["trigger_str"])
elif trigger_dict["trigger_type"] == "nickname_command":
repipe_trigger_dict["trigger_str"] += " | %s %s %s" % (trigger_dict["trigger_prefix"], trigger_dict["trigger_command"], trigger_dict["trigger_str"])
elif trigger_dict["trigger_type"] == "action_command":
repipe_trigger_dict["trigger_str"] += " | %s %s %s" % ("/me", trigger_dict["trigger_command"], trigger_dict["trigger_str"])
return repipe_trigger_dict