pub struct EnabledSet { /* private fields */ }Expand description
命令启用状态表。廉价共享(内部锁),运行期可改。
Implementations§
Source§impl EnabledSet
impl EnabledSet
pub fn new() -> EnabledSet
Sourcepub fn set(&self, key: impl Into<SwitchKey>, peer: Option<Peer>, enabled: bool)
pub fn set(&self, key: impl Into<SwitchKey>, peer: Option<Peer>, enabled: bool)
设置某命令的启用状态:peer=None 为全局覆盖,Some(p) 为按会话覆盖。
在释放写锁后触发 on_change 回调。
同时接受纯字符串键与类型化的 SwitchKey(#[command]/#[event] 宏产出的 <FN>_KEY
常量),故触发器键无需手敲字符串。
Examples found in repository?
examples/plugin_switches.rs (line 48)
41async fn disable(reply: Reply, es: State<EnabledSet>, peer: EventPeer, args: ArgText) -> HandlerResult {
42 let key = args.0.trim();
43 if key.is_empty() {
44 reply.text("用法:/disable <key>").await?;
45 return Ok(());
46 }
47 // `State<EnabledSet>` deref 到 `EnabledSet`,所以 `es.set(..)` 直接调通。
48 es.set(key, Some(peer.0), false);
49 // 引用触发命令,让确认消息挂在它下面。
50 reply.reply(format!("已在本会话禁用:{key}")).await?;
51 Ok(())
52}
53
54#[command("/enable", id = "enable", can_disable = false)]
55async fn enable(reply: Reply, es: State<EnabledSet>, peer: EventPeer, args: ArgText) -> HandlerResult {
56 let key = args.0.trim();
57 if key.is_empty() {
58 reply.text("用法:/enable <key>").await?;
59 return Ok(());
60 }
61 es.set(key, Some(peer.0), true);
62 reply.reply(format!("已在本会话启用:{key}")).await?;
63 Ok(())
64}Sourcepub fn reset(&self, key: impl Into<SwitchKey>, peer: Option<Peer>)
pub fn reset(&self, key: impl Into<SwitchKey>, peer: Option<Peer>)
清除某命令的覆盖,回到 default_enable:peer=None 清全局,Some 清该会话。
Sourcepub fn is_enabled_keyed(
&self,
plugin_key: &str,
trigger_key: &str,
plugin_default: bool,
plugin_can_disable: bool,
trigger_default: bool,
trigger_can_disable: bool,
peer: Option<Peer>,
) -> bool
pub fn is_enabled_keyed( &self, plugin_key: &str, trigger_key: &str, plugin_default: bool, plugin_can_disable: bool, trigger_default: bool, trigger_can_disable: bool, peer: Option<Peer>, ) -> bool
分层的触发器判定:
- 触发器
can_disable=false→ 恒开(豁免,无视总开关); - 插件总开关 OFF(且插件 can_disable)→ 关(一刀压平);
- 否则看触发器自身的开关。
Sourcepub fn is_enabled(
&self,
name: &str,
default_enable: bool,
peer: Option<Peer>,
) -> bool
pub fn is_enabled( &self, name: &str, default_enable: bool, peer: Option<Peer>, ) -> bool
单键查询(无插件层适用处用它)。
Sourcepub fn on_change<F>(&self, f: F)
pub fn on_change<F>(&self, f: F)
注册一个在每次 set 调用后(锁释放后)触发的回调。只能注册一个,再次调用替换前一个。
reset 不触发回调(它是本地回退,不是需持久化的变更)。
Sourcepub fn snapshot(&self) -> EnabledOverrides
pub fn snapshot(&self) -> EnabledOverrides
把当前所有覆盖快照成一个可序列化结构。
Sourcepub fn restore(&self, ov: EnabledOverrides)
pub fn restore(&self, ov: EnabledOverrides)
从先前快照的结构恢复覆盖(替换当前状态)。
Trait Implementations§
Source§impl Default for EnabledSet
impl Default for EnabledSet
Source§fn default() -> EnabledSet
fn default() -> EnabledSet
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl !Freeze for EnabledSet
impl RefUnwindSafe for EnabledSet
impl Send for EnabledSet
impl Sync for EnabledSet
impl Unpin for EnabledSet
impl UnsafeUnpin for EnabledSet
impl UnwindSafe for EnabledSet
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more