避免 Alconna 在 ParamsUnmatched 时自动回复

🎨 将通用 handle 封装一下
This commit is contained in:
2023-11-15 11:04:09 +08:00
parent bfadac4f79
commit 0c968be163
4 changed files with 40 additions and 12 deletions

View File

@@ -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,

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)