diff --git a/src/bot/command.rs b/src/bot/command.rs index 7f75f5e..5c400ca 100644 --- a/src/bot/command.rs +++ b/src/bot/command.rs @@ -1,6 +1,7 @@ mod bang; mod clap; mod hidden; +mod prefixed; use std::future::Future; @@ -12,6 +13,7 @@ use crate::conn::{self, ConnTx, Joined}; pub use self::bang::*; pub use self::clap::*; pub use self::hidden::*; +pub use self::prefixed::*; use super::instance::InstanceConfig; diff --git a/src/bot/command/prefixed.rs b/src/bot/command/prefixed.rs new file mode 100644 index 0000000..486ab55 --- /dev/null +++ b/src/bot/command/prefixed.rs @@ -0,0 +1,39 @@ +use async_trait::async_trait; + +use crate::api::Message; + +use super::{Command, Context}; + +pub struct Prefixed { + prefix: String, + inner: C, +} + +impl Prefixed { + pub fn new(prefix: S, inner: C) -> Self { + Self { + prefix: prefix.to_string(), + inner, + } + } +} + +#[async_trait] +impl Command for Prefixed +where + B: Send, + C: Command + Send + Sync, +{ + fn description(&self, ctx: &Context) -> Option { + let inner = self.inner.description(ctx)?; + Some(format!("{} - {inner}", self.prefix)) + } + + async fn execute(&self, arg: &str, msg: &Message, ctx: &Context, bot: &mut B) -> Result<(), E> { + if let Some(rest) = arg.trim_start().strip_prefix(&self.prefix) { + self.inner.execute(rest, msg, ctx, bot).await + } else { + Ok(()) + } + } +}