This commit is contained in:
dece 2022-11-29 12:56:56 +01:00
parent 6eccd2974b
commit c8c54f2e37
3 changed files with 32 additions and 27 deletions

View file

@ -177,7 +177,7 @@ class Plugin:
self, self,
key: str, key: str,
value: Any, value: Any,
ns: str = None, ns: Optional[str] = None,
skip_save: bool = False, skip_save: bool = False,
) -> None: ) -> None:
"""Append a value to a list in the plugin persistent storage.""" """Append a value to a list in the plugin persistent storage."""
@ -253,7 +253,7 @@ class Plugin:
return False return False
# Is it a question I can answer? # 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", []): for preamble in self.config.get("questions", []):
aliases = self.config.get("aliases", {}).get(preamble, []) aliases = self.config.get("aliases", {}).get(preamble, [])
for q in (preamble, *aliases): for q in (preamble, *aliases):
@ -263,7 +263,7 @@ class Plugin:
return False return False
def __save_question(self, question: str, matched: str, preamble: str): def __save_question(self, question: str, matched: str, preamble: str):
content = question[len(matched) :].strip() content = question[len(matched):].strip()
content = content.rstrip("?").rstrip() content = content.rstrip("?").rstrip()
self.question = Question(preamble, content) self.question = Question(preamble, content)
self.bot.log_i(f"Answering from plugin {self.name}: {self.question}") self.bot.log_i(f"Answering from plugin {self.name}: {self.question}")
@ -300,19 +300,8 @@ class Plugin:
available_commands = self.config.get("commands", []) available_commands = self.config.get("commands", [])
aliases = self.config.get("aliases", {}) aliases = self.config.get("aliases", {})
for ident in available_commands: 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. # 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.ident = ident
parsed_command.matched = ident parsed_command.matched = ident
self.__save_command(parsed_command) self.__save_command(parsed_command)
@ -320,13 +309,31 @@ class Plugin:
# Second case: an alias of the identifier has been used. # Second case: an alias of the identifier has been used.
ident_aliases = aliases.get(ident, []) ident_aliases = aliases.get(ident, [])
for alias in ident_aliases: 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.ident = ident
parsed_command.matched = alias parsed_command.matched = alias
self.__save_command(parsed_command) self.__save_command(parsed_command)
return True return True
return False 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( def __parse_command(
self, message: str, no_content: bool = False self, message: str, no_content: bool = False
) -> Optional[Command]: ) -> Optional[Command]:

View file

@ -6,11 +6,11 @@ from edmond.plugin import Plugin
class TranslatePlugin(Plugin): class TranslatePlugin(Plugin):
"""Translate text using the `translate` package. """Translate text using the `translate` package.
The translate package can use a bunch of translation interfaces but the default is The translate package can use a bunch of translation interfaces but the
MyMemory which is fine for our purposes. There are two ways to ask for a default is MyMemory which is fine for our purposes. There are two ways to
translation: ask for a translation:
- Without any additional params, the source language is autodetected and the target - Without any additional params, the source language is autodetected and
language is specified in the config file; the target language is specified in the config file;
- With BOTH source and target languages. - With BOTH source and target languages.
""" """

View file

@ -19,9 +19,7 @@ class YoutubeParserPlugin(Plugin):
super().__init__(bot) super().__init__(bot)
self.priority = -3 self.priority = -3
self._youtube_plugin: Optional[YoutubePlugin] = None self._youtube_plugin: Optional[YoutubePlugin] = None
self._playlist_of_the_day_plugin: Optional[ self._potd_plugin: Optional[PlaylistOfTheDayPlugin] = None
PlaylistOfTheDayPlugin
] = None
@property @property
def youtube_plugin(self) -> Optional[YoutubePlugin]: def youtube_plugin(self) -> Optional[YoutubePlugin]:
@ -34,12 +32,12 @@ class YoutubeParserPlugin(Plugin):
@property @property
def playlist_of_the_day_plugin(self) -> Optional[PlaylistOfTheDayPlugin]: def playlist_of_the_day_plugin(self) -> Optional[PlaylistOfTheDayPlugin]:
if self._playlist_of_the_day_plugin is None: if self._potd_plugin is None:
self._playlist_of_the_day_plugin = cast( self._potd_plugin = cast(
PlaylistOfTheDayPlugin, PlaylistOfTheDayPlugin,
self.bot.get_plugin("playlistoftheday"), self.bot.get_plugin("playlistoftheday"),
) )
return self._playlist_of_the_day_plugin return self._potd_plugin
def on_welcome(self, _): def on_welcome(self, _):
if not (self.youtube_plugin and self.youtube_plugin.is_ready): if not (self.youtube_plugin and self.youtube_plugin.is_ready):