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
|
2024-01-30 14:58:34 +08:00
|
|
|
|
import traceback
|
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
|
2024-01-28 19:20:10 +08:00
|
|
|
|
from ..platform import adapter as msadapter
|
2023-04-21 17:51:58 +08:00
|
|
|
|
|
2024-01-26 15:51:49 +08:00
|
|
|
|
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
|
|
|
|
|
2023-03-06 17:50:34 +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
|
|
|
|
|
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
|
|
|
|
|
2024-01-24 23:38:13 +08:00
|
|
|
|
# modern
|
|
|
|
|
ap: app.Application = None
|
|
|
|
|
|
2024-01-28 18:38:47 +08:00
|
|
|
|
def __init__(self, ap: app.Application = None):
|
2023-04-21 17:15:32 +08:00
|
|
|
|
|
2024-01-24 23:38:13 +08:00
|
|
|
|
self.ap = ap
|
|
|
|
|
|
|
|
|
|
async def initialize(self):
|
|
|
|
|
|
2024-02-06 21:26:03 +08:00
|
|
|
|
if self.ap.platform_cfg.data['platform-adapter'] == 'yiri-mirai':
|
2024-01-28 19:20:10 +08:00
|
|
|
|
from pkg.platform.sources.yirimirai import YiriMiraiAdapter
|
2024-01-24 22:29:19 +08:00
|
|
|
|
|
2024-02-06 21:26:03 +08:00
|
|
|
|
mirai_http_api_config = self.ap.platform_cfg.data['yiri-mirai-config']
|
|
|
|
|
self.bot_account_id = mirai_http_api_config['qq']
|
2024-01-24 22:29:19 +08:00
|
|
|
|
self.adapter = YiriMiraiAdapter(mirai_http_api_config)
|
2024-02-07 20:03:46 +08:00
|
|
|
|
elif self.ap.platform_cfg.data['platform-adapter'] == 'aiocqhttp':
|
|
|
|
|
from pkg.platform.sources.aiocqhttp import AiocqhttpAdapter
|
|
|
|
|
|
|
|
|
|
aiocqhttp_config = self.ap.platform_cfg.data['aiocqhttp-config']
|
|
|
|
|
self.adapter = AiocqhttpAdapter(aiocqhttp_config, self.ap)
|
2024-02-07 23:21:32 +08:00
|
|
|
|
elif self.ap.platform_cfg.data['platform-adapter'] == 'qq-botpy':
|
|
|
|
|
from pkg.platform.sources.qqbotpy import OfficialAdapter
|
|
|
|
|
|
|
|
|
|
qqbotpy_config = self.ap.platform_cfg.data['qq-botpy-config']
|
|
|
|
|
self.adapter = OfficialAdapter(qqbotpy_config, self.ap)
|
2023-01-02 00:35:36 +08:00
|
|
|
|
|
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-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-26 15:51:49 +08:00
|
|
|
|
)
|
2023-01-15 00:04:47 +08:00
|
|
|
|
|
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
|
|
|
|
|
)
|
|
|
|
|
|
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):
|
2024-01-26 15:51:49 +08:00
|
|
|
|
|
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-26 15:51:49 +08:00
|
|
|
|
)
|
2023-01-14 19:59:51 +08:00
|
|
|
|
|
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,
|
2024-02-01 15:48:26 +08:00
|
|
|
|
message_chain=event.message_chain,
|
2024-01-30 21:45:17 +08:00
|
|
|
|
)
|
|
|
|
|
|
2023-04-23 23:40:08 +08:00
|
|
|
|
# nakuru不区分好友和陌生人,故仅为yirimirai注册陌生人事件
|
2024-02-06 21:26:03 +08:00
|
|
|
|
if self.ap.platform_cfg.data['platform-adapter'] == 'yiri-mirai':
|
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-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-26 15:51:49 +08:00
|
|
|
|
)
|
2024-01-25 17:05:09 +08:00
|
|
|
|
|
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
|
|
|
|
|
)
|
|
|
|
|
|
2023-04-21 17:51:58 +08:00
|
|
|
|
self.adapter.register_listener(
|
|
|
|
|
GroupMessage,
|
|
|
|
|
on_group_message
|
|
|
|
|
)
|
2023-01-02 11:33:26 +08:00
|
|
|
|
|
2024-01-23 22:28:30 +08:00
|
|
|
|
async def send(self, event, msg, check_quote=True, check_at_sender=True):
|
2023-08-01 10:13:15 +08:00
|
|
|
|
|
2024-02-06 23:57:21 +08:00
|
|
|
|
if check_at_sender and self.ap.platform_cfg.data['at-sender'] and isinstance(event, GroupMessage):
|
2024-02-07 23:21:32 +08:00
|
|
|
|
|
2024-02-06 21:26:03 +08:00
|
|
|
|
msg.insert(
|
|
|
|
|
0,
|
|
|
|
|
At(
|
|
|
|
|
event.sender.id
|
2023-08-01 10:13:15 +08:00
|
|
|
|
)
|
2024-02-06 21:26:03 +08:00
|
|
|
|
)
|
2023-08-01 10:13:15 +08:00
|
|
|
|
|
2024-01-23 22:28:30 +08:00
|
|
|
|
await self.adapter.reply_message(
|
2023-04-21 17:51:58 +08:00
|
|
|
|
event,
|
|
|
|
|
msg,
|
2024-02-06 21:26:03 +08:00
|
|
|
|
quote_origin=True if self.ap.platform_cfg.data['quote-origin'] and check_quote else False
|
2023-04-21 17:51:58 +08:00
|
|
|
|
)
|
2022-12-12 22:04:38 +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):
|
2024-02-06 21:26:03 +08:00
|
|
|
|
if self.ap.system_cfg.data['admin-sessions'] != []:
|
2024-01-24 17:00:56 +08:00
|
|
|
|
|
|
|
|
|
admin_list = []
|
2024-02-06 21:26:03 +08:00
|
|
|
|
for admin in self.ap.system_cfg.data['admin-sessions']:
|
|
|
|
|
admin_list.append(admin)
|
2024-01-24 17:00:56 +08:00
|
|
|
|
|
|
|
|
|
for adm in admin_list:
|
2023-04-21 17:51:58 +08:00
|
|
|
|
self.adapter.send_message(
|
2024-02-06 21:26:03 +08:00
|
|
|
|
adm.split("_")[0],
|
|
|
|
|
adm.split("_")[1],
|
2023-04-21 17:51:58 +08:00
|
|
|
|
message
|
|
|
|
|
)
|
2024-01-23 22:28:30 +08:00
|
|
|
|
|
|
|
|
|
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()}")
|