diff --git a/gsuid_core/handler.py b/gsuid_core/handler.py index 413dfe7..aaaf833 100644 --- a/gsuid_core/handler.py +++ b/gsuid_core/handler.py @@ -47,6 +47,10 @@ async def msg_process(msg: MessageReceive) -> Event: event.image_list.append(_msg.data) elif _msg.type == 'reply': event.reply = _msg.data + elif _msg.type == 'file' and _msg.data: + data = _msg.data.split('|') + event.file_name = data[0] + event.file = data[1] _content.append(_msg) event.content = _content return event diff --git a/gsuid_core/models.py b/gsuid_core/models.py index bfd8792..1e6dfe1 100644 --- a/gsuid_core/models.py +++ b/gsuid_core/models.py @@ -29,6 +29,8 @@ class Event(MessageReceive): at_list: List[Any] = [] is_tome: bool = False reply: Optional[str] = None + file_name: Optional[str] = None + file: Optional[str] = None class MessageSend(Struct): diff --git a/gsuid_core/sv.py b/gsuid_core/sv.py index 2bcd8f4..05294d3 100644 --- a/gsuid_core/sv.py +++ b/gsuid_core/sv.py @@ -101,7 +101,9 @@ class SV: def _on( self, - type: Literal['prefix', 'suffix', 'keyword', 'fullmatch', 'command'], + type: Literal[ + 'prefix', 'suffix', 'keyword', 'fullmatch', 'command', 'file' + ], keyword: Union[str, Tuple[str, ...]], block: bool = False, to_me: bool = False, @@ -162,3 +164,11 @@ class SV: to_me: bool = False, ) -> Callable: return self._on('command', keyword, block, to_me) + + def on_file( + self, + file_type: str, + block: bool = False, + to_me: bool = False, + ) -> Callable: + return self._on('file', file_type, block, to_me) diff --git a/gsuid_core/trigger.py b/gsuid_core/trigger.py index 72ca9d5..71eb6bb 100644 --- a/gsuid_core/trigger.py +++ b/gsuid_core/trigger.py @@ -6,7 +6,9 @@ from gsuid_core.models import Event class Trigger: def __init__( self, - type: Literal['prefix', 'suffix', 'keyword', 'fullmatch', 'command'], + type: Literal[ + 'prefix', 'suffix', 'keyword', 'fullmatch', 'command', 'file' + ], keyword: str, func: Callable, block: bool = False, @@ -18,13 +20,15 @@ class Trigger: self.block = block self.to_me = to_me - def check_command(self, raw_msg: Event) -> bool: - msg = raw_msg.raw_text + def check_command(self, ev: Event) -> bool: + msg = ev.raw_text if self.to_me: - if raw_msg.is_tome: + if ev.is_tome: pass else: return False + if self.type == 'file': + return self._check_file(self.keyword, ev) return getattr(self, f'_check_{self.type}')(self.keyword, msg) def _check_prefix(self, prefix: str, msg: str) -> bool: @@ -52,6 +56,12 @@ class Trigger: return True return False + def _check_file(self, file_type: str, ev: Event) -> bool: + if ev.file: + if ev.file_name and ev.file_name.split('.')[-1] == file_type: + return True + return False + async def get_command(self, msg: Event) -> Event: msg.command = self.keyword msg.text = msg.raw_text.replace(self.keyword, '')