plugin_switches/
plugin_switches.rs1use nagisa::prelude::*;
17
18nagisa::plugin! { name = "演示插件", category = Tool, key = "demo" }
20
21#[command("hello", id = "hello")]
22async fn hello(reply: Reply) -> HandlerResult {
23 reply.text("hello!").await?;
24 Ok(())
25}
26
27#[command("world", id = "world")]
28async fn world(reply: Reply) -> HandlerResult {
29 reply.text("world!").await?;
30 Ok(())
31}
32
33#[command("/disable", id = "disable", can_disable = false)]
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 es.set(key, Some(peer.0), false);
49 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}
65
66#[cfg(feature = "onebot")]
67#[tokio::main]
68async fn main() -> Result<()> {
69 let shutdown = ctrl_c_shutdown();
70 App::new()
71 .on_switch_change(|key, peer, value| {
73 println!("switch {key} {peer:?} -> {value}");
74 })
75 .run_onebot(OneBotConfig::new("ws://127.0.0.1:8080/onebot/v11/ws"), shutdown)
76 .await
77}
78
79#[cfg(not(feature = "onebot"))]
82fn main() {
83 println!("build with --features onebot to run this example against a OneBot endpoint");
84}