From 4b351664b9855c82011121e62c43646359060a41 Mon Sep 17 00:00:00 2001 From: c3b2a <62082214+c3b2a@users.noreply.github.com> Date: Mon, 18 Jan 2021 20:24:19 +0800 Subject: [PATCH] Update keyword.py --- keyword.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/keyword.py b/keyword.py index 15fdc18..64ba9c0 100644 --- a/keyword.py +++ b/keyword.py @@ -75,12 +75,30 @@ def parse_multi(rule: str): n_rule.append(data) return n_rule +def get_capture(search_data, group_name): + try: + capture_data = search_data.group(group_name) + return capture_data + except: + return None + async def del_msg(context, t_lim): await asyncio.sleep(t_lim) await context.delete() async def send_reply(chat_id, reply_msg, context): + chat = context.chat + sender = context.sender + replace_data = { + "user_id": sender.id, + "first_name": sender.first_name, + "last_name": sender.last_name if sender.last_name else "", + "chat_id": chat.id, + "chat_name": chat.title + } for re_type, re_msg in reply_msg: + for k, v in replace_data.items(): + re_msg = re_msg.replace(f"${k}", str(v)) if re_type == "plain": await bot.send_message(chat_id, re_msg) elif re_type == "file" and len(re_msg.split()) >= 2: @@ -95,6 +113,10 @@ async def send_reply(chat_id, reply_msg, context): elif re_type == "op": if re_msg == "delete": await context.delete() + elif re_msg.split()[0] == "sleep" and len(re_msg.split()) == 2: + sleep_time = re_msg.split()[1] + if is_num(sleep_time): + await asyncio.sleep(int(sleep_time)) @listener(is_plugin=True, outgoing=True, command="keyword", description="关键词自动回复", @@ -347,4 +369,15 @@ async def auto_reply(context): if pattern.search(send_text) and time.time() - last_time > msg_rate: if validate(str(sender_id), int(mode), user_list): last_time = time.time() + catch_pattern = r"\$\{regex_(?P((?!\}).)+)\}" + while re.search(catch_pattern, v): + search_data = re.search(k, send_text) + print(search_data) + group_name = re.search(catch_pattern, v).group("str") + print(group_name) + capture_data = get_capture(search_data, group_name) + print(capture_data) + if not capture_data: capture_data = "" + v = v.replace("${regex_%s}" % group_name, capture_data) + print(v) await send_reply(chat_id, parse_multi(v), context)