QChatGPT/pkg/platform/manager.py

186 lines
6.2 KiB
Python
Raw Normal View History

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
import asyncio
2024-01-30 14:58:34 +08:00
import traceback
2023-11-26 23:04:14 +08:00
from mirai import At, GroupMessage, MessageEvent, StrangerMessage, \
FriendMessage, Image, MessageChain, Plain
import mirai
2024-01-28 19:20:10 +08:00
from ..platform import adapter as msadapter
from ..core import app, entities as core_entities
2024-01-30 21:45:17 +08:00
from ..plugin import events
2024-01-23 20:55:20 +08:00
2022-12-11 16:10:12 +08:00
# 控制QQ消息输入输出的类
2024-01-30 21:45:17 +08:00
class PlatformManager:
2024-01-28 18:38:47 +08:00
adapter: msadapter.MessageSourceAdapter = None
bot_account_id: int = 0
2022-12-07 23:37:52 +08:00
# modern
ap: app.Application = None
2024-01-28 18:38:47 +08:00
def __init__(self, ap: app.Application = None):
self.ap = ap
async def initialize(self):
2024-01-29 21:22:27 +08:00
config = self.ap.cfg_mgr.data
logging.debug("Use adapter:" + config['msg_source_adapter'])
if config['msg_source_adapter'] == 'yirimirai':
2024-01-28 19:20:10 +08:00
from pkg.platform.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':
2024-01-28 19:20:10 +08:00
from pkg.platform.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 到审计模块
2024-01-29 21:58:47 +08:00
from ..audit.center import apigroup
2023-12-22 12:38:27 +08:00
apigroup.APIGroup._runtime_info['account_id'] = "{}".format(self.bot_account_id)
async def on_friend_message(event: FriendMessage):
2024-01-30 21:45:17 +08:00
event_ctx = await self.ap.plugin_mgr.emit_event(
event=events.PersonMessageReceived(
launcher_type='person',
launcher_id=event.sender.id,
sender_id=event.sender.id,
message_chain=event.message_chain,
query=None
)
)
2024-01-30 21:45:17 +08:00
if not event_ctx.is_prevented_default():
await self.ap.query_pool.add_query(
launcher_type=core_entities.LauncherTypes.PERSON,
launcher_id=event.sender.id,
sender_id=event.sender.id,
message_event=event,
message_chain=event.message_chain
)
self.adapter.register_listener(
FriendMessage,
on_friend_message
)
async def on_stranger_message(event: StrangerMessage):
2024-01-30 21:45:17 +08:00
event_ctx = await self.ap.plugin_mgr.emit_event(
event=events.PersonMessageReceived(
launcher_type='person',
launcher_id=event.sender.id,
sender_id=event.sender.id,
message_chain=event.message_chain,
query=None
)
)
2024-01-30 21:45:17 +08:00
if not event_ctx.is_prevented_default():
await self.ap.query_pool.add_query(
launcher_type=core_entities.LauncherTypes.PERSON,
launcher_id=event.sender.id,
sender_id=event.sender.id,
message_event=event,
message_chain=event.message_chain,
2024-01-30 21:45:17 +08:00
)
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
)
async def on_group_message(event: GroupMessage):
2024-01-30 21:45:17 +08:00
event_ctx = await self.ap.plugin_mgr.emit_event(
event=events.GroupMessageReceived(
launcher_type='person',
launcher_id=event.sender.id,
sender_id=event.sender.id,
message_chain=event.message_chain,
query=None
)
)
2024-01-30 21:45:17 +08:00
if not event_ctx.is_prevented_default():
await self.ap.query_pool.add_query(
launcher_type=core_entities.LauncherTypes.GROUP,
launcher_id=event.group.id,
sender_id=event.sender.id,
message_event=event,
message_chain=event.message_chain
)
self.adapter.register_listener(
GroupMessage,
on_group_message
)
async def send(self, event, msg, check_quote=True, check_at_sender=True):
2024-01-29 21:22:27 +08:00
config = self.ap.cfg_mgr.data
2023-11-26 23:04:14 +08:00
if check_at_sender and config['at_sender']:
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':
msg.insert(
0,
At(
event.sender.id
)
)
await self.adapter.reply_message(
event,
msg,
2023-11-26 23:04:14 +08:00
quote_origin=True if config['quote_origin'] and check_quote else False
)
2022-12-11 16:10:12 +08:00
# 通知系统管理员
async def notify_admin(self, message: str):
await self.notify_admin_message_chain(MessageChain([Plain("[bot]{}".format(message))]))
2023-02-10 19:03:25 +08:00
async def notify_admin_message_chain(self, message: mirai.MessageChain):
2024-01-29 21:22:27 +08:00
config = self.ap.cfg_mgr.data
2023-11-26 23:04:14 +08:00
if config['admin_qq'] != 0 and config['admin_qq'] != []:
2023-02-10 19:03:25 +08:00
logging.info("通知管理员:{}".format(message))
admin_list = []
2023-11-26 23:04:14 +08:00
if type(config['admin_qq']) == int:
admin_list.append(config['admin_qq'])
for adm in admin_list:
self.adapter.send_message(
"person",
adm,
message
)
async def run(self):
2024-01-30 14:58:34 +08:00
try:
await self.adapter.run_async()
except Exception as e:
self.ap.logger.error('平台适配器运行出错: ' + str(e))
self.ap.logger.debug(f"Traceback: {traceback.format_exc()}")