1use nagisa::prelude::*;
12use std::sync::atomic::{AtomicU64, Ordering};
13
14#[command("echo", mention_me)]
16async fn echo(reply: Reply, args: ArgText) -> HandlerResult {
17 reply.text(format!("echo: {}", args.0)).await?;
18 Ok(())
19}
20
21#[command("ping", name = "ping", description = "health check")]
23async fn ping(reply: Reply) -> HandlerResult {
24 reply.text("pong").await?;
25 Ok(())
26}
27
28async fn group_only(msg: GroupMessage, bot: Bot) -> HandlerResult {
30 let _ = (msg, bot);
31 Ok(())
32}
33
34struct Counter {
36 hits: AtomicU64,
37}
38
39#[command("count")]
40async fn count(reply: Reply, state: State<Counter>) -> HandlerResult {
41 let n = state.hits.fetch_add(1, Ordering::Relaxed) + 1;
43 reply.text(format!("count = {n}")).await?;
44 Ok(())
45}
46
47#[derive(Args)]
51struct Transfer {
52 #[arg(at)]
53 target: Uin, amount: u64, #[arg(flag, short = 'f')]
56 force: bool, }
58
59#[command("转账", "transfer", mention_me)]
60async fn transfer(reply: Reply, args: Args<Transfer>) -> HandlerResult {
61 let Transfer { target, amount, force } = args.0;
62 reply.text(format!("transfer {amount} to {} (force={force})", target.0)).await?;
63 Ok(())
64}
65
66#[derive(ArgEnum, Debug)]
68enum Switch {
69 On,
70 Off,
71}
72
73#[derive(Args)]
74struct RepeatCfg {
75 mode: Switch, #[arg(image)]
77 sample: Option<Media>, }
79
80#[command("repeat", mention_me)]
81async fn repeat(reply: Reply, args: Args<RepeatCfg>) -> HandlerResult {
82 let RepeatCfg { mode, sample } = args.0;
83 reply.text(format!("repeat {mode:?}, has_image={}", sample.is_some())).await?;
84 Ok(())
85}
86
87#[tokio::main]
88async fn main() -> Result<()> {
89 let shutdown = ctrl_c_shutdown();
90 App::new()
91 .data(Counter { hits: AtomicU64::new(0) })
92 .on(group_only)
93 .run_onebot(OneBotConfig::new("ws://127.0.0.1:8080/onebot/v11/ws"), shutdown)
94 .await
95}