def passthrough(bot, trigger): # Grab the discord user and what they said user = trigger.group(1) text = trigger.group(2) # Split out the command part of the text cmd = text.split()[0][1:] # Re-use the Sopel regexp on the text passed prefix = bot.config.core.prefix regexp = get_command_regexp(prefix, cmd) re_match = regexp.match(text) # Create a trigger class mirroring what we need from the offical one my_trigger = Trigger(trigger, user, re_match) # Only works on my commands (currently hard-coded.) if cmd == 'cake': cake.cake(bot, my_trigger) elif cmd == 'flip': flip.flip(bot, my_trigger) elif cmd == 'hug': hug.hug(bot, my_trigger) elif cmd == 'twerk': twerk.twerk(bot, my_trigger) else: pass
def clean_callable(func, config): """Compiles the regexes, moves commands into func.rule, fixes up docs and puts them in func._docs, and sets defaults""" nick = config.core.nick alias_nicks = config.core.alias_nicks prefix = config.core.prefix help_prefix = config.core.help_prefix func._docs = {} doc = trim_docstring(func.__doc__) example = None func.unblockable = getattr(func, 'unblockable', False) func.priority = getattr(func, 'priority', 'medium') func.thread = getattr(func, 'thread', True) func.rate = getattr(func, 'rate', 0) func.channel_rate = getattr(func, 'channel_rate', 0) func.global_rate = getattr(func, 'global_rate', 0) if not hasattr(func, 'event'): func.event = ['PRIVMSG'] else: if isinstance(func.event, basestring): func.event = [func.event.upper()] else: func.event = [event.upper() for event in func.event] if hasattr(func, 'rule'): if isinstance(func.rule, basestring): func.rule = [func.rule] func.rule = [ compile_rule(nick, rule, alias_nicks) for rule in func.rule ] if hasattr(func, 'commands') or hasattr(func, 'nickname_commands'): func.rule = getattr(func, 'rule', []) for command in getattr(func, 'commands', []): regexp = get_command_regexp(prefix, command) func.rule.append(regexp) for command in getattr(func, 'nickname_commands', []): regexp = get_nickname_command_regexp(nick, command, alias_nicks) func.rule.append(regexp) if hasattr(func, 'example'): example = func.example[0]["example"] example = example.replace('$nickname', nick) if example[0] != help_prefix and not example.startswith(nick): example = help_prefix + example[len(help_prefix):] if doc or example: cmds = [] cmds.extend(getattr(func, 'commands', [])) cmds.extend(getattr(func, 'nickname_commands', [])) for command in cmds: func._docs[command] = (doc, example) if hasattr(func, 'intents'): func.intents = [ re.compile(intent, re.IGNORECASE) for intent in func.intents ]
def test_command_groups(prefix, command, groups, command_line): regex = tools.get_command_regexp(prefix, command) match = re.match(regex, command_line) assert match.group(0) == command_line assert match.group(1) == command assert match.group(2) == ' '.join(groups.values()) assert match.group(3) == groups[3] assert match.group(4) == groups[4] assert match.group(5) == groups[5] assert match.group(6) == groups[6]
def test_command_groups(prefix, command, groups, command_line): regex = get_command_regexp(prefix, command) match = re.match(regex, command_line) assert match.group(0) == command_line assert match.group(1) == command assert match.group(2) == ' '.join(groups.values()) assert match.group(3) == groups[3] assert match.group(4) == groups[4] assert match.group(5) == groups[5] assert match.group(6) == groups[6]
def clean_callable(func, config): """Compiles the regexes, moves commands into func.rule, fixes up docs and puts them in func._docs, and sets defaults""" nick = config.core.nick alias_nicks = config.core.alias_nicks prefix = config.core.prefix help_prefix = config.core.help_prefix func._docs = {} doc = trim_docstring(func.__doc__) example = None func.unblockable = getattr(func, 'unblockable', False) func.priority = getattr(func, 'priority', 'medium') func.thread = getattr(func, 'thread', True) func.rate = getattr(func, 'rate', 0) func.channel_rate = getattr(func, 'channel_rate', 0) func.global_rate = getattr(func, 'global_rate', 0) if not hasattr(func, 'event'): func.event = ['PRIVMSG'] else: if isinstance(func.event, basestring): func.event = [func.event.upper()] else: func.event = [event.upper() for event in func.event] if hasattr(func, 'rule'): if isinstance(func.rule, basestring): func.rule = [func.rule] func.rule = [compile_rule(nick, rule, alias_nicks) for rule in func.rule] if hasattr(func, 'commands') or hasattr(func, 'nickname_commands'): func.rule = getattr(func, 'rule', []) for command in getattr(func, 'commands', []): regexp = get_command_regexp(prefix, command) func.rule.append(regexp) for command in getattr(func, 'nickname_commands', []): regexp = get_nickname_command_regexp(nick, command, alias_nicks) func.rule.append(regexp) if hasattr(func, 'example'): example = func.example[0]["example"] example = example.replace('$nickname', nick) if example[0] != help_prefix and not example.startswith(nick): example = example.replace(default_prefix, help_prefix, 1) if doc or example: cmds = [] cmds.extend(getattr(func, 'commands', [])) cmds.extend(getattr(func, 'nickname_commands', [])) for command in cmds: func._docs[command] = (doc, example) if hasattr(func, 'intents'): func.intents = [re.compile(intent, re.IGNORECASE) for intent in func.intents]
def get_rule_regex(self): """Make the rule regex for this command. :return: a compiled regex for this command and its aliases The command regex factors in: * the prefix regular expression, * the rule's name (escaped for regex if needed), * all of its aliases (escaped for regex if needed), and everything is then given to :func:`sopel.tools.get_command_regexp`, which creates a compiled regex to return. """ name = [self.escape_name(self._name)] aliases = [self.escape_name(alias) for alias in self._aliases] pattern = r'|'.join(name + aliases) return tools.get_command_regexp(self._prefix, pattern)
def clean_callable(func, config): """Compiles the regexes, moves commands into func.rule, fixes up docs and puts them in func._docs, and sets defaults""" nick = config.core.nick prefix = config.core.prefix help_prefix = config.core.help_prefix func._docs = {} doc = trim_docstring(func.__doc__) example = None func.unblockable = getattr(func, 'unblockable', False) func.priority = getattr(func, 'priority', 'medium') func.thread = getattr(func, 'thread', True) func.rate = getattr(func, 'rate', 0) if not hasattr(func, 'event'): func.event = ['PRIVMSG'] else: if isinstance(func.event, str): func.event = [func.event.upper()] else: func.event = [event.upper() for event in func.event] if hasattr(func, 'rule'): if isinstance(func.rule, str): func.rule = [func.rule] func.rule = [compile_rule(nick, rule) for rule in func.rule] if hasattr(func, 'commands'): func.rule = getattr(func, 'rule', []) for command in func.commands: regexp = get_command_regexp(prefix, command) func.rule.append(regexp) if hasattr(func, 'example'): example = func.example[0]["example"] example = example.replace('$nickname', nick) if example[0] != help_prefix: example = help_prefix + example[len(help_prefix):] if doc or example: for command in func.commands: func._docs[command] = (doc, example)
def clean_callable(func, config): """Compiles the regexes, moves commands into func.rule, fixes up docs and puts them in func._docs, and sets defaults""" nick = config.core.nick prefix = config.core.prefix help_prefix = config.core.help_prefix func._docs = {} doc = trim_docstring(func.__doc__) example = None func.unblockable = getattr(func, "unblockable", False) func.priority = getattr(func, "priority", "medium") func.thread = getattr(func, "thread", True) func.rate = getattr(func, "rate", 0) if not hasattr(func, "event"): func.event = ["PRIVMSG"] else: if isinstance(func.event, basestring): func.event = [func.event.upper()] else: func.event = [event.upper() for event in func.event] if hasattr(func, "rule"): if isinstance(func.rule, basestring): func.rule = [func.rule] func.rule = [compile_rule(nick, rule) for rule in func.rule] if hasattr(func, "commands"): func.rule = getattr(func, "rule", []) for command in func.commands: regexp = get_command_regexp(prefix, command) func.rule.append(regexp) if hasattr(func, "example"): example = func.example[0]["example"] example = example.replace("$nickname", nick) if example[0] != help_prefix: example = help_prefix + example[len(help_prefix) :] if doc or example: for command in func.commands: func._docs[command] = (doc, example)
def clean_callable(func, config): """Compiles the regexes, moves commands into func.rule, fixes up docs and puts them in func._docs, and sets defaults""" nick = config.core.nick prefix = config.core.prefix help_prefix = config.core.help_prefix func._docs = {} doc = trim_docstring(func.__doc__) example = None func.unblockable = getattr(func, 'unblockable', False) func.priority = getattr(func, 'priority', 'medium') func.thread = getattr(func, 'thread', True) func.rate = getattr(func, 'rate', 0) if not hasattr(func, 'event'): func.event = ['PRIVMSG'] else: if isinstance(func.event, basestring): func.event = [func.event.upper()] else: func.event = [event.upper() for event in func.event] if hasattr(func, 'rule'): if isinstance(func.rule, basestring): func.rule = [func.rule] func.rule = [compile_rule(nick, rule) for rule in func.rule] if hasattr(func, 'commands'): func.rule = getattr(func, 'rule', []) for command in func.commands: regexp = get_command_regexp(prefix, command) func.rule.append(regexp) if hasattr(func, 'example'): example = func.example[0]["example"] example = example.replace('$nickname', nick) if example[0] != help_prefix and not example.startswith(nick): example = help_prefix + example[len(help_prefix):] if doc or example: for command in func.commands: func._docs[command] = (doc, example)
def clean_callable(func, config): """Compiles the regexes, moves commands into func.rule, fixes up docs and puts them in func._docs, and sets defaults""" nick = config.core.nick alias_nicks = config.core.alias_nicks prefix = config.core.prefix help_prefix = config.core.help_prefix func._docs = {} doc = trim_docstring(func.__doc__) examples = [] func.unblockable = getattr(func, 'unblockable', False) func.echo = getattr(func, 'echo', False) func.priority = getattr(func, 'priority', 'medium') func.thread = getattr(func, 'thread', True) func.rate = getattr(func, 'rate', 0) func.channel_rate = getattr(func, 'channel_rate', 0) func.global_rate = getattr(func, 'global_rate', 0) if not hasattr(func, 'event'): func.event = ['PRIVMSG'] else: if isinstance(func.event, basestring): func.event = [func.event.upper()] else: func.event = [event.upper() for event in func.event] if hasattr(func, 'rule'): if isinstance(func.rule, basestring): func.rule = [func.rule] func.rule = [ compile_rule(nick, rule, alias_nicks) for rule in func.rule ] if hasattr(func, 'commands') or hasattr(func, 'nickname_commands'): func.rule = getattr(func, 'rule', []) for command in getattr(func, 'commands', []): regexp = get_command_regexp(prefix, command) func.rule.append(regexp) for command in getattr(func, 'nickname_commands', []): regexp = get_nickname_command_regexp(nick, command, alias_nicks) func.rule.append(regexp) if hasattr(func, 'example'): # If no examples are flagged as user-facing, just show the first one like Sopel<7 did examples = [rec["example"] for rec in func.example if rec["help"] ] or [func.example[0]["example"]] for i, example in enumerate(examples): example = example.replace('$nickname', nick) if example[0] != help_prefix and not example.startswith(nick): example = example.replace(default_prefix, help_prefix, 1) examples[i] = example if doc or examples: cmds = [] cmds.extend(getattr(func, 'commands', [])) cmds.extend(getattr(func, 'nickname_commands', [])) for command in cmds: func._docs[command] = (doc, examples) if hasattr(func, 'intents'): func.intents = [ re.compile(intent, re.IGNORECASE) for intent in func.intents ]
def clean_callable(func, config): """Compiles the regexes, moves commands into func.rule, fixes up docs and puts them in func._docs, and sets defaults""" nick = config.core.nick alias_nicks = config.core.alias_nicks prefix = config.core.prefix help_prefix = config.core.help_prefix func._docs = {} doc = trim_docstring(func.__doc__) examples = [] func.unblockable = getattr(func, 'unblockable', False) func.echo = getattr(func, 'echo', False) func.priority = getattr(func, 'priority', 'medium') func.thread = getattr(func, 'thread', True) func.rate = getattr(func, 'rate', 0) func.channel_rate = getattr(func, 'channel_rate', 0) func.global_rate = getattr(func, 'global_rate', 0) if not hasattr(func, 'event'): func.event = ['PRIVMSG'] else: if isinstance(func.event, basestring): func.event = [func.event.upper()] else: func.event = [event.upper() for event in func.event] if hasattr(func, 'rule'): if isinstance(func.rule, basestring): func.rule = [func.rule] func.rule = [compile_rule(nick, rule, alias_nicks) for rule in func.rule] if hasattr(func, 'commands') or hasattr(func, 'nickname_commands'): func.rule = getattr(func, 'rule', []) for command in getattr(func, 'commands', []): regexp = get_command_regexp(prefix, command) if regexp not in func.rule: func.rule.append(regexp) for command in getattr(func, 'nickname_commands', []): regexp = get_nickname_command_regexp(nick, command, alias_nicks) if regexp not in func.rule: func.rule.append(regexp) if hasattr(func, 'example'): # If no examples are flagged as user-facing, just show the first one like Sopel<7 did examples = [rec["example"] for rec in func.example if rec["help"]] or [func.example[0]["example"]] for i, example in enumerate(examples): example = example.replace('$nickname', nick) if example[0] != help_prefix and not example.startswith(nick): example = example.replace(default_prefix, help_prefix, 1) examples[i] = example if doc or examples: cmds = [] cmds.extend(getattr(func, 'commands', [])) cmds.extend(getattr(func, 'nickname_commands', [])) for command in cmds: func._docs[command] = (doc, examples) if hasattr(func, 'intents'): # Can be implementation-dependent _regex_type = type(re.compile('')) func.intents = [ (intent if isinstance(intent, _regex_type) else re.compile(intent, re.IGNORECASE)) for intent in func.intents ]
def clean_callable(func, config): """Compiles the regexes, moves commands into func.rule, fixes up docs and puts them in func._docs, and sets defaults""" nick = config.core.nick alias_nicks = config.core.alias_nicks prefix = config.core.prefix help_prefix = config.core.help_prefix func._docs = {} doc = trim_docstring(func.__doc__) examples = [] func.thread = getattr(func, 'thread', True) if not is_triggerable(func): # Rate-limiting, priority, etc. doesn't apply to non-triggerable functions. # Adding the default attributes below is a waste of memory, as well as # potentially confusing to other code. return func.unblockable = getattr(func, 'unblockable', False) func.echo = getattr(func, 'echo', False) func.priority = getattr(func, 'priority', 'medium') func.rate = getattr(func, 'rate', 0) func.channel_rate = getattr(func, 'channel_rate', 0) func.global_rate = getattr(func, 'global_rate', 0) func.output_prefix = getattr(func, 'output_prefix', '') if not hasattr(func, 'event'): func.event = ['PRIVMSG'] else: if isinstance(func.event, basestring): func.event = [func.event.upper()] else: func.event = [event.upper() for event in func.event] if hasattr(func, 'rule'): if isinstance(func.rule, basestring): func.rule = [func.rule] func.rule = [compile_rule(nick, rule, alias_nicks) for rule in func.rule] if any(hasattr(func, attr) for attr in ['commands', 'nickname_commands', 'action_commands']): func.rule = getattr(func, 'rule', []) for command in getattr(func, 'commands', []): regexp = get_command_regexp(prefix, command) if regexp not in func.rule: func.rule.append(regexp) for command in getattr(func, 'nickname_commands', []): regexp = get_nickname_command_regexp(nick, command, alias_nicks) if regexp not in func.rule: func.rule.append(regexp) for command in getattr(func, 'action_commands', []): regexp = get_action_command_regexp(command) if regexp not in func.rule: func.rule.append(regexp) if hasattr(func, 'example'): # If no examples are flagged as user-facing, just show the first one like Sopel<7.0 did examples = [rec["example"] for rec in func.example if rec["help"]] or [func.example[0]["example"]] for i, example in enumerate(examples): example = example.replace('$nickname', nick) if example[0] != help_prefix and not example.startswith(nick): example = example.replace(default_prefix, help_prefix, 1) examples[i] = example if doc or examples: cmds = [] cmds.extend(getattr(func, 'commands', [])) cmds.extend(getattr(func, 'nickname_commands', [])) for command in cmds: func._docs[command] = (doc, examples) if hasattr(func, 'intents'): # Can be implementation-dependent _regex_type = type(re.compile('')) func.intents = [ (intent if isinstance(intent, _regex_type) else re.compile(intent, re.IGNORECASE)) for intent in func.intents ]
def test_basic_lua(bot, pretrigger): msg = '.lua print("test")' line = pretrigger(msg) bot = bot(line) cmd = re.compile(get_command_regexp("\.", "lua")) lua.lua_cmd(bot, Trigger(bot.config, line, cmd.match(msg)))