fix: 修复 event loop closed

This commit is contained in:
Soulter
2024-12-15 13:03:00 +08:00
parent 91cc21e729
commit 8c03c90708
5 changed files with 18 additions and 14 deletions
+6
View File
@@ -18,6 +18,7 @@ DEFAULT_CONFIG = {
"reply_prefix": "", "reply_prefix": "",
"forward_threshold": 200, "forward_threshold": 200,
"id_whitelist": [], "id_whitelist": [],
"id_whitelist_log": True,
"wl_ignore_admin_on_group": True, "wl_ignore_admin_on_group": True,
"wl_ignore_admin_on_friend": True, "wl_ignore_admin_on_friend": True,
}, },
@@ -167,6 +168,11 @@ CONFIG_METADATA_2 = {
"items": {"type": "int"}, "items": {"type": "int"},
"hint": "填写后,将只处理所填写的 ID 发来的消息事件。为空时表示不启用白名单过滤。可以使用 /myid 指令获取在某个平台上的会话 ID。也可在 AstrBot 日志内获取会话 ID,当一条消息没通过白名单时,会输出 INFO 级别的日志。会话 ID 类似 aiocqhttp:GroupMessage:547540978", "hint": "填写后,将只处理所填写的 ID 发来的消息事件。为空时表示不启用白名单过滤。可以使用 /myid 指令获取在某个平台上的会话 ID。也可在 AstrBot 日志内获取会话 ID,当一条消息没通过白名单时,会输出 INFO 级别的日志。会话 ID 类似 aiocqhttp:GroupMessage:547540978",
}, },
"id_whitelist_log": {
"description": "打印白名单日志",
"type": "bool",
"hint": "启用后,当一条消息没通过白名单时,会输出 INFO 级别的日志。",
},
"wl_ignore_admin_on_group": { "wl_ignore_admin_on_group": {
"description": "管理员群组消息无视 ID 白名单", "description": "管理员群组消息无视 ID 白名单",
"type": "bool", "type": "bool",
@@ -13,7 +13,8 @@ class WhitelistCheckStage(Stage):
self.whitelist = ctx.astrbot_config['platform_settings']['id_whitelist'] self.whitelist = ctx.astrbot_config['platform_settings']['id_whitelist']
self.wl_ignore_admin_on_group = ctx.astrbot_config['platform_settings']['wl_ignore_admin_on_group'] self.wl_ignore_admin_on_group = ctx.astrbot_config['platform_settings']['wl_ignore_admin_on_group']
self.wl_ignore_admin_on_friend = ctx.astrbot_config['platform_settings']['wl_ignore_admin_on_friend'] self.wl_ignore_admin_on_friend = ctx.astrbot_config['platform_settings']['wl_ignore_admin_on_friend']
self.wl_log = ctx.astrbot_config['platform_settings']['id_whitelist_log']
async def process(self, event: AstrMessageEvent) -> Union[None, AsyncGenerator[None, None]]: async def process(self, event: AstrMessageEvent) -> Union[None, AsyncGenerator[None, None]]:
# 检查是否在白名单 # 检查是否在白名单
if self.wl_ignore_admin_on_group: if self.wl_ignore_admin_on_group:
@@ -23,5 +24,6 @@ class WhitelistCheckStage(Stage):
if event.role == 'admin' and event.get_message_type() == MessageType.FRIEND_MESSAGE: if event.role == 'admin' and event.get_message_type() == MessageType.FRIEND_MESSAGE:
return return
if event.unified_msg_origin not in self.whitelist: if event.unified_msg_origin not in self.whitelist:
logger.info(f"会话 {event.unified_msg_origin} 不在会话白名单中,已终止事件传播。") if self.wl_log:
logger.info(f"会话 ID {event.unified_msg_origin} 不在会话白名单中,已终止事件传播。请在配置文件中添加该会话 ID 到白名单。")
event.stop_event() event.stop_event()
@@ -63,8 +63,6 @@ class QQOfficialPlatformAdapter(Platform):
def __init__(self, platform_config: dict, platform_settings: dict, event_queue: asyncio.Queue) -> None: def __init__(self, platform_config: dict, platform_settings: dict, event_queue: asyncio.Queue) -> None:
super().__init__(event_queue) super().__init__(event_queue)
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
self.config = platform_config self.config = platform_config
+6 -4
View File
@@ -3,17 +3,19 @@ from astrbot.core import logger
from astrbot.core.core_lifecycle import AstrBotCoreLifecycle from astrbot.core.core_lifecycle import AstrBotCoreLifecycle
from .server import AstrBotDashboard from .server import AstrBotDashboard
from astrbot.core.db import BaseDatabase from astrbot.core.db import BaseDatabase
from astrbot.core import LogBroker
class AstrBotDashBoardLifecycle: class AstrBotDashBoardLifecycle:
def __init__(self, db: BaseDatabase): def __init__(self, db: BaseDatabase, log_broker: LogBroker):
self.db = db self.db = db
self.logger = logger self.logger = logger
self.log_broker = log_broker
self.dashboard_server = None self.dashboard_server = None
async def start(self, core_lifecycle: AstrBotCoreLifecycle): async def start(self):
core_lifecycle = AstrBotCoreLifecycle(self.log_broker, self.db)
await core_lifecycle.initialize()
core_task = core_lifecycle.start() core_task = core_lifecycle.start()
self.dashboard_server = AstrBotDashboard(core_lifecycle, self.db) self.dashboard_server = AstrBotDashboard(core_lifecycle, self.db)
task = asyncio.gather(core_task, self.dashboard_server.run()) task = asyncio.gather(core_task, self.dashboard_server.run())
try: try:
+2 -6
View File
@@ -5,7 +5,6 @@ import sys
import mimetypes import mimetypes
import aiohttp import aiohttp
import zipfile import zipfile
from astrbot.core.core_lifecycle import AstrBotCoreLifecycle
from astrbot.dashboard import AstrBotDashBoardLifecycle from astrbot.dashboard import AstrBotDashBoardLifecycle
from astrbot.core import db_helper from astrbot.core import db_helper
from astrbot.core import logger, LogManager, LogBroker from astrbot.core import logger, LogManager, LogBroker
@@ -94,8 +93,5 @@ if __name__ == "__main__":
# print logo # print logo
logger.info(logo_tmpl) logger.info(logo_tmpl)
core_lifecycle = AstrBotCoreLifecycle(log_broker, db) dashboard_lifecycle = AstrBotDashBoardLifecycle(db, log_broker)
asyncio.run(core_lifecycle.initialize()) asyncio.run(dashboard_lifecycle.start())
dashboard_lifecycle = AstrBotDashBoardLifecycle(db)
asyncio.run(dashboard_lifecycle.start(core_lifecycle))