2024-01-23 20:55:20 +08:00
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2022-12-11 17:17:33 +08:00
|
|
|
|
import json
|
|
|
|
|
import os
|
2023-11-13 21:59:23 +08:00
|
|
|
|
import logging
|
2024-01-23 22:28:30 +08:00
|
|
|
|
import asyncio
|
2023-04-21 17:51:58 +08:00
|
|
|
|
|
2023-11-26 23:04:14 +08:00
|
|
|
|
from mirai import At, GroupMessage, MessageEvent, StrangerMessage, \
|
2023-04-21 17:51:58 +08:00
|
|
|
|
FriendMessage, Image, MessageChain, Plain
|
2024-01-24 17:00:56 +08:00
|
|
|
|
import mirai
|
2023-11-13 21:59:23 +08:00
|
|
|
|
import func_timeout
|
2022-12-09 16:18:25 +08:00
|
|
|
|
|
2023-11-13 21:59:23 +08:00
|
|
|
|
from ..openai import session as openai_session
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
2023-11-13 21:59:23 +08:00
|
|
|
|
from ..qqbot import process as processor
|
|
|
|
|
from ..utils import context
|
|
|
|
|
from ..plugin import host as plugin_host
|
|
|
|
|
from ..plugin import models as plugin_models
|
2023-04-07 13:20:57 +08:00
|
|
|
|
import tips as tips_custom
|
2023-11-13 21:59:23 +08:00
|
|
|
|
from ..qqbot import adapter as msadapter
|
2024-01-24 16:11:56 +08:00
|
|
|
|
from . import resprule
|
2024-01-24 23:38:13 +08:00
|
|
|
|
from .bansess import bansess
|
2024-01-25 15:28:23 +08:00
|
|
|
|
from .cntfilter import cntfilter
|
2023-04-21 17:51:58 +08:00
|
|
|
|
|
2024-01-23 20:55:20 +08:00
|
|
|
|
from ..boot import app
|
|
|
|
|
|
2023-03-06 17:50:34 +08:00
|
|
|
|
|
2022-12-11 16:10:12 +08:00
|
|
|
|
# 控制QQ消息输入输出的类
|
2022-12-07 23:37:52 +08:00
|
|
|
|
class QQBotManager:
|
|
|
|
|
retry = 3
|
|
|
|
|
|
2023-04-21 17:51:58 +08:00
|
|
|
|
adapter: msadapter.MessageSourceAdapter = None
|
|
|
|
|
|
|
|
|
|
bot_account_id: int = 0
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
2023-01-07 16:50:34 +08:00
|
|
|
|
enable_banlist = False
|
|
|
|
|
|
2023-06-07 23:47:13 +08:00
|
|
|
|
enable_private = True
|
|
|
|
|
enable_group = True
|
|
|
|
|
|
2023-01-07 16:50:34 +08:00
|
|
|
|
ban_person = []
|
|
|
|
|
ban_group = []
|
|
|
|
|
|
2024-01-24 23:38:13 +08:00
|
|
|
|
# modern
|
|
|
|
|
ap: app.Application = None
|
|
|
|
|
|
|
|
|
|
bansess_mgr: bansess.SessionBanManager = None
|
2024-01-25 15:28:23 +08:00
|
|
|
|
cntfilter_mgr: cntfilter.ContentFilterManager = None
|
2024-01-24 23:38:13 +08:00
|
|
|
|
|
2024-01-23 20:55:20 +08:00
|
|
|
|
def __init__(self, first_time_init=True, ap: app.Application = None):
|
2023-11-26 23:04:14 +08:00
|
|
|
|
config = context.get_config_manager().data
|
2023-04-21 17:15:32 +08:00
|
|
|
|
|
2024-01-24 23:38:13 +08:00
|
|
|
|
self.ap = ap
|
|
|
|
|
self.bansess_mgr = bansess.SessionBanManager(ap)
|
2024-01-25 15:28:23 +08:00
|
|
|
|
self.cntfilter_mgr = cntfilter.ContentFilterManager(ap)
|
2024-01-24 23:38:13 +08:00
|
|
|
|
|
2023-11-26 23:04:14 +08:00
|
|
|
|
self.timeout = config['process_message_timeout']
|
|
|
|
|
self.retry = config['retry_times']
|
2024-01-24 23:38:13 +08:00
|
|
|
|
|
|
|
|
|
async def initialize(self):
|
|
|
|
|
await self.bansess_mgr.initialize()
|
2024-01-25 15:28:23 +08:00
|
|
|
|
await self.cntfilter_mgr.initialize()
|
2024-01-24 23:38:13 +08:00
|
|
|
|
|
|
|
|
|
config = context.get_config_manager().data
|
2023-03-06 08:50:28 +08:00
|
|
|
|
|
2024-01-24 22:29:19 +08:00
|
|
|
|
logging.debug("Use adapter:" + config['msg_source_adapter'])
|
|
|
|
|
if config['msg_source_adapter'] == 'yirimirai':
|
|
|
|
|
from pkg.qqbot.sources.yirimirai import YiriMiraiAdapter
|
|
|
|
|
|
|
|
|
|
mirai_http_api_config = config['mirai_http_api_config']
|
|
|
|
|
self.bot_account_id = config['mirai_http_api_config']['qq']
|
|
|
|
|
self.adapter = YiriMiraiAdapter(mirai_http_api_config)
|
|
|
|
|
elif config['msg_source_adapter'] == 'nakuru':
|
|
|
|
|
from pkg.qqbot.sources.nakuru import NakuruProjectAdapter
|
|
|
|
|
self.adapter = NakuruProjectAdapter(config['nakuru_config'])
|
|
|
|
|
self.bot_account_id = self.adapter.bot_account_id
|
2023-12-21 18:51:10 +08:00
|
|
|
|
|
|
|
|
|
# 保存 account_id 到审计模块
|
|
|
|
|
from ..utils.center import apigroup
|
2023-12-22 12:38:27 +08:00
|
|
|
|
apigroup.APIGroup._runtime_info['account_id'] = "{}".format(self.bot_account_id)
|
2023-01-02 00:35:36 +08:00
|
|
|
|
|
2023-11-13 21:59:23 +08:00
|
|
|
|
context.set_qqbot_manager(self)
|
2023-01-02 00:35:36 +08:00
|
|
|
|
|
2023-04-21 17:51:58 +08:00
|
|
|
|
# 注册诸事件
|
2023-01-02 11:33:26 +08:00
|
|
|
|
# Caution: 注册新的事件处理器之后,请务必在unsubscribe_all中编写相应的取消订阅代码
|
2024-01-23 22:28:30 +08:00
|
|
|
|
async def on_friend_message(event: FriendMessage):
|
2023-01-14 19:59:51 +08:00
|
|
|
|
|
2024-01-23 22:28:30 +08:00
|
|
|
|
async def friend_message_handler():
|
2023-01-15 00:04:47 +08:00
|
|
|
|
# 触发事件
|
|
|
|
|
args = {
|
|
|
|
|
"launcher_type": "person",
|
|
|
|
|
"launcher_id": event.sender.id,
|
|
|
|
|
"sender_id": event.sender.id,
|
|
|
|
|
"message_chain": event.message_chain,
|
|
|
|
|
}
|
|
|
|
|
plugin_event = plugin_host.emit(plugin_models.PersonMessageReceived, **args)
|
|
|
|
|
|
|
|
|
|
if plugin_event.is_prevented_default():
|
|
|
|
|
return
|
|
|
|
|
|
2024-01-23 22:28:30 +08:00
|
|
|
|
await self.on_person_message(event)
|
2023-01-15 00:04:47 +08:00
|
|
|
|
|
2024-01-23 22:28:30 +08:00
|
|
|
|
asyncio.create_task(friend_message_handler())
|
2023-04-21 17:51:58 +08:00
|
|
|
|
self.adapter.register_listener(
|
|
|
|
|
FriendMessage,
|
|
|
|
|
on_friend_message
|
|
|
|
|
)
|
2023-01-02 11:33:26 +08:00
|
|
|
|
|
2024-01-23 22:28:30 +08:00
|
|
|
|
async def on_stranger_message(event: StrangerMessage):
|
2023-01-14 19:59:51 +08:00
|
|
|
|
|
2024-01-23 22:28:30 +08:00
|
|
|
|
async def stranger_message_handler():
|
2023-01-15 00:04:47 +08:00
|
|
|
|
# 触发事件
|
|
|
|
|
args = {
|
|
|
|
|
"launcher_type": "person",
|
|
|
|
|
"launcher_id": event.sender.id,
|
|
|
|
|
"sender_id": event.sender.id,
|
|
|
|
|
"message_chain": event.message_chain,
|
|
|
|
|
}
|
|
|
|
|
plugin_event = plugin_host.emit(plugin_models.PersonMessageReceived, **args)
|
|
|
|
|
|
|
|
|
|
if plugin_event.is_prevented_default():
|
|
|
|
|
return
|
|
|
|
|
|
2024-01-23 22:28:30 +08:00
|
|
|
|
await self.on_person_message(event)
|
2023-01-14 19:59:51 +08:00
|
|
|
|
|
2024-01-23 22:28:30 +08:00
|
|
|
|
asyncio.create_task(stranger_message_handler())
|
2023-04-23 23:40:08 +08:00
|
|
|
|
# nakuru不区分好友和陌生人,故仅为yirimirai注册陌生人事件
|
2023-11-26 23:04:14 +08:00
|
|
|
|
if config['msg_source_adapter'] == 'yirimirai':
|
2023-04-23 23:40:08 +08:00
|
|
|
|
self.adapter.register_listener(
|
|
|
|
|
StrangerMessage,
|
|
|
|
|
on_stranger_message
|
|
|
|
|
)
|
2023-01-02 11:33:26 +08:00
|
|
|
|
|
2024-01-23 22:28:30 +08:00
|
|
|
|
async def on_group_message(event: GroupMessage):
|
2023-01-15 00:04:47 +08:00
|
|
|
|
|
2024-01-23 22:28:30 +08:00
|
|
|
|
async def group_message_handler(event: GroupMessage):
|
2023-01-15 00:04:47 +08:00
|
|
|
|
# 触发事件
|
|
|
|
|
args = {
|
|
|
|
|
"launcher_type": "group",
|
|
|
|
|
"launcher_id": event.group.id,
|
|
|
|
|
"sender_id": event.sender.id,
|
|
|
|
|
"message_chain": event.message_chain,
|
|
|
|
|
}
|
|
|
|
|
plugin_event = plugin_host.emit(plugin_models.GroupMessageReceived, **args)
|
|
|
|
|
|
|
|
|
|
if plugin_event.is_prevented_default():
|
|
|
|
|
return
|
|
|
|
|
|
2024-01-23 22:28:30 +08:00
|
|
|
|
await self.on_group_message(event)
|
2023-01-15 00:04:47 +08:00
|
|
|
|
|
2024-01-23 22:28:30 +08:00
|
|
|
|
asyncio.create_task(group_message_handler(event))
|
2023-04-21 17:51:58 +08:00
|
|
|
|
self.adapter.register_listener(
|
|
|
|
|
GroupMessage,
|
|
|
|
|
on_group_message
|
|
|
|
|
)
|
2023-01-02 11:33:26 +08:00
|
|
|
|
|
|
|
|
|
def unsubscribe_all():
|
2023-01-02 13:06:48 +08:00
|
|
|
|
"""取消所有订阅
|
|
|
|
|
|
|
|
|
|
用于在热重载流程中卸载所有事件处理器
|
|
|
|
|
"""
|
2023-04-21 17:51:58 +08:00
|
|
|
|
self.adapter.unregister_listener(
|
|
|
|
|
FriendMessage,
|
|
|
|
|
on_friend_message
|
|
|
|
|
)
|
2023-11-26 23:04:14 +08:00
|
|
|
|
if config['msg_source_adapter'] == 'yirimirai':
|
2023-04-24 11:21:51 +08:00
|
|
|
|
self.adapter.unregister_listener(
|
|
|
|
|
StrangerMessage,
|
|
|
|
|
on_stranger_message
|
|
|
|
|
)
|
2023-04-21 17:51:58 +08:00
|
|
|
|
self.adapter.unregister_listener(
|
|
|
|
|
GroupMessage,
|
|
|
|
|
on_group_message
|
|
|
|
|
)
|
2023-01-02 11:33:26 +08:00
|
|
|
|
|
|
|
|
|
self.unsubscribe_all = unsubscribe_all
|
|
|
|
|
|
2024-01-23 22:28:30 +08:00
|
|
|
|
async def send(self, event, msg, check_quote=True, check_at_sender=True):
|
2023-11-26 23:04:14 +08:00
|
|
|
|
config = context.get_config_manager().data
|
2023-08-01 10:13:15 +08:00
|
|
|
|
|
2023-11-26 23:04:14 +08:00
|
|
|
|
if check_at_sender and config['at_sender']:
|
2023-08-01 10:13:15 +08:00
|
|
|
|
msg.insert(
|
|
|
|
|
0,
|
|
|
|
|
Plain(" \n")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 当回复的正文中包含换行时,quote可能会自带at,此时就不再单独添加at,只添加换行
|
2023-11-26 23:04:14 +08:00
|
|
|
|
if "\n" not in str(msg[1]) or config['msg_source_adapter'] == 'nakuru':
|
2023-08-01 10:13:15 +08:00
|
|
|
|
msg.insert(
|
|
|
|
|
0,
|
|
|
|
|
At(
|
|
|
|
|
event.sender.id
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2024-01-23 22:28:30 +08:00
|
|
|
|
await self.adapter.reply_message(
|
2023-04-21 17:51:58 +08:00
|
|
|
|
event,
|
|
|
|
|
msg,
|
2023-11-26 23:04:14 +08:00
|
|
|
|
quote_origin=True if config['quote_origin'] and check_quote else False
|
2023-04-21 17:51:58 +08:00
|
|
|
|
)
|
2022-12-12 22:04:38 +08:00
|
|
|
|
|
2024-01-24 17:00:56 +08:00
|
|
|
|
async def common_process(
|
|
|
|
|
self,
|
|
|
|
|
launcher_type: str,
|
|
|
|
|
launcher_id: int,
|
|
|
|
|
text_message: str,
|
|
|
|
|
message_chain: MessageChain,
|
|
|
|
|
sender_id: int
|
|
|
|
|
) -> mirai.MessageChain:
|
|
|
|
|
"""
|
|
|
|
|
私聊群聊通用消息处理方法
|
|
|
|
|
"""
|
2024-01-24 23:38:13 +08:00
|
|
|
|
# 检查bansess
|
|
|
|
|
if await self.bansess_mgr.is_banned(launcher_type, launcher_id, sender_id):
|
|
|
|
|
self.ap.logger.info("根据禁用列表忽略{}_{}的消息".format(launcher_type, launcher_id))
|
|
|
|
|
return []
|
|
|
|
|
|
2024-01-24 17:00:56 +08:00
|
|
|
|
if mirai.Image in message_chain:
|
|
|
|
|
return []
|
|
|
|
|
elif sender_id == self.bot_account_id:
|
|
|
|
|
return []
|
|
|
|
|
else:
|
|
|
|
|
# 超时则重试,重试超过次数则放弃
|
|
|
|
|
failed = 0
|
|
|
|
|
for i in range(self.retry):
|
|
|
|
|
try:
|
|
|
|
|
reply = await processor.process_message(launcher_type, launcher_id, text_message, message_chain,
|
|
|
|
|
sender_id)
|
|
|
|
|
return reply
|
|
|
|
|
|
|
|
|
|
# TODO openai 超时处理
|
|
|
|
|
except func_timeout.FunctionTimedOut:
|
|
|
|
|
logging.warning("{}_{}: 超时,重试中({})".format(launcher_type, launcher_id, i))
|
|
|
|
|
openai_session.get_session("{}_{}".format(launcher_type, launcher_id)).release_response_lock()
|
|
|
|
|
if "{}_{}".format(launcher_type, launcher_id) in processor.processing:
|
|
|
|
|
processor.processing.remove("{}_{}".format(launcher_type, launcher_id))
|
|
|
|
|
failed += 1
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if failed == self.retry:
|
|
|
|
|
openai_session.get_session("{}_{}".format(launcher_type, launcher_id)).release_response_lock()
|
|
|
|
|
await self.notify_admin("{} 请求超时".format("{}_{}".format(launcher_type, launcher_id)))
|
|
|
|
|
reply = [tips_custom.reply_message]
|
|
|
|
|
|
2022-12-11 16:10:12 +08:00
|
|
|
|
# 私聊消息处理
|
2024-01-23 22:28:30 +08:00
|
|
|
|
async def on_person_message(self, event: MessageEvent):
|
2022-12-07 23:37:52 +08:00
|
|
|
|
reply = ''
|
|
|
|
|
|
2023-06-07 23:47:13 +08:00
|
|
|
|
if not self.enable_private:
|
|
|
|
|
logging.debug("已在banlist.py中禁用所有私聊")
|
2024-01-24 17:00:56 +08:00
|
|
|
|
|
2022-12-07 23:37:52 +08:00
|
|
|
|
else:
|
2024-01-24 17:00:56 +08:00
|
|
|
|
reply = await self.common_process(
|
|
|
|
|
launcher_type="person",
|
|
|
|
|
launcher_id=event.sender.id,
|
|
|
|
|
text_message=str(event.message_chain),
|
|
|
|
|
message_chain=event.message_chain,
|
|
|
|
|
sender_id=event.sender.id
|
|
|
|
|
)
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
2022-12-26 23:53:56 +08:00
|
|
|
|
if reply:
|
2024-01-23 22:28:30 +08:00
|
|
|
|
await self.send(event, reply, check_quote=False, check_at_sender=False)
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
2022-12-11 16:10:12 +08:00
|
|
|
|
# 群消息处理
|
2024-01-23 22:28:30 +08:00
|
|
|
|
async def on_group_message(self, event: GroupMessage):
|
2022-12-07 23:37:52 +08:00
|
|
|
|
reply = ''
|
2023-06-07 23:47:13 +08:00
|
|
|
|
|
|
|
|
|
if not self.enable_group:
|
|
|
|
|
logging.debug("已在banlist.py中禁用所有群聊")
|
2024-01-24 17:00:56 +08:00
|
|
|
|
|
2022-12-19 17:07:31 +08:00
|
|
|
|
else:
|
2024-01-24 17:00:56 +08:00
|
|
|
|
do_req = False
|
|
|
|
|
text = str(event.message_chain).strip()
|
2024-01-24 16:11:56 +08:00
|
|
|
|
if At(self.bot_account_id) in event.message_chain and resprule.response_at(event.group.id):
|
2023-03-06 17:50:34 +08:00
|
|
|
|
# 直接调用
|
2024-01-24 17:00:56 +08:00
|
|
|
|
# reply = await process()
|
|
|
|
|
event.message_chain.remove(At(self.bot_account_id))
|
|
|
|
|
text = str(event.message_chain).strip()
|
|
|
|
|
do_req = True
|
2023-03-06 17:50:34 +08:00
|
|
|
|
else:
|
2024-01-24 16:11:56 +08:00
|
|
|
|
check, result = resprule.check_response_rule(event.group.id, str(event.message_chain).strip())
|
2023-03-06 17:50:34 +08:00
|
|
|
|
|
|
|
|
|
if check:
|
2024-01-24 17:00:56 +08:00
|
|
|
|
do_req = True
|
|
|
|
|
text = result.strip()
|
2023-03-06 17:50:34 +08:00
|
|
|
|
# 检查是否随机响应
|
2024-01-24 16:11:56 +08:00
|
|
|
|
elif resprule.random_responding(event.group.id):
|
2023-03-06 17:50:34 +08:00
|
|
|
|
logging.info("随机响应group_{}消息".format(event.group.id))
|
2024-01-24 17:00:56 +08:00
|
|
|
|
# reply = await process()
|
|
|
|
|
do_req = True
|
|
|
|
|
|
|
|
|
|
if do_req:
|
|
|
|
|
reply = await self.common_process(
|
|
|
|
|
launcher_type="group",
|
|
|
|
|
launcher_id=event.group.id,
|
|
|
|
|
text_message=text,
|
|
|
|
|
message_chain=event.message_chain,
|
|
|
|
|
sender_id=event.sender.id
|
|
|
|
|
)
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
2022-12-26 23:53:56 +08:00
|
|
|
|
if reply:
|
2024-01-23 22:28:30 +08:00
|
|
|
|
await self.send(event, reply)
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
2022-12-11 16:10:12 +08:00
|
|
|
|
# 通知系统管理员
|
2024-01-23 22:28:30 +08:00
|
|
|
|
async def notify_admin(self, message: str):
|
2024-01-24 17:00:56 +08:00
|
|
|
|
await self.notify_admin_message_chain(MessageChain([Plain("[bot]{}".format(message))]))
|
2023-02-10 19:03:25 +08:00
|
|
|
|
|
2024-01-24 17:00:56 +08:00
|
|
|
|
async def notify_admin_message_chain(self, message: mirai.MessageChain):
|
2023-11-26 23:04:14 +08:00
|
|
|
|
config = context.get_config_manager().data
|
|
|
|
|
if config['admin_qq'] != 0 and config['admin_qq'] != []:
|
2023-02-10 19:03:25 +08:00
|
|
|
|
logging.info("通知管理员:{}".format(message))
|
2024-01-24 17:00:56 +08:00
|
|
|
|
|
|
|
|
|
admin_list = []
|
|
|
|
|
|
2023-11-26 23:04:14 +08:00
|
|
|
|
if type(config['admin_qq']) == int:
|
2024-01-24 17:00:56 +08:00
|
|
|
|
admin_list.append(config['admin_qq'])
|
|
|
|
|
|
|
|
|
|
for adm in admin_list:
|
2023-04-21 17:51:58 +08:00
|
|
|
|
self.adapter.send_message(
|
|
|
|
|
"person",
|
2024-01-24 17:00:56 +08:00
|
|
|
|
adm,
|
2023-04-21 17:51:58 +08:00
|
|
|
|
message
|
|
|
|
|
)
|
2024-01-23 22:28:30 +08:00
|
|
|
|
|
|
|
|
|
async def run(self):
|
2024-01-24 17:00:56 +08:00
|
|
|
|
await self.adapter.run_async()
|