From 0c74fbadcac2d0f51bc2ddfbd6854865177e66d2 Mon Sep 17 00:00:00 2001 From: Richard Chien Date: Sat, 30 Jun 2018 10:58:56 +0800 Subject: [PATCH] Improve command experience --- none/command/__init__.py | 4 ++++ none/message.py | 21 +++++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/none/command/__init__.py b/none/command/__init__.py index 8fcfc341..e0dbc3a7 100644 --- a/none/command/__init__.py +++ b/none/command/__init__.py @@ -263,6 +263,10 @@ def _new_command_session(bot: CQHttp, :param ctx: message context :return: CommandSession object or None """ + if not ctx['to_me']: + # TODO: 支持不需要 at 的命令 + return None + msg_text = str(ctx['message']).lstrip() for start in bot.config.COMMAND_START: diff --git a/none/message.py b/none/message.py index b4c3fd16..01dc1016 100644 --- a/none/message.py +++ b/none/message.py @@ -5,22 +5,31 @@ from aiocqhttp.message import MessageSegment from .command import handle_command from .log import logger +from .helpers import send async def handle_message(bot: CQHttp, ctx: Dict[str, Any]) -> None: # TODO: 支持让插件自己选择是否响应没有 at 的消息 if ctx['message_type'] != 'private': # group or discuss - first_message_seg = ctx['message'][0] - if first_message_seg != MessageSegment.at(ctx['self_id']): - return - del ctx['message'][0] + ctx['to_me'] = False + indexes_to_remove = [] + for i, seg in enumerate(ctx['message']): + if seg == MessageSegment.at(ctx['self_id']): + ctx['to_me'] = True + indexes_to_remove.append(i) + for i in reversed(indexes_to_remove): + del ctx['message'][i] if not ctx['message']: ctx['message'].append(MessageSegment.text('')) + else: + ctx['to_me'] = True handled = await handle_command(bot, ctx) if handled: logger.debug('Message is handled as a command') return - else: - await bot.send(ctx, '你在说什么我看不懂诶') + elif ctx['to_me']: + await send(bot, ctx, '你在说什么我看不懂诶') + + # TODO: NLP