|
|
|
@ -177,7 +177,7 @@ class Plugin:
|
|
|
|
|
self,
|
|
|
|
|
key: str,
|
|
|
|
|
value: Any,
|
|
|
|
|
ns: str = None,
|
|
|
|
|
ns: Optional[str] = None,
|
|
|
|
|
skip_save: bool = False,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Append a value to a list in the plugin persistent storage."""
|
|
|
|
@ -253,7 +253,7 @@ class Plugin:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
# Is it a question I can answer?
|
|
|
|
|
question = message[len(words[0]) :].strip()
|
|
|
|
|
question = message[len(words[0]):].strip()
|
|
|
|
|
for preamble in self.config.get("questions", []):
|
|
|
|
|
aliases = self.config.get("aliases", {}).get(preamble, [])
|
|
|
|
|
for q in (preamble, *aliases):
|
|
|
|
@ -263,7 +263,7 @@ class Plugin:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def __save_question(self, question: str, matched: str, preamble: str):
|
|
|
|
|
content = question[len(matched) :].strip()
|
|
|
|
|
content = question[len(matched):].strip()
|
|
|
|
|
content = content.rstrip("?").rstrip()
|
|
|
|
|
self.question = Question(preamble, content)
|
|
|
|
|
self.bot.log_i(f"Answering from plugin {self.name}: {self.question}")
|
|
|
|
@ -300,19 +300,8 @@ class Plugin:
|
|
|
|
|
available_commands = self.config.get("commands", [])
|
|
|
|
|
aliases = self.config.get("aliases", {})
|
|
|
|
|
for ident in available_commands:
|
|
|
|
|
# Match commands differently according to no_content. If no_content
|
|
|
|
|
# is True, check the parsed command (pc) raw data as a string that
|
|
|
|
|
# may contain the available identifier (ai) at its beginning.
|
|
|
|
|
# If no_content is False (default), simply compare the parsed
|
|
|
|
|
# identifier with available identifiers.
|
|
|
|
|
if no_content:
|
|
|
|
|
matches = lambda pc, ai: (
|
|
|
|
|
pc.raw == ai or pc.raw.startswith(ai + " ")
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
matches = lambda pc, ai: pc.ident == ai
|
|
|
|
|
# First case: the command identifier has been used.
|
|
|
|
|
if matches(parsed_command, ident):
|
|
|
|
|
if self.__command_matches(parsed_command, ident, no_content):
|
|
|
|
|
parsed_command.ident = ident
|
|
|
|
|
parsed_command.matched = ident
|
|
|
|
|
self.__save_command(parsed_command)
|
|
|
|
@ -320,13 +309,31 @@ class Plugin:
|
|
|
|
|
# Second case: an alias of the identifier has been used.
|
|
|
|
|
ident_aliases = aliases.get(ident, [])
|
|
|
|
|
for alias in ident_aliases:
|
|
|
|
|
if matches(parsed_command, alias):
|
|
|
|
|
if self.__command_matches(parsed_command, alias, no_content):
|
|
|
|
|
parsed_command.ident = ident
|
|
|
|
|
parsed_command.matched = alias
|
|
|
|
|
self.__save_command(parsed_command)
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def __command_matches(
|
|
|
|
|
command: Command,
|
|
|
|
|
ident: str,
|
|
|
|
|
no_content: bool
|
|
|
|
|
) -> bool:
|
|
|
|
|
"""Return True if this command matches this command identifier.
|
|
|
|
|
|
|
|
|
|
Match commands differently according to no_content. If no_content is
|
|
|
|
|
True, check the parsed command raw data as a string that
|
|
|
|
|
may contain the available identifier at its beginning.
|
|
|
|
|
If no_content is False (default), simply compare the parsed
|
|
|
|
|
identifier with available identifiers.
|
|
|
|
|
"""
|
|
|
|
|
if no_content:
|
|
|
|
|
return command.raw == ident or command.raw.startswith(ident + " ")
|
|
|
|
|
return command.ident == ident
|
|
|
|
|
|
|
|
|
|
def __parse_command(
|
|
|
|
|
self, message: str, no_content: bool = False
|
|
|
|
|
) -> Optional[Command]:
|
|
|
|
|