Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: make the prefix and suffix detector supports multiple detections #149

Closed
wants to merge 5 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions src/graia/ariadne/message/parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async def target(self, interface: DecoratorInterface):
class DetectPrefix(ChainDecorator):
"""前缀检测器"""

def __init__(self, prefix: str) -> None:
def __init__(self, prefix: Union[str, List[str]]) -> None:
BlueGlassBlock marked this conversation as resolved.
Show resolved Hide resolved
"""初始化前缀检测器.

Args:
BlueGlassBlock marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -65,17 +65,27 @@ def __init__(self, prefix: str) -> None:
async def decorate(self, chain: MessageChain, interface: DecoratorInterface) -> Optional[MessageChain]:
header = chain.include(Quote, Source)
rest: MessageChain = chain.exclude(Quote, Source)
if not rest.startswith(self.prefix):
result = None
if isinstance(self.prefix, List):
for prefix in self.prefix:
if rest.startswith(prefix):
BlueGlassBlock marked this conversation as resolved.
Show resolved Hide resolved
result = rest.removeprefix(prefix).removeprefix(" ")
break
elif isinstance(self.prefix, str):
if rest.startswith(self.prefix):
result = rest.removeprefix(self.prefix).removeprefix(" ")
else:
BlueGlassBlock marked this conversation as resolved.
Show resolved Hide resolved
raise ExecutionStop
if result is None:
raise ExecutionStop
result = rest.removeprefix(self.prefix).removeprefix(" ")
if interface.annotation is MessageChain:
return header + result


class DetectSuffix(ChainDecorator):
"""后缀检测器"""

def __init__(self, suffix: str) -> None:
def __init__(self, suffix: Union[str, List[str]]) -> None:
BlueGlassBlock marked this conversation as resolved.
Show resolved Hide resolved
"""初始化后缀检测器.

Args:
Expand All @@ -86,9 +96,19 @@ def __init__(self, suffix: str) -> None:
async def decorate(self, chain: MessageChain, interface: DecoratorInterface) -> Optional[MessageChain]:
header = chain.include(Quote, Source)
rest: MessageChain = chain.exclude(Quote, Source)
if not rest.endswith(self.suffix):
result = None
if isinstance(self.suffix, List):
for suffix in self.suffix:
if rest.endswith(suffix):
result = rest.removeprefix(suffix).removeprefix(" ")
break
elif isinstance(self.suffix, str):
if rest.endswith(self.suffix):
result = rest.removesuffix(self.suffix).removesuffix(" ")
else:
raise ExecutionStop
if result is None:
raise ExecutionStop
result = rest.removesuffix(self.suffix).removesuffix(" ")
if interface.annotation is MessageChain:
return header + result

Expand Down