Add bot::command::clap

This commit is contained in:
Joscha 2023-01-24 14:12:50 +01:00
parent 84b742ee6e
commit 4dcc021f73
4 changed files with 189 additions and 7 deletions

View file

@ -1,8 +1,14 @@
mod clap;
use std::future::Future;
use async_trait::async_trait;
use crate::api::{self, Message, MessageId};
use crate::conn::{self, ConnTx, Joined};
pub use self::clap::{Clap, ClapCommand};
use super::instance::InstanceConfig;
#[derive(Clone, Copy, PartialEq, Eq)]
@ -38,6 +44,7 @@ impl Kind {
}
pub struct Context {
pub name: String,
pub kind: Kind,
pub config: InstanceConfig,
pub conn_tx: ConnTx,
@ -45,24 +52,26 @@ pub struct Context {
}
impl Context {
pub async fn send<S: ToString>(&self, content: S) -> Result<Message, conn::Error> {
pub fn send<S: ToString>(&self, content: S) -> impl Future<Output = conn::Result<Message>> {
let cmd = api::Send {
content: content.to_string(),
parent: None,
};
Ok(self.conn_tx.send(cmd).await?.0)
let reply = self.conn_tx.send(cmd);
async move { reply.await.map(|r| r.0) }
}
pub async fn reply<S: ToString>(
pub fn reply<S: ToString>(
&self,
parent: MessageId,
content: S,
) -> Result<Message, conn::Error> {
) -> impl Future<Output = conn::Result<Message>> {
let cmd = api::Send {
content: content.to_string(),
parent: Some(parent),
};
Ok(self.conn_tx.send(cmd).await?.0)
let reply = self.conn_tx.send(cmd);
async move { reply.await.map(|r| r.0) }
}
}