1010from arclet .letoderea .exceptions import ProviderUnsatisfied
1111from arclet .letoderea .provider import ProviderFactory
1212from nepattern .util import CUnionType
13- from satori import MessageObject
13+ from satori import ChannelType , MessageObject
1414from satori .element import Text
1515from tarina import LRU
1616from tarina .generic import generic_isinstance , get_origin , origin_is_union
1919from ..const import ITEM_MESSAGE_CONTENT
2020from ..event .base import Reply
2121from ..event .command import CommandOutput , CommandParse , CommandReceive
22+ from ..filter .parse import parse_filter
2223from ..message import MessageChain
2324from ..session import Session
2425from .model import CommandResult , Match , Query
@@ -36,18 +37,18 @@ def _is_optional(annotation: Any) -> bool:
3637 return False
3738
3839
39- def _remove_config_prefix (message : MessageChain ) :
40+ def _remove_config_prefix (message : MessageChain , allow_skip : bool = False ) -> MessageChain :
4041 command_prefix = EntariConfig .instance .basic .prefix
4142 nickname = EntariConfig .instance .basic .nickname
4243 if not command_prefix and not nickname :
4344 return message
4445 if message and isinstance (message [0 ], Text ):
4546 text = message [0 ].text .lstrip ()
4647 if nickname :
47- if not ( mat := re .match (rf"^@?{ re .escape (nickname )} [,,:\s]+" , text ) ):
48- return MessageChain ()
49- text = text [ mat . end () :]
50- message [ 0 ] = Text ( text )
48+ if mat := re .match (rf"^@?{ re .escape (nickname )} [,,:\s]+" , text ):
49+ text = text [ mat . end () :]
50+ message [ 0 ] = Text ( text )
51+ return message
5152 if not command_prefix :
5253 return message
5354 for prefix in command_prefix :
@@ -56,29 +57,46 @@ def _remove_config_prefix(message: MessageChain):
5657 if text .startswith (prefix ):
5758 message [0 ] = Text (text [len (prefix ) :])
5859 return message
60+ if allow_skip :
61+ return message
5962 return MessageChain ()
6063
6164
65+ # fmt: off
66+
67+
6268class MessageJudges (Propagator ):
63- def __init__ (self , need_reply_me : bool , need_notice_me : bool , use_config_prefix : bool ):
69+ def __init__ (self , need_reply_me : bool , need_notice_me : bool , use_config_prefix : bool , ignore_prefix_filter : str | None = None ): # noqa: E501
6470 self .need_reply_me = need_reply_me
6571 self .need_notice_me = need_notice_me
6672 self .use_config_prefix = use_config_prefix
73+ self .ignore_prefix_checker = self ._checker (ignore_prefix_filter )
6774
68- async def judge (self , ctx : Contexts , message : MessageChain , is_reply_me : bool = False , is_notice_me : bool = False ):
75+ def _checker (self , ignore_prefix_filter : str | None ):
76+ if ignore_prefix_filter :
77+ return parse_filter (ignore_prefix_filter )
78+ else :
79+ async def checker (session : Session , * _ ): return session .channel .type is ChannelType .DIRECT
80+ return checker
81+
82+ async def judge (self , ctx : Contexts , message : MessageChain , is_reply_me : bool = False , is_notice_me : bool = False , session : Session | None = None ): # noqa: E501
6983 message = message .fork ()
7084 if self .need_reply_me and not is_reply_me :
7185 return STOP
7286 if self .need_notice_me and not is_notice_me :
7387 return STOP
74- if self .use_config_prefix and not (message := _remove_config_prefix (message )):
88+ skip = False
89+ if session :
90+ skip = await self .ignore_prefix_checker (session , is_reply_me , is_notice_me )
91+ if self .use_config_prefix and not (message := _remove_config_prefix (message , skip )):
7592 return STOP
7693 if ITEM_MESSAGE_CONTENT in ctx :
7794 return {ITEM_MESSAGE_CONTENT : message }
7895 return {"$message" : message }
7996
8097 def compose (self ):
8198 yield self .judge , True , 60
99+ # fmt: on
82100
83101
84102class AlconnaSuppiler (Propagator ):
0 commit comments