SpiceBot/sopel_SpiceBot_Core_Prerun/dispatch_multi.py
deathbybandaid 3d5e5af76b test
2022-02-22 11:03:30 -05:00

132 lines
4.6 KiB
Python

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