Skip to main content

Matcher

Struct Matcher 

Source
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

Source

pub fn command<I, S>(alts: I) -> Matcher
where I: IntoIterator<Item = S>, S: Into<String>,

字面量命令词触发器(最常用)。["echo","签到"] 编译成 ^(?:echo|签到)(?:\s|$) ——首词精确命中其一即触发,命中词为 command,其余进 args

多词命令(如 "git add")里的字面空格放宽成 \s+,容忍多空格——故 "a b" 也命中 "a b"。要匹配字面空格请改用 Matcher::regex。空 alts 永不命中。

Source

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 取捕获组,或给模式加 ^ 锚定。

Source

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_eventslot_names 把每个命名组(Some(text) | 缺省 None)填入 ParsedCommand.named_captures,供 Slots<T> 类型化投影。

Source

pub fn with_usage(self, usage: impl Into<String>) -> Matcher

附带显式用法串(#[command(usage="…")]):命中后随 ParsedCommand.usage 携带, parse-miss 时优先于 dev 自动 hint 回贴。增量构造器,不影响既有匹配语义。

Source

pub fn mention_me(self) -> Matcher

要求 @bot 呼叫(置 to_me_only),文本层再剥前导 /。前导 @self 段的剥离 对所有命令统一做,不属本开关。

Source

pub fn to_me(self) -> Matcher

仅要求 to_me(不剥 @;私聊或 @self 时通过)。

Source

pub fn no_args(self) -> Matcher

无参命令的默认剩余内容要求(#[command] 对无命令体形参的命令自动调用, 命令式注册无参命令请手动加):只认「命令词」与「前置 回复 / @bot / 空白 + 命令词」; 剩余文本非空,或剩余段里有前导回复以外的段(表情 / 图片 / 任何 @ 含 @bot…)都算内容, 整体不算命中(静默放行,不进 parse-miss)——「我的 xxx」是日常说话,不该触发「我的」。

Source

pub fn exact(self) -> Matcher

严格模式(#[command(.., exact)] 显式开启):整条消息只能是命令词本身, 连 回复 / @bot 这些呼叫姿势都不算——比 no_args 更狠。 (与 mention_me 同用时 @self 在匹配前已被剥掉,不受此限。)

Source

pub fn match_event(&self, ctx: &Ctx) -> Option<ParsedCommand>

针对事件运行匹配。None = 不匹配(或非消息事件);Some = 命中并解析。

Trait Implementations§

Source§

impl Clone for Matcher

Source§

fn clone(&self) -> Matcher

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Matcher

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more