async def handle_message(bot: NoneBot, ctx: Dict[str, Any]) -> None: _log_message(ctx) if ctx['message_type'] != 'private': # group or discuss 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] if not ctx['message']: ctx['message'].append(MessageSegment.text('')) else: ctx['to_me'] = True 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 if handled: logger.info(f'Message {ctx["message_id"]} is handled as a command') return handled = await handle_natural_language(bot, ctx) if handled: logger.info(f'Message {ctx["message_id"]} is handled ' f'as natural language') return
def _check_at_me(bot: NoneBot, event: CQEvent) -> None: if event.detail_type == 'private': event['to_me'] = True else: # group or discuss event['to_me'] = False at_me_seg = MessageSegment.at(event.self_id) # check the first segment first_msg_seg = event.message[0] if first_msg_seg == at_me_seg: event['to_me'] = True del event.message[0] if not event['to_me']: # check the last segment i = -1 last_msg_seg = event.message[i] if last_msg_seg.type == 'text' and \ not last_msg_seg.data['text'].strip() and \ len(event.message) >= 2: i -= 1 last_msg_seg = event.message[i] if last_msg_seg == at_me_seg: event['to_me'] = True del event.message[i:] if not event.message: event.message.append(MessageSegment.text(''))
def _check_at_me(bot: NoneBot, event: CQEvent) -> None: if event.detail_type == 'private': event['to_me'] = True return def is_at_me(seg): return seg.type == 'at' and str(seg.data['qq']) == str(event.self_id) # group or discuss event['to_me'] = False # check the first segment first_msg_seg = event.message[0] if is_at_me(first_msg_seg): event['to_me'] = True del event.message[0] if not event['to_me']: # check the last segment i = -1 last_msg_seg = event.message[i] if last_msg_seg.type == 'text' and \ not last_msg_seg.data['text'].strip() and \ len(event.message) >= 2: i -= 1 last_msg_seg = event.message[i] if is_at_me(last_msg_seg): event['to_me'] = True del event.message[i:] if not event.message: event.message.append(MessageSegment.text(''))
async def handle_message(bot: NoneBot, event: CQEvent) -> None: """INTERNAL API""" _log_message(event) assert isinstance(event.message, Message) if not event.message: event.message.append(MessageSegment.text('')) # type: ignore raw_to_me = event.get('to_me', False) _check_at_me(bot, event) _check_calling_me_nickname(bot, event) event['to_me'] = raw_to_me or event['to_me'] coros = [] plugin_manager = PluginManager() for preprocessor in MessagePreprocessorManager.preprocessors: coros.append(preprocessor.func(bot, event, plugin_manager)) if coros: try: await asyncio.gather(*coros) except CanceledException as e: logger.info( f'Message {event["message_id"]} is ignored: {e.reason}') return while True: try: handled = await handle_command(bot, event, plugin_manager.cmd_manager) break except SwitchException as e: # we are sure that there is no session existing now event['message'] = e.new_message event['to_me'] = True if handled: logger.info(f'Message {event.message_id} is handled as a command') return handled = await handle_natural_language(bot, event, plugin_manager.nlp_manager) if handled: logger.info(f'Message {event.message_id} is handled ' f'as natural language') return logger.debug(f'Message {event.message_id} was not handled')
async def handle_message(bot: NoneBot, event: CQEvent) -> None: _log_message(event) assert isinstance(event.message, Message) if not event.message: event.message.append(MessageSegment.text('')) # type: ignore # 检查是否是对我说话 # 但不在这里终止,因为还可能是会话未结束。 raw_to_me = event.get('to_me', False) _check_at_me(bot, event) _check_calling_me_nickname(bot, event) event['to_me'] = raw_to_me or event['to_me'] plugin_manager = PluginManager() while True: try: handled = await handle_regexp(bot, event, plugin_manager.regexp_manager) break except SwitchException as e: event['message'] = e.new_message event['to_me'] = True if handled: logger.info( f'Message {event.message_id} is handled as a regular expression command.' ) return # 过一遍预处理器(好像没用到过) coros = [] for preprocessor in _message_preprocessors: coros.append(preprocessor(bot, event, plugin_manager)) if coros: try: await asyncio.gather(*coros) except CanceledException: logger.info(f'Message {event["message_id"]} is ignored') return # 处理命令,如果中途被 Switch 则继续处理。 # 此处命令还未被识别 while True: try: handled = await handle_command(bot, event, plugin_manager.cmd_manager) break except SwitchException as e: # we are sure that there is no session existing now event['message'] = e.new_message event['to_me'] = True if handled: logger.info(f'Message {event.message_id} is handled as a command') return handled = await handle_natural_language(bot, event, plugin_manager.nlp_manager) if handled: logger.info(f'Message {event.message_id} is handled ' f'as natural language') return