mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-27 18:45:05 +08:00
Improve command experience
This commit is contained in:
parent
dc889045c8
commit
0c74fbadca
@ -263,6 +263,10 @@ def _new_command_session(bot: CQHttp,
|
|||||||
:param ctx: message context
|
:param ctx: message context
|
||||||
:return: CommandSession object or None
|
:return: CommandSession object or None
|
||||||
"""
|
"""
|
||||||
|
if not ctx['to_me']:
|
||||||
|
# TODO: 支持不需要 at 的命令
|
||||||
|
return None
|
||||||
|
|
||||||
msg_text = str(ctx['message']).lstrip()
|
msg_text = str(ctx['message']).lstrip()
|
||||||
|
|
||||||
for start in bot.config.COMMAND_START:
|
for start in bot.config.COMMAND_START:
|
||||||
|
@ -5,22 +5,31 @@ from aiocqhttp.message import MessageSegment
|
|||||||
|
|
||||||
from .command import handle_command
|
from .command import handle_command
|
||||||
from .log import logger
|
from .log import logger
|
||||||
|
from .helpers import send
|
||||||
|
|
||||||
|
|
||||||
async def handle_message(bot: CQHttp, ctx: Dict[str, Any]) -> None:
|
async def handle_message(bot: CQHttp, ctx: Dict[str, Any]) -> None:
|
||||||
# TODO: 支持让插件自己选择是否响应没有 at 的消息
|
# TODO: 支持让插件自己选择是否响应没有 at 的消息
|
||||||
if ctx['message_type'] != 'private':
|
if ctx['message_type'] != 'private':
|
||||||
# group or discuss
|
# group or discuss
|
||||||
first_message_seg = ctx['message'][0]
|
ctx['to_me'] = False
|
||||||
if first_message_seg != MessageSegment.at(ctx['self_id']):
|
indexes_to_remove = []
|
||||||
return
|
for i, seg in enumerate(ctx['message']):
|
||||||
del ctx['message'][0]
|
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']:
|
if not ctx['message']:
|
||||||
ctx['message'].append(MessageSegment.text(''))
|
ctx['message'].append(MessageSegment.text(''))
|
||||||
|
else:
|
||||||
|
ctx['to_me'] = True
|
||||||
|
|
||||||
handled = await handle_command(bot, ctx)
|
handled = await handle_command(bot, ctx)
|
||||||
if handled:
|
if handled:
|
||||||
logger.debug('Message is handled as a command')
|
logger.debug('Message is handled as a command')
|
||||||
return
|
return
|
||||||
else:
|
elif ctx['to_me']:
|
||||||
await bot.send(ctx, '你在说什么我看不懂诶')
|
await send(bot, ctx, '你在说什么我看不懂诶')
|
||||||
|
|
||||||
|
# TODO: NLP
|
||||||
|
Loading…
Reference in New Issue
Block a user