nonebot2/none/message.py

33 lines
969 B
Python
Raw Normal View History

2018-06-14 22:58:24 +00:00
from typing import Dict, Any
from aiocqhttp.message import MessageSegment
2018-07-04 01:28:31 +00:00
from . import NoneBot
2018-06-27 14:05:12 +00:00
from .command import handle_command
2018-06-27 14:50:01 +00:00
from .log import logger
2018-07-02 08:54:29 +00:00
from .natural_language import handle_natural_language
2018-06-14 22:58:24 +00:00
2018-06-27 08:36:40 +00:00
2018-07-04 01:28:31 +00:00
async def handle_message(bot: NoneBot, ctx: Dict[str, Any]) -> None:
2018-06-14 22:58:24 +00:00
if ctx['message_type'] != 'private':
# group or discuss
2018-06-30 02:58:56 +00:00
ctx['to_me'] = False
first_message_seg = ctx['message'][0]
if first_message_seg == MessageSegment.at(ctx['self_id']):
ctx['to_me'] = True
del ctx['message'][0]
2018-06-14 22:58:24 +00:00
if not ctx['message']:
ctx['message'].append(MessageSegment.text(''))
2018-06-30 02:58:56 +00:00
else:
ctx['to_me'] = True
2018-06-14 22:58:24 +00:00
2018-06-27 08:36:40 +00:00
handled = await handle_command(bot, ctx)
2018-06-14 22:58:24 +00:00
if handled:
2018-07-01 12:01:05 +00:00
logger.debug('Message is handled as a command')
2018-06-27 08:36:40 +00:00
return
2018-06-30 02:58:56 +00:00
2018-07-01 03:01:24 +00:00
handled = await handle_natural_language(bot, ctx)
if handled:
logger.debug('Message is handled as natural language')
return