diff options
| author | 2023-05-02 23:30:19 +0800 | |
|---|---|---|
| committer | 2023-05-02 23:30:19 +0800 | |
| commit | dfa97c9d24b8124ca38ec5eb605cde932cd6ea78 (patch) | |
| tree | 470a8bfd1960964b8c9724c19de876cf52cf60c3 /tests/plugins/iamai_plugin_base/__init__.py | |
| parent | 45c49007ee01143ad26899751b856658321cd3ae (diff) | |
| download | HydroRoll-dfa97c9d24b8124ca38ec5eb605cde932cd6ea78.tar.gz HydroRoll-dfa97c9d24b8124ca38ec5eb605cde932cd6ea78.zip | |
Diffstat (limited to 'tests/plugins/iamai_plugin_base/__init__.py')
| -rw-r--r-- | tests/plugins/iamai_plugin_base/__init__.py | 85 |
1 files changed, 0 insertions, 85 deletions
diff --git a/tests/plugins/iamai_plugin_base/__init__.py b/tests/plugins/iamai_plugin_base/__init__.py deleted file mode 100644 index 8fe559b..0000000 --- a/tests/plugins/iamai_plugin_base/__init__.py +++ /dev/null @@ -1,85 +0,0 @@ -import re -from abc import ABC, abstractmethod -from typing import Type, Union, Generic, TypeVar - -from iamai import Plugin -from iamai.typing import T_State -from iamai.adapter.cqhttp.event import GroupMessageEvent, PrivateMessageEvent - -from .config import BasePluginConfig, RegexPluginConfig, CommandPluginConfig - -T_Config = TypeVar("T_Config", bound=BasePluginConfig) -T_RegexPluginConfig = TypeVar("T_RegexPluginConfig", bound=RegexPluginConfig) -T_CommandPluginConfig = TypeVar("T_CommandPluginConfig", bound=CommandPluginConfig) - - -class BasePlugin( - Plugin[Union[PrivateMessageEvent, GroupMessageEvent], T_State, T_Config], - ABC, - Generic[T_State, T_Config], -): - Config: Type[T_Config] = BasePluginConfig - - def format_str(self, format_str: str, message_str: str = "") -> str: - return format_str.format( - message=message_str, - user_name=self.event.sender.nickname, - user_id=self.event.sender.user_id, - ) - - async def rule(self) -> bool: - if self.event.adapter.name != "cqhttp": - return False - if self.event.type != "message_sent": - return False - if self.config.handle_all_message: - return self.str_match(self.event.message.get_plain_text()) - elif self.config.handle_friend_message: - if self.event.message_type == "private": - return self.str_match(self.event.message.get_plain_text()) - elif self.config.handle_group_message: - if self.event.message_type == "group": - if ( - self.config.accept_group is None - or self.event.group_id in self.config.accept_group - ): - return self.str_match(self.event.message.get_plain_text()) - return False - - @abstractmethod - def str_match(self, msg_str: str) -> bool: - raise NotImplemented - - -class RegexPluginBase(BasePlugin[T_State, T_RegexPluginConfig], ABC): - msg_match: re.Match - re_pattern: re.Pattern - Config: Type[T_RegexPluginConfig] = RegexPluginConfig - - def str_match(self, msg_str: str) -> bool: - msg_str = msg_str.strip() - self.msg_match = self.re_pattern.fullmatch(msg_str) - return bool(self.msg_match) - - -class CommandPluginBase(RegexPluginBase[T_State, T_CommandPluginConfig], ABC): - command_match: re.Match - command_re_pattern: re.Pattern - Config: Type[T_CommandPluginConfig] = CommandPluginConfig - - def str_match(self, msg_str: str) -> bool: - if not hasattr(self, "command_re_pattern"): - self.command_re_pattern = re.compile( - f'[{"".join(self.config.command_prefix)}]' - f'({"|".join(self.config.command)})' - r"\s*(?P<command_args>.*)", - flags=re.I if self.config.ignore_case else 0, - ) - msg_str = msg_str.strip() - self.command_match = self.command_re_pattern.fullmatch(msg_str) - if not self.command_match: - return False - self.msg_match = self.re_pattern.fullmatch( - self.command_match.group("command_args") - ) - return bool(self.msg_match) |
