MCPcopy
hub / github.com/nonebot/nonebot2 / __call__

Method __call__

nonebot/rule.py:549–586  ·  view source on GitHub ↗
(
        self,
        state: T_State,
        cmd: tuple[str, ...] | None = Command(),
        msg: Message | None = CommandArg(),
    )

Source from the content-addressed store, hash-verified

547 return hash((frozenset(self.cmds), self.parser))
548
549 async def __call__(
550 self,
551 state: T_State,
552 cmd: tuple[str, ...] | None = Command(),
553 msg: Message | None = CommandArg(),
554 ) -> bool:
555 if cmd not in self.cmds or msg is None:
556 return False
557
558 try:
559 state[SHELL_ARGV] = list(
560 chain.from_iterable(
561 shlex.split(str(seg))
562 if cast(MessageSegment, seg).is_text()
563 else (seg,)
564 for seg in msg
565 )
566 )
567 except Exception as e:
568 # set SHELL_ARGV to none indicating shlex error
569 state[SHELL_ARGV] = None
570 # ensure SHELL_ARGS is set to ParserExit if parser is provided
571 if self.parser:
572 state[SHELL_ARGS] = ParserExit(status=2, message=str(e))
573 return True
574
575 if self.parser:
576 t = parser_message.set("")
577 try:
578 args = self.parser.parse_args(state[SHELL_ARGV])
579 state[SHELL_ARGS] = args
580 except ArgumentError as e:
581 state[SHELL_ARGS] = ParserExit(status=2, message=str(e))
582 except ParserExit as e:
583 state[SHELL_ARGS] = e
584 finally:
585 parser_message.reset(t)
586 return True
587
588
589def shell_command(

Callers

nothing calls this directly

Calls 6

CommandFunction · 0.90
CommandArgFunction · 0.90
ParserExitClass · 0.90
setMethod · 0.80
parse_argsMethod · 0.80
is_textMethod · 0.45

Tested by

no test coverage detected