//! Basic command wrappers. use async_trait::async_trait; use euphoxide::api::Message; use crate::bot::Bot; use super::{Command, Context, Info, Propagate}; /// Rewrite or hide command info. pub struct Described { pub inner: C, pub trigger: Option>, pub description: Option>, } impl Described { pub fn new(inner: C) -> Self { Self { inner, trigger: None, description: None, } } pub fn hidden(inner: C) -> Self { Self::new(inner) .with_trigger_hidden() .with_description_hidden() } pub fn with_trigger(mut self, trigger: impl ToString) -> Self { self.trigger = Some(Some(trigger.to_string())); self } pub fn with_trigger_hidden(mut self) -> Self { self.trigger = Some(None); self } pub fn with_description(mut self, description: impl ToString) -> Self { self.description = Some(Some(description.to_string())); self } pub fn with_description_hidden(mut self) -> Self { self.description = Some(None); self } } #[async_trait] impl Command for Described where C: Command + Sync, { fn info(&self, ctx: &Context) -> Info { let info = self.inner.info(ctx); Info { trigger: self.trigger.clone().unwrap_or(info.trigger), description: self.description.clone().unwrap_or(info.description), } } async fn execute( &self, arg: &str, msg: &Message, ctx: &Context, bot: &Bot, ) -> Result { self.inner.execute(arg, msg, ctx, bot).await } } 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 C: Command + Sync, { fn info(&self, ctx: &Context) -> Info { self.inner.info(ctx).with_prepended_trigger(&self.prefix) } async fn execute( &self, arg: &str, msg: &Message, ctx: &Context, bot: &Bot, ) -> Result { if let Some(rest) = arg.trim_start().strip_prefix(&self.prefix) { self.inner.execute(rest, msg, ctx, bot).await } else { Ok(Propagate::Yes) } } }