1use nagisa::prelude::*;
6use std::sync::atomic::{AtomicU64, Ordering};
7use std::time::Duration;
8
9nagisa::plugin! { name = "账号绑定", category = User, key = "bind" }
10
11static COUNTER: AtomicU64 = AtomicU64::new(0);
12
13#[command("绑定", id = "issue")]
15async fn issue(reply: Reply, s: Sender, pend: State<Rendezvous<String, Uin>>) -> HandlerResult {
16 let token = format!("BIND{:04}", COUNTER.fetch_add(1, Ordering::Relaxed) % 10000);
17 pend.issue_with_ttl(token.clone(), s.0, Duration::from_secs(300));
18 reply.text(format!("私聊我发送 {token} 完成绑定(5 分钟内有效)")).await?;
19 Ok(())
20}
21
22#[command(regex = r"^BIND\d{4}$", id = "claim")]
25async fn claim(_pm: PrivateMessage, cmd: Command, pend: State<Rendezvous<String, Uin>>, reply: Reply) -> HandlerResult {
26 match pend.claim(&cmd.0) {
27 Some(issuer) => {
28 reply.text(format!("绑定成功:已关联群账号 {}", issuer.0)).await?;
30 }
31 None => {
32 reply.text("token 无效或已过期,请重新在群里发送「绑定」").await?;
33 }
34 }
35 Ok(())
36}
37
38#[cfg(feature = "onebot")]
39#[tokio::main]
40async fn main() -> Result<()> {
41 let shutdown = nagisa::ctrl_c_shutdown();
42 App::new().run_onebot(OneBotConfig::new("ws://127.0.0.1:8080/onebot/v11/ws"), shutdown).await
46}
47
48#[cfg(not(feature = "onebot"))]
49fn main() {
50 eprintln!("enable the `onebot` feature to run this example");
51}