2024-01-23 20:55:20 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-12-11 17:17:33 +08:00
|
|
|
import json
|
|
|
|
import os
|
2024-09-26 14:38:18 +08:00
|
|
|
import sys
|
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
|
|
|
|
2024-09-26 00:23:03 +08:00
|
|
|
# FriendMessage, Image, MessageChain, Plain
|
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-09-26 00:23:03 +08:00
|
|
|
from .types import message as platform_message
|
|
|
|
from .types import events as platform_events
|
|
|
|
from .types import entities as platform_entities
|
|
|
|
|
2024-09-26 14:38:18 +08:00
|
|
|
# 处理 3.4 移除了 YiriMirai 之后,插件的兼容性问题
|
|
|
|
from . import types as mirai
|
|
|
|
sys.modules['mirai'] = mirai
|
|
|
|
|
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
|
|
|
|
2024-02-11 23:07:38 +08:00
|
|
|
# adapter: msadapter.MessageSourceAdapter = None
|
|
|
|
adapters: list[msadapter.MessageSourceAdapter] = []
|
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
|
2024-02-11 23:07:38 +08:00
|
|
|
self.adapters = []
|
2024-01-24 23:38:13 +08:00
|
|
|
|
|
|
|
async def initialize(self):
|
|
|
|
|
2024-02-08 13:12:33 +08:00
|
|
|
from .sources import yirimirai, nakuru, aiocqhttp, qqbotpy
|
2024-01-24 22:29:19 +08:00
|
|
|
|
2024-09-26 00:23:03 +08:00
|
|
|
async def on_friend_message(event: platform_events.FriendMessage, adapter: msadapter.MessageSourceAdapter):
|
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,
|
2024-02-11 23:07:38 +08:00
|
|
|
message_chain=event.message_chain,
|
|
|
|
adapter=adapter
|
2024-01-30 21:45:17 +08:00
|
|
|
)
|
|
|
|
|
2024-09-26 00:23:03 +08:00
|
|
|
async def on_stranger_message(event: platform_events.StrangerMessage, adapter: msadapter.MessageSourceAdapter):
|
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-02-11 23:07:38 +08:00
|
|
|
adapter=adapter
|
2024-01-30 21:45:17 +08:00
|
|
|
)
|
|
|
|
|
2024-09-26 00:23:03 +08:00
|
|
|
async def on_group_message(event: platform_events.GroupMessage, adapter: msadapter.MessageSourceAdapter):
|
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(
|
2024-04-04 16:50:45 +08:00
|
|
|
launcher_type='group',
|
|
|
|
launcher_id=event.group.id,
|
2024-01-30 21:45:17 +08:00
|
|
|
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,
|
2024-02-11 23:07:38 +08:00
|
|
|
message_chain=event.message_chain,
|
|
|
|
adapter=adapter
|
2024-01-30 21:45:17 +08:00
|
|
|
)
|
2024-02-11 23:07:38 +08:00
|
|
|
|
2024-02-12 13:37:41 +08:00
|
|
|
index = 0
|
|
|
|
|
2024-02-11 23:07:38 +08:00
|
|
|
for adap_cfg in self.ap.platform_cfg.data['platform-adapters']:
|
|
|
|
if adap_cfg['enable']:
|
2024-02-12 13:37:41 +08:00
|
|
|
self.ap.logger.info(f'初始化平台适配器 {index}: {adap_cfg["adapter"]}')
|
|
|
|
index += 1
|
2024-02-11 23:07:38 +08:00
|
|
|
cfg_copy = adap_cfg.copy()
|
|
|
|
del cfg_copy['enable']
|
|
|
|
adapter_name = cfg_copy['adapter']
|
|
|
|
del cfg_copy['adapter']
|
|
|
|
|
|
|
|
found = False
|
|
|
|
|
|
|
|
for adapter in msadapter.preregistered_adapters:
|
|
|
|
if adapter.name == adapter_name:
|
|
|
|
found = True
|
|
|
|
adapter_cls = adapter
|
|
|
|
|
|
|
|
adapter_inst = adapter_cls(
|
|
|
|
cfg_copy,
|
|
|
|
self.ap
|
|
|
|
)
|
|
|
|
self.adapters.append(adapter_inst)
|
|
|
|
|
|
|
|
if adapter_name == 'yiri-mirai':
|
|
|
|
adapter_inst.register_listener(
|
2024-09-26 00:23:03 +08:00
|
|
|
platform_events.StrangerMessage,
|
2024-02-11 23:07:38 +08:00
|
|
|
on_stranger_message
|
|
|
|
)
|
|
|
|
|
|
|
|
adapter_inst.register_listener(
|
2024-09-26 00:23:03 +08:00
|
|
|
platform_events.FriendMessage,
|
2024-02-11 23:07:38 +08:00
|
|
|
on_friend_message
|
|
|
|
)
|
|
|
|
adapter_inst.register_listener(
|
2024-09-26 00:23:03 +08:00
|
|
|
platform_events.GroupMessage,
|
2024-02-11 23:07:38 +08:00
|
|
|
on_group_message
|
|
|
|
)
|
|
|
|
|
|
|
|
if not found:
|
|
|
|
raise Exception('platform.json 中启用了未知的平台适配器: ' + adapter_name)
|
2024-02-15 16:12:42 +08:00
|
|
|
|
|
|
|
if len(self.adapters) == 0:
|
|
|
|
self.ap.logger.warning('未运行平台适配器,请根据文档配置并启用平台适配器。')
|
2024-02-11 23:07:38 +08:00
|
|
|
|
2024-09-26 00:23:03 +08:00
|
|
|
async def send(self, event: platform_events.MessageEvent, msg: platform_message.MessageChain, adapter: msadapter.MessageSourceAdapter):
|
2023-08-01 10:13:15 +08:00
|
|
|
|
2024-09-26 00:23:03 +08:00
|
|
|
if self.ap.platform_cfg.data['at-sender'] and isinstance(event, platform_events.GroupMessage):
|
2024-02-07 23:21:32 +08:00
|
|
|
|
2024-02-06 21:26:03 +08:00
|
|
|
msg.insert(
|
|
|
|
0,
|
2024-09-26 00:23:03 +08:00
|
|
|
platform_message.At(
|
2024-02-06 21:26:03 +08:00
|
|
|
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-02-11 23:07:38 +08:00
|
|
|
await adapter.reply_message(
|
2023-04-21 17:51:58 +08:00
|
|
|
event,
|
|
|
|
msg,
|
2024-07-28 20:23:52 +08:00
|
|
|
quote_origin=True if self.ap.platform_cfg.data['quote-origin'] else False
|
2023-04-21 17:51:58 +08:00
|
|
|
)
|
2022-12-12 22:04:38 +08:00
|
|
|
|
2024-01-23 22:28:30 +08:00
|
|
|
async def run(self):
|
2024-01-30 14:58:34 +08:00
|
|
|
try:
|
2024-02-11 23:07:38 +08:00
|
|
|
tasks = []
|
|
|
|
for adapter in self.adapters:
|
2024-02-12 13:37:41 +08:00
|
|
|
async def exception_wrapper(adapter):
|
|
|
|
try:
|
|
|
|
await adapter.run_async()
|
|
|
|
except Exception as e:
|
|
|
|
self.ap.logger.error('平台适配器运行出错: ' + str(e))
|
|
|
|
self.ap.logger.debug(f"Traceback: {traceback.format_exc()}")
|
|
|
|
|
|
|
|
tasks.append(exception_wrapper(adapter))
|
2024-02-11 23:35:05 +08:00
|
|
|
|
|
|
|
for task in tasks:
|
2024-10-22 18:09:18 +08:00
|
|
|
# async_task = asyncio.create_task(task)
|
|
|
|
# self.ap.asyncio_tasks.append(async_task)
|
2024-11-01 22:41:26 +08:00
|
|
|
self.ap.task_mgr.create_task(
|
|
|
|
task,
|
|
|
|
kind="platform-adapter",
|
|
|
|
name=f"platform-adapter-{adapter.name}",
|
|
|
|
)
|
2024-02-11 23:35:05 +08:00
|
|
|
|
2024-01-30 14:58:34 +08:00
|
|
|
except Exception as e:
|
|
|
|
self.ap.logger.error('平台适配器运行出错: ' + str(e))
|
|
|
|
self.ap.logger.debug(f"Traceback: {traceback.format_exc()}")
|