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

Method get_value

nonebot/rule.py:88–135  ·  view source on GitHub ↗
(cls, bot: Bot, event: Event, state: T_State)

Source from the content-addressed store, hash-verified

86
87 @classmethod
88 def get_value(cls, bot: Bot, event: Event, state: T_State) -> CMD_RESULT:
89 prefix = CMD_RESULT(
90 command=None,
91 raw_command=None,
92 command_arg=None,
93 command_start=None,
94 command_whitespace=None,
95 )
96 state[PREFIX_KEY] = prefix
97 if event.get_type() != "message":
98 return prefix
99
100 message = event.get_message()
101 message_seg: MessageSegment = message[0]
102 if message_seg.is_text():
103 segment_text = str(message_seg).lstrip()
104 if pf := cls.prefix.longest_prefix(segment_text):
105 value: TRIE_VALUE = pf.value
106 prefix[RAW_CMD_KEY] = pf.key
107 prefix[CMD_START_KEY] = value.command_start
108 prefix[CMD_KEY] = value.command
109
110 msg = message.copy()
111 msg.pop(0)
112
113 # check whitespace
114 arg_str = segment_text[len(pf.key) :]
115 arg_str_stripped = arg_str.lstrip()
116 # check next segment until arg detected or no text remain
117 while not arg_str_stripped and msg and msg[0].is_text():
118 arg_str += str(msg.pop(0))
119 arg_str_stripped = arg_str.lstrip()
120
121 has_arg = arg_str_stripped or msg
122 if (
123 has_arg
124 and (stripped_len := len(arg_str) - len(arg_str_stripped)) > 0
125 ):
126 prefix[CMD_WHITESPACE_KEY] = arg_str[:stripped_len]
127
128 # construct command arg
129 if arg_str_stripped:
130 new_message = msg.__class__(arg_str_stripped)
131 for new_segment in reversed(new_message):
132 msg.insert(0, new_segment)
133 prefix[CMD_ARG_KEY] = msg
134
135 return prefix
136
137
138class StartswithRule:

Callers 3

handle_eventFunction · 0.80
get_fieldMethod · 0.80
test_trieFunction · 0.80

Calls 6

CMD_RESULTClass · 0.85
popMethod · 0.80
get_typeMethod · 0.45
get_messageMethod · 0.45
is_textMethod · 0.45
copyMethod · 0.45

Tested by 1

test_trieFunction · 0.64