新增on_file触发器

This commit is contained in:
Wuyi无疑 2023-04-04 00:20:29 +08:00
parent c0802a9b48
commit 087f81db83
4 changed files with 31 additions and 5 deletions

View File

@ -47,6 +47,10 @@ async def msg_process(msg: MessageReceive) -> Event:
event.image_list.append(_msg.data) event.image_list.append(_msg.data)
elif _msg.type == 'reply': elif _msg.type == 'reply':
event.reply = _msg.data 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) _content.append(_msg)
event.content = _content event.content = _content
return event return event

View File

@ -29,6 +29,8 @@ class Event(MessageReceive):
at_list: List[Any] = [] at_list: List[Any] = []
is_tome: bool = False is_tome: bool = False
reply: Optional[str] = None reply: Optional[str] = None
file_name: Optional[str] = None
file: Optional[str] = None
class MessageSend(Struct): class MessageSend(Struct):

View File

@ -101,7 +101,9 @@ class SV:
def _on( def _on(
self, self,
type: Literal['prefix', 'suffix', 'keyword', 'fullmatch', 'command'], type: Literal[
'prefix', 'suffix', 'keyword', 'fullmatch', 'command', 'file'
],
keyword: Union[str, Tuple[str, ...]], keyword: Union[str, Tuple[str, ...]],
block: bool = False, block: bool = False,
to_me: bool = False, to_me: bool = False,
@ -162,3 +164,11 @@ class SV:
to_me: bool = False, to_me: bool = False,
) -> Callable: ) -> Callable:
return self._on('command', keyword, block, to_me) 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)

View File

@ -6,7 +6,9 @@ from gsuid_core.models import Event
class Trigger: class Trigger:
def __init__( def __init__(
self, self,
type: Literal['prefix', 'suffix', 'keyword', 'fullmatch', 'command'], type: Literal[
'prefix', 'suffix', 'keyword', 'fullmatch', 'command', 'file'
],
keyword: str, keyword: str,
func: Callable, func: Callable,
block: bool = False, block: bool = False,
@ -18,13 +20,15 @@ class Trigger:
self.block = block self.block = block
self.to_me = to_me self.to_me = to_me
def check_command(self, raw_msg: Event) -> bool: def check_command(self, ev: Event) -> bool:
msg = raw_msg.raw_text msg = ev.raw_text
if self.to_me: if self.to_me:
if raw_msg.is_tome: if ev.is_tome:
pass pass
else: else:
return False return False
if self.type == 'file':
return self._check_file(self.keyword, ev)
return getattr(self, f'_check_{self.type}')(self.keyword, msg) return getattr(self, f'_check_{self.type}')(self.keyword, msg)
def _check_prefix(self, prefix: str, msg: str) -> bool: def _check_prefix(self, prefix: str, msg: str) -> bool:
@ -52,6 +56,12 @@ class Trigger:
return True return True
return False 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: async def get_command(self, msg: Event) -> Event:
msg.command = self.keyword msg.command = self.keyword
msg.text = msg.raw_text.replace(self.keyword, '') msg.text = msg.raw_text.replace(self.keyword, '')