keyword 插件更新,允许对单个规则进行设置 (#106)

This commit is contained in:
c3b2a 2021-02-03 08:06:42 +08:00 committed by GitHub
parent 25370973f4
commit c260326e28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 11 deletions

View File

@ -82,6 +82,16 @@ def get_capture(search_data, group_name):
except: except:
return None return None
def get_rule(chat_id, rule_type, rule_index):
rule_index = int(rule_index)
rule_data = get_redis(f"keyword.{chat_id}.{rule_type}")
index = 0
for k, v in rule_data.items():
if index == rule_index:
return encode(k)
index += 1
return None
async def del_msg(context, t_lim): async def del_msg(context, t_lim):
await asyncio.sleep(t_lim) await asyncio.sleep(t_lim)
await context.delete() await context.delete()
@ -165,6 +175,7 @@ async def reply(context):
elif parse[0][0] == "del" and len(parse) == 2: elif parse[0][0] == "del" and len(parse) == 2:
if parse[0][1] == "plain": if parse[0][1] == "plain":
if parse[1] in plain_dict: if parse[1] in plain_dict:
redis.delete(f"keyword.{chat_id}.single.plain.{encode(parse[1])}")
plain_dict.pop(parse[1]) plain_dict.pop(parse[1])
redis.set(f"keyword.{chat_id}.plain", save_rules(plain_dict, placeholder)) redis.set(f"keyword.{chat_id}.plain", save_rules(plain_dict, placeholder))
else: else:
@ -173,6 +184,7 @@ async def reply(context):
return return
elif parse[0][1] == "regex": elif parse[0][1] == "regex":
if parse[1] in regex_dict: if parse[1] in regex_dict:
redis.delete(f"keyword.{chat_id}.single.regex.{encode(parse[1])}")
regex_dict.pop(parse[1]) regex_dict.pop(parse[1])
redis.set(f"keyword.{chat_id}.regex", save_rules(regex_dict, placeholder)) redis.set(f"keyword.{chat_id}.regex", save_rules(regex_dict, placeholder))
else: else:
@ -187,16 +199,24 @@ async def reply(context):
await del_msg(context, 5) await del_msg(context, 5)
elif parse[0][0] == "list" and len(parse) == 1: elif parse[0][0] == "list" and len(parse) == 1:
plain_msg = "Plain: \n" plain_msg = "Plain: \n"
index = 0
for k, v in plain_dict.items(): for k, v in plain_dict.items():
plain_msg += f"`{k}` -> `{v}`\n" plain_msg += f"`{index}`: `{k}` -> `{v}`\n"
index += 1
regex_msg = "Regex: \n" regex_msg = "Regex: \n"
index = 0
for k, v in regex_dict.items(): for k, v in regex_dict.items():
regex_msg += f"`{k}` -> `{v}`\n" regex_msg += f"`{index}`: `{k}` -> `{v}`\n"
index += 1
await context.edit(plain_msg + "\n" + regex_msg) await context.edit(plain_msg + "\n" + regex_msg)
elif parse[0][0] == "clear" and len(parse) == 1: elif parse[0][0] == "clear" and len(parse) == 1:
if parse[0][1] == "plain": if parse[0][1] == "plain":
for k in plain_dict.keys():
redis.delete(f"keyword.{chat_id}.single.plain.{encode(k)}")
redis.set(f"keyword.{chat_id}.plain", "") redis.set(f"keyword.{chat_id}.plain", "")
elif parse[0][1] == "regex": elif parse[0][1] == "regex":
for k in regex_dict.keys():
redis.delete(f"keyword.{chat_id}.single.regex.{encode(k)}")
redis.set(f"keyword.{chat_id}.regex", "") redis.set(f"keyword.{chat_id}.regex", "")
else: else:
await context.edit("参数错误") await context.edit("参数错误")
@ -223,10 +243,15 @@ async def reply_set(context):
await del_msg(context, 5) await del_msg(context, 5)
return return
params = context.parameter params = context.parameter
is_global = len(params) >= 1 and params[0] == "global" redis_data = f"keyword.{chat_id}.settings"
redis_data = "keyword.settings" if is_global else f"keyword.{chat_id}.settings" if len(params) >= 1 and params[0] == "global":
if is_global: redis_data = "keyword.settings"
del params[0] del params[0]
elif len(params) >= 2 and params[0] in ("plain", "regex") and is_num(params[1]):
rule_data = get_rule(chat_id, params[0], params[1])
if rule_data:
redis_data = f"keyword.{chat_id}.single.{params[0]}.{rule_data}"
del params[0:2]
settings_dict = get_redis(redis_data) settings_dict = get_redis(redis_data)
cmd_list = ["help", "mode", "list", "show", "clear"] cmd_list = ["help", "mode", "list", "show", "clear"]
cmd_dict = {"help": (1, ), "mode": (2, ), "list": (2, 3), "show": (1, ), "clear": (1, )} cmd_dict = {"help": (1, ), "mode": (2, ), "list": (2, 3), "show": (1, ), "clear": (1, )}
@ -365,13 +390,21 @@ async def auto_reply(context):
send_text = context.text send_text = context.text
for k, v in plain_dict.items(): for k, v in plain_dict.items():
if k in send_text and time.time() - last_time > msg_rate: if k in send_text and time.time() - last_time > msg_rate:
if validate(str(sender_id), int(mode), user_list): tmp = get_redis(f"keyword.{chat_id}.single.plain.{encode(k)}")
could_reply = validate(str(sender_id), int(mode), user_list)
if tmp:
could_reply = validate(str(sender_id), int(tmp.get("mode", "0")), tmp.get("list", []))
if could_reply:
last_time = time.time() last_time = time.time()
await send_reply(chat_id, parse_multi(v), context) await send_reply(chat_id, parse_multi(v), context)
for k, v in regex_dict.items(): for k, v in regex_dict.items():
pattern = re.compile(k) pattern = re.compile(k)
if pattern.search(send_text) and time.time() - last_time > msg_rate: if pattern.search(send_text) and time.time() - last_time > msg_rate:
if validate(str(sender_id), int(mode), user_list): tmp = get_redis(f"keyword.{chat_id}.single.regex.{encode(k)}")
could_reply = validate(str(sender_id), int(mode), user_list)
if tmp:
could_reply = validate(str(sender_id), int(tmp.get("mode", "0")), tmp.get("list", []))
if could_reply:
last_time = time.time() last_time = time.time()
catch_pattern = r"\$\{regex_(?P<str>((?!\}).)+)\}" catch_pattern = r"\$\{regex_(?P<str>((?!\}).)+)\}"
count = 0 count = 0

View File

@ -362,10 +362,10 @@
}, },
{ {
"name": "keyword", "name": "keyword",
"version": "1.8", "version": "2.0",
"section": "chat", "section": "chat",
"maintainer": "c3b2a", "maintainer": "c3b2a",
"size": "15.5 kb", "size": "17.0 kb",
"supported": true, "supported": true,
"des-short": "群组关键词自动回复插件", "des-short": "群组关键词自动回复插件",
"des": "命令: keyword, replyset。" "des": "命令: keyword, replyset。"