2022-12-26 19:37:25 +08:00
|
|
|
|
# 此模块提供了消息处理的具体逻辑的接口
|
2023-01-01 18:27:34 +08:00
|
|
|
|
import asyncio
|
2022-12-26 19:37:25 +08:00
|
|
|
|
|
|
|
|
|
from func_timeout import func_set_timeout
|
|
|
|
|
import logging
|
|
|
|
|
|
2023-01-14 22:36:48 +08:00
|
|
|
|
from mirai import MessageChain, Plain
|
2022-12-27 22:52:53 +08:00
|
|
|
|
|
2023-01-04 17:09:57 +08:00
|
|
|
|
# 这里不使用动态引入config
|
|
|
|
|
# 因为在这里动态引入会卡死程序
|
|
|
|
|
# 而此模块静态引用config与动态引入的表现一致
|
2023-01-04 21:46:01 +08:00
|
|
|
|
import config as config_init_import
|
2022-12-26 19:37:25 +08:00
|
|
|
|
|
|
|
|
|
import pkg.openai.session
|
|
|
|
|
import pkg.openai.manager
|
2023-01-01 22:55:09 +08:00
|
|
|
|
import pkg.utils.reloader
|
|
|
|
|
import pkg.utils.updater
|
2023-01-01 23:18:32 +08:00
|
|
|
|
import pkg.utils.context
|
2023-01-14 20:34:33 +08:00
|
|
|
|
import pkg.qqbot.message
|
|
|
|
|
import pkg.qqbot.command
|
2022-12-26 19:37:25 +08:00
|
|
|
|
|
2023-01-14 22:36:48 +08:00
|
|
|
|
import pkg.plugin.host as plugin_host
|
|
|
|
|
import pkg.plugin.models as plugin_models
|
|
|
|
|
|
2022-12-26 19:37:25 +08:00
|
|
|
|
processing = []
|
|
|
|
|
|
|
|
|
|
|
2023-01-04 21:46:01 +08:00
|
|
|
|
@func_set_timeout(config_init_import.process_message_timeout)
|
2023-01-01 17:20:54 +08:00
|
|
|
|
def process_message(launcher_type: str, launcher_id: int, text_message: str, message_chain: MessageChain,
|
|
|
|
|
sender_id: int) -> MessageChain:
|
2022-12-26 19:37:25 +08:00
|
|
|
|
global processing
|
|
|
|
|
|
2023-01-01 23:18:32 +08:00
|
|
|
|
mgr = pkg.utils.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
|
|
|
|
# 检查发送方是否被禁用
|
|
|
|
|
if pkg.utils.context.get_qqbot_manager().enable_banlist:
|
|
|
|
|
if sender_id in pkg.utils.context.get_qqbot_manager().ban_person:
|
|
|
|
|
logging.info("根据禁用列表忽略用户{}的消息".format(sender_id))
|
|
|
|
|
return []
|
|
|
|
|
if launcher_type == 'group' and launcher_id in pkg.utils.context.get_qqbot_manager().ban_group:
|
|
|
|
|
logging.info("根据禁用列表忽略群{}的消息".format(launcher_id))
|
|
|
|
|
return []
|
|
|
|
|
|
2023-01-09 21:15:13 +08:00
|
|
|
|
# 检查是否被禁言
|
|
|
|
|
if launcher_type == 'group':
|
|
|
|
|
result = mgr.bot.member_info(target=launcher_id, member_id=mgr.bot.qq).get()
|
|
|
|
|
result = asyncio.run(result)
|
|
|
|
|
if result.mute_time_remaining > 0:
|
|
|
|
|
logging.info("机器人被禁言,跳过消息处理(group_{},剩余{}s)".format(launcher_id,
|
|
|
|
|
result.mute_time_remaining))
|
2023-01-01 19:19:04 +08:00
|
|
|
|
return reply
|
2023-01-01 18:27:34 +08:00
|
|
|
|
|
2022-12-26 19:37:25 +08:00
|
|
|
|
pkg.openai.session.get_session(session_name).acquire_response_lock()
|
|
|
|
|
|
2023-01-14 20:34:33 +08:00
|
|
|
|
# 处理消息
|
2022-12-26 19:37:25 +08:00
|
|
|
|
try:
|
|
|
|
|
if session_name in processing:
|
|
|
|
|
pkg.openai.session.get_session(session_name).release_response_lock()
|
2023-01-08 14:49:23 +08:00
|
|
|
|
return MessageChain([Plain("[bot]err:正在处理中,请稍后再试")])
|
2022-12-26 19:37:25 +08:00
|
|
|
|
|
2023-01-04 21:46:01 +08:00
|
|
|
|
config = pkg.utils.context.get_config()
|
|
|
|
|
|
2023-01-14 20:34:33 +08:00
|
|
|
|
processing.append(session_name)
|
2022-12-26 19:37:25 +08:00
|
|
|
|
try:
|
|
|
|
|
|
|
|
|
|
if text_message.startswith('!') or text_message.startswith("!"): # 指令
|
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,
|
|
|
|
|
'is_admin': sender_id is config.admin_qq,
|
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
|
if not event.is_prevented_default():
|
|
|
|
|
reply = pkg.qqbot.command.process_command(session_name, text_message,
|
|
|
|
|
mgr, config, launcher_type, launcher_id)
|
2023-01-03 00:02:18 +08:00
|
|
|
|
|
2023-01-14 20:34:33 +08:00
|
|
|
|
else: # 消息
|
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
|
|
|
|
|
|
|
|
|
if not event.is_prevented_default():
|
|
|
|
|
reply = pkg.qqbot.message.process_normal_message(text_message,
|
|
|
|
|
mgr, config, launcher_type, launcher_id)
|
2022-12-26 19:37:25 +08:00
|
|
|
|
|
2022-12-26 23:53:56 +08:00
|
|
|
|
if reply is not None and type(reply[0]) == str:
|
|
|
|
|
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 "")))
|
2022-12-26 23:53:56 +08:00
|
|
|
|
reply = [mgr.reply_filter.process(reply[0])]
|
2022-12-27 23:01:04 +08:00
|
|
|
|
else:
|
|
|
|
|
logging.info("回复[{}]图片消息:{}".format(session_name, reply))
|
2022-12-26 19:37:25 +08:00
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
processing.remove(session_name)
|
|
|
|
|
finally:
|
|
|
|
|
pkg.openai.session.get_session(session_name).release_response_lock()
|
|
|
|
|
|
2023-01-01 17:20:54 +08:00
|
|
|
|
return MessageChain(reply)
|