2022-12-26 19:37:25 +08:00
|
|
|
|
# 此模块提供了消息处理的具体逻辑的接口
|
2023-01-01 18:27:34 +08:00
|
|
|
|
import asyncio
|
2023-02-25 00:27:14 +08:00
|
|
|
|
import time
|
2023-11-26 23:58:06 +08:00
|
|
|
|
import traceback
|
2022-12-26 19:37:25 +08:00
|
|
|
|
|
2023-01-17 21:21:35 +08:00
|
|
|
|
import mirai
|
2022-12-26 19:37:25 +08:00
|
|
|
|
import logging
|
|
|
|
|
|
2023-01-04 17:09:57 +08:00
|
|
|
|
# 这里不使用动态引入config
|
|
|
|
|
# 因为在这里动态引入会卡死程序
|
|
|
|
|
# 而此模块静态引用config与动态引入的表现一致
|
2023-02-22 23:37:13 +08:00
|
|
|
|
# 已弃用,由于超时时间现已动态使用
|
|
|
|
|
# import config as config_init_import
|
2022-12-26 19:37:25 +08:00
|
|
|
|
|
2023-11-13 21:59:23 +08:00
|
|
|
|
from ..qqbot import ratelimit
|
|
|
|
|
from ..qqbot import command, message
|
|
|
|
|
from ..openai import session as openai_session
|
|
|
|
|
from ..utils import context
|
|
|
|
|
|
|
|
|
|
from ..plugin import host as plugin_host
|
|
|
|
|
from ..plugin import models as plugin_models
|
|
|
|
|
from ..qqbot import ignore
|
|
|
|
|
from ..qqbot import banlist
|
|
|
|
|
from ..qqbot import blob
|
2023-04-07 13:20:57 +08:00
|
|
|
|
import tips as tips_custom
|
2023-01-14 22:36:48 +08:00
|
|
|
|
|
2022-12-26 19:37:25 +08:00
|
|
|
|
processing = []
|
|
|
|
|
|
|
|
|
|
|
2023-02-25 15:39:31 +08:00
|
|
|
|
def is_admin(qq: int) -> bool:
|
|
|
|
|
"""兼容list和int类型的管理员判断"""
|
2023-11-26 23:58:06 +08:00
|
|
|
|
config = context.get_config_manager().data
|
|
|
|
|
if type(config['admin_qq']) == list:
|
|
|
|
|
return qq in config['admin_qq']
|
2023-02-25 15:39:31 +08:00
|
|
|
|
else:
|
2023-11-26 23:58:06 +08:00
|
|
|
|
return qq == config['admin_qq']
|
2023-02-25 15:39:31 +08:00
|
|
|
|
|
|
|
|
|
|
2023-11-13 21:59:23 +08:00
|
|
|
|
def process_message(launcher_type: str, launcher_id: int, text_message: str, message_chain: mirai.MessageChain,
|
|
|
|
|
sender_id: int) -> mirai.MessageChain:
|
2022-12-26 19:37:25 +08:00
|
|
|
|
global processing
|
|
|
|
|
|
2023-11-13 21:59:23 +08:00
|
|
|
|
mgr = context.get_qqbot_manager()
|
2022-12-26 19:37:25 +08:00
|
|
|
|
|
2022-12-26 23:53:56 +08:00
|
|
|
|
reply = []
|
2022-12-26 19:37:25 +08:00
|
|
|
|
session_name = "{}_{}".format(launcher_type, launcher_id)
|
|
|
|
|
|
2023-01-07 16:50:34 +08:00
|
|
|
|
# 检查发送方是否被禁用
|
2023-03-05 10:33:16 +08:00
|
|
|
|
if banlist.is_banned(launcher_type, launcher_id, sender_id):
|
2023-02-27 13:57:45 +08:00
|
|
|
|
logging.info("根据禁用列表忽略{}_{}的消息".format(launcher_type, launcher_id))
|
|
|
|
|
return []
|
2023-01-07 16:50:34 +08:00
|
|
|
|
|
2023-02-23 10:02:55 +08:00
|
|
|
|
if ignore.ignore(text_message):
|
|
|
|
|
logging.info("根据忽略规则忽略消息: {}".format(text_message))
|
|
|
|
|
return []
|
|
|
|
|
|
2023-11-26 23:58:06 +08:00
|
|
|
|
config = context.get_config_manager().data
|
2023-04-15 17:33:57 +08:00
|
|
|
|
|
2023-11-26 23:58:06 +08:00
|
|
|
|
if not config['wait_last_done'] and session_name in processing:
|
2023-11-13 21:59:23 +08:00
|
|
|
|
return mirai.MessageChain([mirai.Plain(tips_custom.message_drop_tip)])
|
2023-04-15 17:33:57 +08:00
|
|
|
|
|
2023-01-09 21:15:13 +08:00
|
|
|
|
# 检查是否被禁言
|
|
|
|
|
if launcher_type == 'group':
|
2023-04-24 10:34:51 +08:00
|
|
|
|
is_muted = mgr.adapter.is_muted(launcher_id)
|
|
|
|
|
if is_muted:
|
2023-04-21 17:51:58 +08:00
|
|
|
|
logging.info("机器人被禁言,跳过消息处理(group_{})".format(launcher_id))
|
2023-01-01 19:19:04 +08:00
|
|
|
|
return reply
|
2023-01-01 18:27:34 +08:00
|
|
|
|
|
2023-11-26 23:58:06 +08:00
|
|
|
|
if config['income_msg_check']:
|
2023-03-05 10:49:07 +08:00
|
|
|
|
if mgr.reply_filter.is_illegal(text_message):
|
2023-11-13 21:59:23 +08:00
|
|
|
|
return mirai.MessageChain(mirai.Plain("[bot] 消息中存在不合适的内容, 请更换措辞"))
|
2023-03-05 10:49:07 +08:00
|
|
|
|
|
2023-11-13 21:59:23 +08:00
|
|
|
|
openai_session.get_session(session_name).acquire_response_lock()
|
2022-12-26 19:37:25 +08:00
|
|
|
|
|
2023-02-24 17:30:03 +08:00
|
|
|
|
text_message = text_message.strip()
|
|
|
|
|
|
2023-05-18 20:12:36 +08:00
|
|
|
|
|
|
|
|
|
# 为强制消息延迟计时
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
|
2023-01-14 20:34:33 +08:00
|
|
|
|
# 处理消息
|
2022-12-26 19:37:25 +08:00
|
|
|
|
try:
|
|
|
|
|
|
2023-01-14 20:34:33 +08:00
|
|
|
|
processing.append(session_name)
|
2022-12-26 19:37:25 +08:00
|
|
|
|
try:
|
2023-12-18 16:31:45 +08:00
|
|
|
|
msg_type = ''
|
2024-01-12 16:48:47 +08:00
|
|
|
|
if text_message.startswith('!') or text_message.startswith("!"): # 命令
|
2023-12-18 16:31:45 +08:00
|
|
|
|
msg_type = 'command'
|
2023-01-14 22:36:48 +08:00
|
|
|
|
# 触发插件事件
|
|
|
|
|
args = {
|
|
|
|
|
'launcher_type': launcher_type,
|
|
|
|
|
'launcher_id': launcher_id,
|
|
|
|
|
'sender_id': sender_id,
|
|
|
|
|
'command': text_message[1:].strip().split(' ')[0],
|
|
|
|
|
'params': text_message[1:].strip().split(' ')[1:],
|
|
|
|
|
'text_message': text_message,
|
2023-02-25 15:39:31 +08:00
|
|
|
|
'is_admin': is_admin(sender_id),
|
2023-01-14 22:36:48 +08:00
|
|
|
|
}
|
2023-01-14 23:35:03 +08:00
|
|
|
|
event = plugin_host.emit(plugin_models.PersonCommandSent
|
2023-01-14 22:36:48 +08:00
|
|
|
|
if launcher_type == 'person'
|
2023-01-14 23:35:03 +08:00
|
|
|
|
else plugin_models.GroupCommandSent, **args)
|
2023-01-14 22:36:48 +08:00
|
|
|
|
|
2023-01-15 20:37:23 +08:00
|
|
|
|
if event.get_return_value("alter") is not None:
|
|
|
|
|
text_message = event.get_return_value("alter")
|
|
|
|
|
|
2023-01-15 13:57:05 +08:00
|
|
|
|
# 取出插件提交的返回值赋值给reply
|
|
|
|
|
if event.get_return_value("reply") is not None:
|
2023-01-15 21:13:44 +08:00
|
|
|
|
reply = event.get_return_value("reply")
|
2023-01-15 13:57:05 +08:00
|
|
|
|
|
2023-01-14 22:36:48 +08:00
|
|
|
|
if not event.is_prevented_default():
|
2023-11-13 21:59:23 +08:00
|
|
|
|
reply = command.process_command(session_name, text_message,
|
2023-02-25 15:39:31 +08:00
|
|
|
|
mgr, config, launcher_type, launcher_id, sender_id, is_admin(sender_id))
|
2023-01-03 00:02:18 +08:00
|
|
|
|
|
2023-01-14 20:34:33 +08:00
|
|
|
|
else: # 消息
|
2023-12-18 16:31:45 +08:00
|
|
|
|
msg_type = 'message'
|
2023-02-25 00:27:14 +08:00
|
|
|
|
# 限速丢弃检查
|
|
|
|
|
# print(ratelimit.__crt_minute_usage__[session_name])
|
2023-11-26 23:58:06 +08:00
|
|
|
|
if config['rate_limit_strategy'] == "drop":
|
2023-02-25 00:27:14 +08:00
|
|
|
|
if ratelimit.is_reach_limit(session_name):
|
|
|
|
|
logging.info("根据限速策略丢弃[{}]消息: {}".format(session_name, text_message))
|
2023-04-07 16:32:32 +08:00
|
|
|
|
|
2023-11-13 21:59:23 +08:00
|
|
|
|
return mirai.MessageChain(["[bot]"+tips_custom.rate_limit_drop_tip]) if tips_custom.rate_limit_drop_tip != "" else []
|
2023-02-25 00:27:14 +08:00
|
|
|
|
|
|
|
|
|
before = time.time()
|
2023-01-14 22:36:48 +08:00
|
|
|
|
# 触发插件事件
|
|
|
|
|
args = {
|
|
|
|
|
"launcher_type": launcher_type,
|
|
|
|
|
"launcher_id": launcher_id,
|
|
|
|
|
"sender_id": sender_id,
|
|
|
|
|
"text_message": text_message,
|
|
|
|
|
}
|
2023-01-14 23:35:03 +08:00
|
|
|
|
event = plugin_host.emit(plugin_models.PersonNormalMessageReceived
|
2023-01-14 22:36:48 +08:00
|
|
|
|
if launcher_type == 'person'
|
2023-01-14 23:35:03 +08:00
|
|
|
|
else plugin_models.GroupNormalMessageReceived, **args)
|
2023-01-14 22:36:48 +08:00
|
|
|
|
|
2023-01-15 20:37:23 +08:00
|
|
|
|
if event.get_return_value("alter") is not None:
|
|
|
|
|
text_message = event.get_return_value("alter")
|
|
|
|
|
|
2023-01-15 22:41:47 +08:00
|
|
|
|
# 取出插件提交的返回值赋值给reply
|
|
|
|
|
if event.get_return_value("reply") is not None:
|
|
|
|
|
reply = event.get_return_value("reply")
|
|
|
|
|
|
2023-01-14 22:36:48 +08:00
|
|
|
|
if not event.is_prevented_default():
|
2023-11-13 21:59:23 +08:00
|
|
|
|
reply = message.process_normal_message(text_message,
|
2023-01-15 13:57:05 +08:00
|
|
|
|
mgr, config, launcher_type, launcher_id, sender_id)
|
2022-12-26 19:37:25 +08:00
|
|
|
|
|
2023-02-25 00:27:14 +08:00
|
|
|
|
# 限速等待时间
|
2023-11-26 23:58:06 +08:00
|
|
|
|
if config['rate_limit_strategy'] == "wait":
|
2023-02-25 00:27:14 +08:00
|
|
|
|
time.sleep(ratelimit.get_rest_wait_time(session_name, time.time() - before))
|
|
|
|
|
|
2023-04-06 20:34:56 +08:00
|
|
|
|
ratelimit.add_usage(session_name)
|
2023-02-25 00:27:14 +08:00
|
|
|
|
|
2023-02-25 10:47:52 +08:00
|
|
|
|
if reply is not None and len(reply) > 0 and (type(reply[0]) == str or type(reply[0]) == mirai.Plain):
|
2023-02-25 20:29:21 +08:00
|
|
|
|
if type(reply[0]) == mirai.Plain:
|
|
|
|
|
reply[0] = reply[0].text
|
2022-12-26 23:53:56 +08:00
|
|
|
|
logging.info(
|
|
|
|
|
"回复[{}]文字消息:{}".format(session_name,
|
2023-01-01 17:20:54 +08:00
|
|
|
|
reply[0][:min(100, len(reply[0]))] + (
|
|
|
|
|
"..." if len(reply[0]) > 100 else "")))
|
2023-12-18 16:31:45 +08:00
|
|
|
|
if msg_type == 'message':
|
|
|
|
|
reply = [mgr.reply_filter.process(reply[0])]
|
|
|
|
|
|
2023-03-15 20:33:44 +08:00
|
|
|
|
reply = blob.check_text(reply[0])
|
2022-12-27 23:01:04 +08:00
|
|
|
|
else:
|
2023-03-04 21:14:10 +08:00
|
|
|
|
logging.info("回复[{}]消息".format(session_name))
|
2022-12-26 19:37:25 +08:00
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
processing.remove(session_name)
|
|
|
|
|
finally:
|
2023-11-13 21:59:23 +08:00
|
|
|
|
openai_session.get_session(session_name).release_response_lock()
|
2022-12-26 19:37:25 +08:00
|
|
|
|
|
2023-05-18 20:12:36 +08:00
|
|
|
|
# 检查延迟时间
|
2023-11-26 23:58:06 +08:00
|
|
|
|
if config['force_delay_range'][1] == 0:
|
2023-05-18 20:12:36 +08:00
|
|
|
|
delay_time = 0
|
|
|
|
|
else:
|
|
|
|
|
import random
|
|
|
|
|
|
|
|
|
|
# 从延迟范围中随机取一个值(浮点)
|
2023-11-26 23:58:06 +08:00
|
|
|
|
rdm = random.uniform(config['force_delay_range'][0], config['force_delay_range'][1])
|
2023-05-18 20:12:36 +08:00
|
|
|
|
|
|
|
|
|
spent = time.time() - start_time
|
|
|
|
|
|
|
|
|
|
# 如果花费时间小于延迟时间,则延迟
|
|
|
|
|
delay_time = rdm - spent if rdm - spent > 0 else 0
|
|
|
|
|
|
|
|
|
|
# 延迟
|
|
|
|
|
if delay_time > 0:
|
|
|
|
|
logging.info("[风控] 强制延迟{:.2f}秒(如需关闭,请到config.py修改force_delay_range字段)".format(delay_time))
|
|
|
|
|
time.sleep(delay_time)
|
|
|
|
|
|
2023-11-13 21:59:23 +08:00
|
|
|
|
return mirai.MessageChain(reply)
|