nonebot2/nonebot/message.py

121 lines
3.7 KiB
Python
Raw Normal View History

2018-07-30 23:41:19 +08:00
import asyncio
2018-10-14 19:54:55 +08:00
from typing import Callable
2018-06-15 06:58:24 +08:00
2018-10-14 19:54:55 +08:00
from aiocqhttp.message import *
2018-06-15 06:58:24 +08:00
2018-07-04 09:28:31 +08:00
from . import NoneBot
2018-07-24 23:59:45 +08:00
from .command import handle_command, SwitchException
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-10-16 01:03:50 +08:00
from .typing import Context_T
2018-06-15 06:58:24 +08:00
2018-07-30 23:41:19 +08:00
_message_preprocessors = set()
def message_preprocessor(func: Callable) -> Callable:
_message_preprocessors.add(func)
return func
2018-06-27 16:36:40 +08:00
2018-10-16 01:03:50 +08:00
async def handle_message(bot: NoneBot, ctx: Context_T) -> None:
2018-07-21 00:46:34 +08:00
_log_message(ctx)
if not ctx['message']:
ctx['message'].append(MessageSegment.text(''))
2018-07-30 23:41:19 +08:00
coros = []
for processor in _message_preprocessors:
2018-10-16 01:03:50 +08:00
coros.append(processor(bot, ctx))
2018-07-30 23:41:19 +08:00
if coros:
await asyncio.wait(coros)
raw_to_me = ctx.get('to_me', False)
_check_at_me(bot, ctx)
_check_calling_me_nickname(bot, ctx)
ctx['to_me'] = raw_to_me or ctx['to_me']
2018-06-15 06:58:24 +08:00
2018-07-24 23:59:45 +08:00
while True:
try:
handled = await handle_command(bot, ctx)
break
except SwitchException as e:
# we are sure that there is no session existing now
ctx['message'] = e.new_ctx_message
ctx['to_me'] = True
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 _check_at_me(bot: NoneBot, ctx: Context_T) -> None:
if ctx['message_type'] == 'private':
ctx['to_me'] = True
else:
# group or discuss
ctx['to_me'] = False
at_me_seg = MessageSegment.at(ctx['self_id'])
# check the first segment
first_msg_seg = ctx['message'][0]
if first_msg_seg == at_me_seg:
ctx['to_me'] = True
del ctx['message'][0]
if not ctx['to_me']:
# check the last segment
i = -1
last_msg_seg = ctx['message'][i]
if last_msg_seg.type == 'text' and \
not last_msg_seg.data['text'].strip() and \
len(ctx['message']) >= 2:
i -= 1
last_msg_seg = ctx['message'][i]
if last_msg_seg == at_me_seg:
ctx['to_me'] = True
del ctx['message'][i:]
if not ctx['message']:
ctx['message'].append(MessageSegment.text(''))
def _check_calling_me_nickname(bot: NoneBot, ctx: Context_T) -> None:
first_msg_seg = ctx['message'][0]
if first_msg_seg.type != 'text':
return
first_text = first_msg_seg.data['text']
if bot.config.NICKNAME:
# check if the user is calling me with my nickname
if isinstance(bot.config.NICKNAME, str) or \
not isinstance(bot.config.NICKNAME, Iterable):
nicknames = (bot.config.NICKNAME,)
else:
nicknames = filter(lambda n: n, bot.config.NICKNAME)
nickname_regex = '|'.join(nicknames)
m = re.search(rf'^({nickname_regex})([\s,]*|$)',
first_text, re.IGNORECASE)
if m:
nickname = m.group(1)
logger.debug(f'User is calling me {nickname}')
ctx['to_me'] = True
first_msg_seg.data['text'] = first_text[m.end():]
2018-10-16 01:03:50 +08:00
def _log_message(ctx: Context_T) -> None:
2019-01-05 22:41:15 +08:00
msg_from = str(ctx['user_id'])
2018-07-21 00:46:34 +08:00
if ctx['message_type'] == 'group':
msg_from += f'@[群:{ctx["group_id"]}]'
elif ctx['message_type'] == 'discuss':
msg_from += f'@[讨论组:{ctx["discuss_id"]}]'
2019-01-05 22:41:15 +08:00
logger.info(f'Self: {ctx["self_id"]}, '
f'Message {ctx["message_id"]} from {msg_from}: '
2018-07-21 00:46:34 +08:00
f'{ctx["message"]}')