diff --git a/nonebot_plugin_tetris_stats/game_data_processor/__init__.py b/nonebot_plugin_tetris_stats/game_data_processor/__init__.py index f33c4da..9305699 100644 --- a/nonebot_plugin_tetris_stats/game_data_processor/__init__.py +++ b/nonebot_plugin_tetris_stats/game_data_processor/__init__.py @@ -1,7 +1,12 @@ from abc import ABC, abstractmethod from dataclasses import dataclass from datetime import UTC, datetime +from typing import Any +from nonebot.matcher import Matcher +from nonebot_plugin_alconna import AlcMatches, AlconnaMatcher + +from ..utils.exception import MessageFormatError from ..utils.typing import CommandType, GameType @@ -75,6 +80,24 @@ class Processor(ABC): Recorder.update_historical_data(self.event_id, historical_data) +def add_default_handlers(matcher: type[AlconnaMatcher]) -> None: + @matcher.handle() + async def _(matcher: Matcher, account: MessageFormatError): + await matcher.finish(str(account)) + + @matcher.handle() + async def _(matcher: Matcher, matches: AlcMatches): + if matches.head_matched and matches.options != {}: + await matcher.finish( + (f'{matches.error_info!r}\n' if matches.error_info is not None else '') + + f'输入"{matches.header_result} --help"查看帮助' + ) + + @matcher.handle() + async def _(matcher: Matcher, other: Any): # noqa: ANN401 + await matcher.finish() + + from . import ( # noqa: F401, E402 io_data_processor, top_data_processor, diff --git a/nonebot_plugin_tetris_stats/game_data_processor/io_data_processor/__init__.py b/nonebot_plugin_tetris_stats/game_data_processor/io_data_processor/__init__.py index 92b783e..60bdf1b 100644 --- a/nonebot_plugin_tetris_stats/game_data_processor/io_data_processor/__init__.py +++ b/nonebot_plugin_tetris_stats/game_data_processor/io_data_processor/__init__.py @@ -1,4 +1,5 @@ from datetime import timedelta +from typing import Any from arclet.alconna import Alconna, Arg, ArgFlag, Args, CommandMeta, Option from nonebot.adapters import Bot, Event @@ -8,10 +9,11 @@ from nonebot_plugin_orm import get_session from sqlalchemy import select from ...db import query_bind_info -from ...utils.exception import MessageFormatError, NeedCatchError +from ...utils.exception import NeedCatchError from ...utils.metrics import get_metrics from ...utils.platform import get_platform from ...utils.typing import Me +from .. import add_default_handlers from ..constant import BIND_COMMAND, QUERY_COMMAND from .constant import GAME_TYPE from .model import IORank @@ -65,6 +67,7 @@ alc = on_alconna( dest='rank', help_text='查询 IO 段位信息', ), + Arg('other', Any, flags=[ArgFlag.HIDDEN, ArgFlag.OPTIONAL]), meta=CommandMeta( description='查询 TETR.IO 的信息', example='io绑定scdhh\nio查我\niorankx', @@ -174,6 +177,4 @@ async def _(event: Event, matcher: Matcher, rank: Rank): await matcher.finish(message) -@alc.handle() -async def _(matcher: Matcher, account: MessageFormatError): - await matcher.finish(str(account)) +add_default_handlers(alc) diff --git a/nonebot_plugin_tetris_stats/game_data_processor/top_data_processor/__init__.py b/nonebot_plugin_tetris_stats/game_data_processor/top_data_processor/__init__.py index e3dc307..5345cba 100644 --- a/nonebot_plugin_tetris_stats/game_data_processor/top_data_processor/__init__.py +++ b/nonebot_plugin_tetris_stats/game_data_processor/top_data_processor/__init__.py @@ -1,3 +1,5 @@ +from typing import Any + from arclet.alconna import Alconna, Arg, ArgFlag, Args, CommandMeta, Option from nonebot.adapters import Bot, Event from nonebot.matcher import Matcher @@ -5,9 +7,10 @@ from nonebot_plugin_alconna import At, on_alconna from nonebot_plugin_orm import get_session from ...db import query_bind_info -from ...utils.exception import MessageFormatError, NeedCatchError +from ...utils.exception import NeedCatchError from ...utils.platform import get_platform from ...utils.typing import Me +from .. import add_default_handlers from ..constant import BIND_COMMAND, QUERY_COMMAND from .constant import GAME_TYPE from .processor import Processor, User, identify_user_info @@ -51,6 +54,7 @@ alc = on_alconna( dest='query', help_text='查询 TOP 游戏信息', ), + Arg('other', Any, flags=[ArgFlag.HIDDEN, ArgFlag.OPTIONAL]), meta=CommandMeta( description='查询 TetrisOnline波兰服 的信息', example='top绑定scdhh\ntop查我', @@ -113,6 +117,4 @@ async def _(event: Event, matcher: Matcher, account: User): await matcher.finish(str(e)) -@alc.handle() -async def _(matcher: Matcher, account: MessageFormatError): - await matcher.finish(str(account)) +add_default_handlers(alc) diff --git a/nonebot_plugin_tetris_stats/game_data_processor/tos_data_processor/__init__.py b/nonebot_plugin_tetris_stats/game_data_processor/tos_data_processor/__init__.py index 2126d38..bb0f01d 100644 --- a/nonebot_plugin_tetris_stats/game_data_processor/tos_data_processor/__init__.py +++ b/nonebot_plugin_tetris_stats/game_data_processor/tos_data_processor/__init__.py @@ -1,3 +1,5 @@ +from typing import Any + from arclet.alconna import Alconna, Arg, ArgFlag, Args, CommandMeta, Option from nonebot.adapters import Bot, Event from nonebot.matcher import Matcher @@ -5,9 +7,10 @@ from nonebot_plugin_alconna import At, on_alconna from nonebot_plugin_orm import get_session from ...db import query_bind_info -from ...utils.exception import MessageFormatError, NeedCatchError +from ...utils.exception import NeedCatchError from ...utils.platform import get_platform from ...utils.typing import Me +from .. import add_default_handlers from ..constant import BIND_COMMAND, QUERY_COMMAND from .constant import GAME_TYPE from .processor import Processor, User, identify_user_info @@ -52,6 +55,7 @@ alc = on_alconna( dest='query', help_text='查询 茶服 游戏信息', ), + Arg('other', Any), meta=CommandMeta( description='查询 TetrisOnline茶服 的信息', example='茶服查我', @@ -138,6 +142,4 @@ async def _(event: Event, matcher: Matcher, account: User): await matcher.finish(str(e)) -@alc.handle() -async def _(matcher: Matcher, account: MessageFormatError): - await matcher.finish(str(account)) +add_default_handlers(alc)