From 50326abdc993effe591addb2ee9d9c55767a69e5 Mon Sep 17 00:00:00 2001 From: KimigaiiWuyi <444835641@qq.com> Date: Sun, 26 Nov 2023 05:07:27 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E5=AF=B9=E4=BA=8E`on=5Fregex`?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=E5=99=A8=E6=96=B0=E5=A2=9E`ev.regex=5Fdict`?= =?UTF-8?q?=E5=92=8C`ev.regex=5Fgroup`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gsuid_core/models.py | 4 +++- gsuid_core/plugins/gs_test.py | 6 ++++-- gsuid_core/segment.py | 6 ++++-- gsuid_core/trigger.py | 7 +++++-- gsuid_core/utils/plugins_config/send_pic_config.py | 2 +- 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/gsuid_core/models.py b/gsuid_core/models.py index d7b3df2..133aaa9 100644 --- a/gsuid_core/models.py +++ b/gsuid_core/models.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, List, Literal, Optional +from typing import Any, Dict, List, Tuple, Literal, Optional from msgspec import Struct @@ -34,6 +34,8 @@ class Event(MessageReceive): file_name: Optional[str] = None file: Optional[str] = None file_type: Optional[Literal['url', 'base64']] = None + regex_group: Tuple[str, ...] = () + regex_dict: Dict[str, str] = {} class MessageSend(Struct): diff --git a/gsuid_core/plugins/gs_test.py b/gsuid_core/plugins/gs_test.py index 67c7c41..1590e24 100644 --- a/gsuid_core/plugins/gs_test.py +++ b/gsuid_core/plugins/gs_test.py @@ -79,8 +79,10 @@ async def get_keyword_msg(bot: Bot, ev: Event): await bot.send('[关键词测试]校验成功!') -@sv_switch.on_regex(r'\d+') +@sv_switch.on_regex(r'这是一个(?P正则|数字)测试!(?P[\d]+)') async def get_regex_msg(bot: Bot, ev: Event): await bot.send('正在进行[正则测试]') await asyncio.sleep(2) - await bot.send('[正则测试]校验成功!') + await bot.send( + f'[正则测试]校验成功!{ev.regex_dict["name"]}你输入的是{ev.regex_dict["int"]}' + ) diff --git a/gsuid_core/segment.py b/gsuid_core/segment.py index 0d43dd1..624a14c 100644 --- a/gsuid_core/segment.py +++ b/gsuid_core/segment.py @@ -202,8 +202,10 @@ async def _convert_message_to_image( and len(message.data) >= int(text2pic_limit) ): image_bytes = await text2pic(message.data) - elif message.type == 'image': - img: Union[bytes, str] = message.data + message = Message(type='image', data=image_bytes) + + if message.type == 'image': + img: Union[bytes, str] = message.data # type: ignore if isinstance(img, str) and img.startswith('base64://'): image_b64 = img image_bytes = b64decode(img[9:]) diff --git a/gsuid_core/trigger.py b/gsuid_core/trigger.py index a610471..5d464e1 100644 --- a/gsuid_core/trigger.py +++ b/gsuid_core/trigger.py @@ -80,8 +80,11 @@ class Trigger: msg.command = self.keyword msg.text = msg.raw_text.replace(self.keyword, '') else: - command_list = re.findall(self.keyword, msg.raw_text) - msg.command = '|'.join(command_list) + command_group = re.search(self.keyword, msg.raw_text) + if command_group: + msg.regex_dict = command_group.groupdict() + msg.regex_group = command_group.groups() + msg.command = '|'.join(list(msg.regex_group)) text_list = re.split(self.keyword, msg.raw_text) msg.text = '|'.join(text_list) return msg diff --git a/gsuid_core/utils/plugins_config/send_pic_config.py b/gsuid_core/utils/plugins_config/send_pic_config.py index f80c31f..b600145 100644 --- a/gsuid_core/utils/plugins_config/send_pic_config.py +++ b/gsuid_core/utils/plugins_config/send_pic_config.py @@ -4,7 +4,7 @@ from .models import GSC, GsStrConfig SEND_PIC_CONIFG: Dict[str, GSC] = { 'onebot': GsStrConfig('OneBot图片发送方式', '可选link或base64', 'base64'), - 'onebotv12': GsStrConfig('OneBot V12图片发送方式', '可选link或base64', 'base64'), + 'onebot_v12': GsStrConfig('OneBot V12图片发送方式', '可选link或base64', 'base64'), 'qqguild': GsStrConfig('QQ Guild图片发送方式', '可选link或base64', 'base64'), 'qqgroup': GsStrConfig('QQ Group图片发送方式', '可选link或base64', 'link'), 'telegram': GsStrConfig('Telegram图片发送方式', '可选link或base64', 'base64'),