Skip to content

Commit c60b7de

Browse files
committed
fix: code optimize
1 parent 6f53164 commit c60b7de

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

astrbot/core/star/context.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
from asyncio import Queue
55
from collections.abc import Awaitable, Callable
6+
from dataclasses import dataclass
67
from typing import TYPE_CHECKING, Any, Protocol
78

89
from deprecated import deprecated
@@ -53,13 +54,17 @@ class PlatformManagerProtocol(Protocol):
5354
platform_insts: list[Platform]
5455

5556

57+
@dataclass
58+
class UnifiedWebhook:
59+
handler: Callable[..., Awaitable[Any]]
60+
methods: list[str]
61+
desc: str = ""
62+
63+
5664
class Context:
5765
"""暴露给插件的接口上下文。"""
5866

5967
registered_web_apis: list = []
60-
registered_unified_webhooks: dict[
61-
str, tuple[Callable[..., Awaitable[Any]], list[str], str]
62-
] = {}
6368

6469
# 向后兼容的变量
6570
_register_tasks: list[Awaitable] = []
@@ -80,6 +85,8 @@ def __init__(
8085
cron_manager: CronJobManager,
8186
subagent_orchestrator: SubAgentOrchestrator | None = None,
8287
) -> None:
88+
self.registered_unified_webhooks: dict[str, UnifiedWebhook] = {}
89+
8390
self._event_queue = event_queue
8491
"""事件队列。消息平台通过事件队列传递消息事件。"""
8592
self._config = config
@@ -541,10 +548,10 @@ def register_unified_webhook(
541548
如果相同 webhook_uuid 已注册,会覆盖原有回调。
542549
"""
543550
normalized_methods = [method.upper() for method in (methods or ["GET", "POST"])]
544-
self.registered_unified_webhooks[webhook_uuid] = (
545-
view_handler,
546-
normalized_methods,
547-
desc,
551+
self.registered_unified_webhooks[webhook_uuid] = UnifiedWebhook(
552+
handler=view_handler,
553+
methods=normalized_methods,
554+
desc=desc,
548555
)
549556

550557
"""

astrbot/dashboard/server.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,10 @@ async def srv_plug_webhook_route(self, webhook_uuid, *args, **kwargs):
173173
if not callback:
174174
return jsonify(Response().error("未找到对应 webhook").__dict__), 404
175175

176-
view_handler, methods, _ = callback
177-
if request.method not in methods:
176+
if request.method.upper() not in callback.methods:
178177
return jsonify(Response().error("请求方法不被允许").__dict__), 405
179178

180-
return await view_handler(*args, **kwargs)
179+
return await callback.handler(*args, **kwargs)
181180

182181
async def auth_middleware(self):
183182
if not request.path.startswith("/api"):

0 commit comments

Comments
 (0)