use nagisa::prelude::*;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;
nagisa::plugin! { name = "账号绑定", category = User, key = "bind" }
static COUNTER: AtomicU64 = AtomicU64::new(0);
#[command("绑定", id = "issue")]
async fn issue(reply: Reply, s: Sender, pend: State<Rendezvous<String, Uin>>) -> HandlerResult {
let token = format!("BIND{:04}", COUNTER.fetch_add(1, Ordering::Relaxed) % 10000);
pend.issue_with_ttl(token.clone(), s.0, Duration::from_secs(300));
reply.text(format!("私聊我发送 {token} 完成绑定(5 分钟内有效)")).await?;
Ok(())
}
#[command(regex = r"^BIND\d{4}$", id = "claim")]
async fn claim(_pm: PrivateMessage, cmd: Command, pend: State<Rendezvous<String, Uin>>, reply: Reply) -> HandlerResult {
match pend.claim(&cmd.0) {
Some(issuer) => {
reply.text(format!("绑定成功:已关联群账号 {}", issuer.0)).await?;
}
None => {
reply.text("token 无效或已过期,请重新在群里发送「绑定」").await?;
}
}
Ok(())
}
#[cfg(feature = "onebot")]
#[tokio::main]
async fn main() -> Result<()> {
let shutdown = nagisa::ctrl_c_shutdown();
App::new().run_onebot(OneBotConfig::new("ws://127.0.0.1:8080/onebot/v11/ws"), shutdown).await
}
#[cfg(not(feature = "onebot"))]
fn main() {
eprintln!("enable the `onebot` feature to run this example");
}