Construct clap commands from handlers too

This commit is contained in:
Joscha 2024-12-29 13:59:27 +01:00
parent a73ababe02
commit a527b22515
3 changed files with 88 additions and 0 deletions

View file

@ -4,6 +4,7 @@ use euphoxide::api::Message;
use euphoxide_bot::{
basic::FromHandler,
botrulez::{FullHelp, Ping, ShortHelp},
clap::FromClapHandler,
CommandExt, Commands, Context, Propagate,
};
use euphoxide_client::MultiClient;
@ -24,6 +25,21 @@ async fn pyramid(_arg: &str, msg: &Message, ctx: &Context) -> euphoxide::Result<
Ok(Propagate::No)
}
#[derive(clap::Parser)]
struct AddArgs {
lhs: i64,
rhs: i64,
}
async fn add(args: AddArgs, msg: &Message, ctx: &Context) -> euphoxide::Result<Propagate> {
let result = args.lhs + args.rhs;
ctx.reply_only(msg.id, format!("{} + {} = {result}", args.lhs, args.rhs))
.await?;
Ok(Propagate::No)
}
#[tokio::main]
async fn main() {
let (event_tx, mut event_rx) = mpsc::channel(10);
@ -48,6 +64,13 @@ async fn main() {
.with_description("build a pyramid")
.general("pyramid"),
)
.then(
FromClapHandler::new(add)
.clap()
.described()
.with_description("add two numbers")
.general("add"),
)
.build();
let clients = MultiClient::new(event_tx);