pub struct Matcher {
pub mention_me: bool,
pub to_me_only: bool,
/* private fields */
}Expand description
命令触发匹配器:一组正则(任一命中即整体命中)+ MentionMe / to_me 开关。
Fields§
§mention_me: bool是否启用 MentionMe 预处理(剥前导 @self + 前导 /)。
to_me_only: bool是否要求消息「to me」(私聊或 @self)才匹配。
Implementations§
Source§impl Matcher
impl Matcher
Sourcepub fn command<I, S>(alts: I) -> Matcher
pub fn command<I, S>(alts: I) -> Matcher
字面量命令词触发器(最常用)。["echo","签到"] 编译成 ^(?:echo|签到)(?:\s|$)
——首词精确命中其一即触发,命中词为 command,其余进 args。
多词命令(如 "git add")里的字面空格放宽成 \s+,容忍多空格——故 "a b" 也命中
"a b"。要匹配字面空格请改用 Matcher::regex。空 alts 永不命中。
Sourcepub fn regex(re: &str) -> Result<Matcher, Error>
pub fn regex(re: &str) -> Result<Matcher, Error>
原始正则触发器;编译失败返回 regex::Error。捕获组进 ParsedCommand.captures。
切分语义(留意未锚定的写法):命中后 ParsedCommand.command = 整段匹配文本
(捕获组 0,trim 后——含被捕获的“参数“部分),args/args_text = 匹配区间之外的
文本(匹配前缀 + 匹配后缀按字节区间拼接)。对 ^ 锚定的模式这就是「命令头 + 其后参数」
的直觉切分;未锚定的模式(如 r"weather (\w+)" 匹配 "today weather beijing now")
则会得到 command = "weather beijing"、args_text = "today now"(前缀保留、中间留缝)。
想要参数,优先从 captures 取捕获组,或给模式加 ^ 锚定。
Sourcepub fn slots(specs: Vec<SlotSpec>) -> Result<Matcher, Error>
pub fn slots(specs: Vec<SlotSpec>) -> Result<Matcher, Error>
第三种构造器(增量):一组有序 SlotSpec 编成一条锚定正则,
命名槽成编号组,保留 名→组下标 映射。
编译规则:
- 整条正则
^锚定(在首个文本段上跑),保证头部确定性匹配。 Flank::Pre的槽排在最前、Whole居中、Post排在最后——故pre+字面量头+post程序编成^(?:pre)?lit(?:post)?(字面量头两侧的可选修饰)。- 命名槽(
name != "")外包一层捕获组(...),其 1-based 组下标进slot_names;optional再外包(?:...)?。字面量头(name == "")不额外包捕获组(不进映射)。
命中后 match_event 据 slot_names 把每个命名组(Some(text) | 缺省 None)填入
ParsedCommand.named_captures,供 Slots<T> 类型化投影。
Sourcepub fn with_usage(self, usage: impl Into<String>) -> Matcher
pub fn with_usage(self, usage: impl Into<String>) -> Matcher
附带显式用法串(#[command(usage="…")]):命中后随 ParsedCommand.usage 携带,
parse-miss 时优先于 dev 自动 hint 回贴。增量构造器,不影响既有匹配语义。
Sourcepub fn mention_me(self) -> Matcher
pub fn mention_me(self) -> Matcher
要求 @bot 呼叫(置 to_me_only),文本层再剥前导 /。前导 @self 段的剥离
对所有命令统一做,不属本开关。
Sourcepub fn no_args(self) -> Matcher
pub fn no_args(self) -> Matcher
无参命令的默认剩余内容要求(#[command] 对无命令体形参的命令自动调用,
命令式注册无参命令请手动加):只认「命令词」与「前置 回复 / @bot / 空白 + 命令词」;
剩余文本非空,或剩余段里有前导回复以外的段(表情 / 图片 / 任何 @ 含 @bot…)都算内容,
整体不算命中(静默放行,不进 parse-miss)——「我的 xxx」是日常说话,不该触发「我的」。
Sourcepub fn exact(self) -> Matcher
pub fn exact(self) -> Matcher
严格模式(#[command(.., exact)] 显式开启):整条消息只能是命令词本身,
连 回复 / @bot 这些呼叫姿势都不算——比 no_args 更狠。
(与 mention_me 同用时 @self 在匹配前已被剥掉,不受此限。)
Sourcepub fn match_event(&self, ctx: &Ctx) -> Option<ParsedCommand>
pub fn match_event(&self, ctx: &Ctx) -> Option<ParsedCommand>
针对事件运行匹配。None = 不匹配(或非消息事件);Some = 命中并解析。