nonebot2/none/message.py

46 lines
1.4 KiB
Python
Raw Normal View History

2018-06-15 06:58:24 +08:00
from typing import Dict, Any
from aiocqhttp.message import MessageSegment
2018-07-04 09:28:31 +08:00
from . import NoneBot
2018-06-27 22:05:12 +08:00
from .command import handle_command
2018-06-27 22:50:01 +08:00
from .log import logger
2018-07-02 16:54:29 +08:00
from .natural_language import handle_natural_language
2018-06-15 06:58:24 +08:00
2018-06-27 16:36:40 +08:00
2018-07-04 09:28:31 +08:00
async def handle_message(bot: NoneBot, ctx: Dict[str, Any]) -> None:
2018-07-21 00:46:34 +08:00
_log_message(ctx)
2018-06-15 06:58:24 +08:00
if ctx['message_type'] != 'private':
# group or discuss
2018-06-30 10:58:56 +08: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-15 06:58:24 +08:00
if not ctx['message']:
ctx['message'].append(MessageSegment.text(''))
2018-06-30 10:58:56 +08:00
else:
ctx['to_me'] = True
2018-06-15 06:58:24 +08:00
2018-06-27 16:36:40 +08:00
handled = await handle_command(bot, ctx)
2018-06-15 06:58:24 +08:00
if handled:
2018-07-21 00:46:34 +08:00
logger.info(f'Message {ctx["message_id"]} is handled as a command')
2018-06-27 16:36:40 +08:00
return
2018-06-30 10:58:56 +08:00
2018-07-01 11:01:24 +08:00
handled = await handle_natural_language(bot, ctx)
if handled:
2018-07-21 00:46:34 +08:00
logger.info(f'Message {ctx["message_id"]} is handled '
f'as natural language')
2018-07-01 11:01:24 +08:00
return
2018-07-21 00:46:34 +08:00
def _log_message(ctx: Dict[str, Any]) -> None:
msg_from = f'{ctx["user_id"]}'
if ctx['message_type'] == 'group':
msg_from += f'@[群:{ctx["group_id"]}]'
elif ctx['message_type'] == 'discuss':
msg_from += f'@[讨论组:{ctx["discuss_id"]}]'
logger.info(f'Message {ctx["message_id"]} from {msg_from}: '
f'{ctx["message"]}')