keyword 更新,允许群外回复 (#112)

1. 群外回复,ext_<群组 id>::回复信息
2. 修复自定义频率限制的一个 bug
3. 去除原来仅支持群组的限制,但 pm 的变量会有削减
This commit is contained in:
c3b2a 2021-02-05 09:13:18 +08:00 committed by GitHub
parent 928a3ad4c3
commit ecc3782e91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 76 deletions

View File

@ -96,13 +96,15 @@ def get_rule(chat_id, rule_type, rule_index):
def valid_time(chat_id):
global msg_freq, group_last_time
cus_rate = get_redis(f"keyword.{chat_id}.settings").get("freq", msg_freq)
if cus_rate and is_num(str(cus_rate, "ascii")):
msg_freq = float(cus_rate)
cus_freq = get_redis(f"keyword.{chat_id}.settings").get("freq", msg_freq)
try:
cus_freq = float(cus_freq)
except:
cus_freq = msg_freq
n_time = time.time()
chat_id = int(chat_id)
if chat_id in group_last_time:
if n_time - group_last_time[chat_id] >= msg_freq:
if n_time - group_last_time[chat_id] >= cus_freq:
return True
else:
return False
@ -118,8 +120,11 @@ async def del_msg(context, t_lim):
async def send_reply(chat_id, reply_msg, context):
try:
real_chat_id = chat_id
chat = context.chat
sender = context.sender
replace_data = {}
if chat_id < 0:
replace_data = {
"user_id": sender.id,
"first_name": sender.first_name,
@ -127,17 +132,29 @@ async def send_reply(chat_id, reply_msg, context):
"chat_id": chat.id,
"chat_name": chat.title
}
else:
replace_data["user_id"] = chat_id
if sender:
replace_data["first_name"] = sender.first_name
replace_data["last_name"] = sender.last_name if sender.last_name else ""
if chat:
replace_data["chat_id"] = chat.id
replace_data["chat_name"] = chat.title
update_last_time = False
could_send_msg = valid_time(chat_id)
for re_type, re_msg in reply_msg:
for k, v in replace_data.items():
re_msg = re_msg.replace(f"${k}", str(v))
type_parse = re_type.split(",")
for s in type_parse:
if len(s) >= 5 and "ext_" == s[0:4] and is_num(s[4:]):
chat_id = int(s[4:])
break
if "plain" in type_parse:
if could_send_msg:
update_last_time = True
await bot.send_message(chat_id, re_msg, reply_to = None)
elif "reply" in type_parse:
elif "reply" in type_parse and chat_id == real_chat_id:
if could_send_msg:
update_last_time = True
await bot.send_message(chat_id, re_msg, reply_to = context.id)
@ -166,6 +183,7 @@ async def send_reply(chat_id, reply_msg, context):
sleep_time = re_msg.split()[1]
if is_num(sleep_time):
await asyncio.sleep(int(sleep_time))
chat_id = real_chat_id
if update_last_time:
global group_last_time
group_last_time[int(chat_id)] = time.time()
@ -181,10 +199,6 @@ async def reply(context):
await del_msg(context, 5)
return
chat_id = context.chat_id
if chat_id > 0:
await context.edit("请在群组中使用")
await del_msg(context, 5)
return
plain_dict = get_redis(f"keyword.{chat_id}.plain")
regex_dict = get_redis(f"keyword.{chat_id}.regex")
params = context.parameter
@ -285,10 +299,6 @@ async def reply_set(context):
await del_msg(context, 5)
return
chat_id = context.chat_id
if chat_id > 0:
await context.edit("请在群组中使用")
await del_msg(context, 5)
return
params = context.parameter
redis_data = f"keyword.{chat_id}.settings"
if len(params) >= 1 and params[0] == "global":
@ -449,7 +459,6 @@ async def auto_reply(context):
return
chat_id = context.chat_id
sender_id = context.sender_id
if chat_id < 0:
if context.id not in read_context:
plain_dict = get_redis(f"keyword.{chat_id}.plain")
regex_dict = get_redis(f"keyword.{chat_id}.regex")

View File

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