Add CommandExt trait

This commit is contained in:
Joscha 2025-02-21 03:07:39 +01:00
parent 15dddb205f
commit 98916b0d2b
2 changed files with 43 additions and 14 deletions

View file

@ -5,10 +5,8 @@ use euphoxide::api::Message;
use euphoxide_bot::{ use euphoxide_bot::{
bot::Bot, bot::Bot,
command::{ command::{
bang::{General, Specific},
basic::Described,
botrulez::{FullHelp, Ping, ShortHelp}, botrulez::{FullHelp, Ping, ShortHelp},
Command, Commands, Context, Info, Propagate, Command, CommandExt, Commands, Context, Info, Propagate,
}, },
}; };
use tokio::sync::mpsc; use tokio::sync::mpsc;
@ -46,17 +44,20 @@ async fn run() -> anyhow::Result<()> {
let (event_tx, mut event_rx) = mpsc::channel(10); let (event_tx, mut event_rx) = mpsc::channel(10);
let commands = Commands::new() let commands = Commands::new()
.then(Described::hidden(General::new("ping", Ping::default()))) .then(Ping::default().general("ping").hidden())
.then(Described::hidden(Specific::new("ping", Ping::default()))) .then(Ping::default().specific("ping").hidden())
.then(Described::hidden(General::new( .then(
"help", ShortHelp::new("/me demonstrates how to use euphoxide")
ShortHelp::new("/me demonstrates how to use euphoxide"), .general("help")
))) .hidden(),
.then(Described::hidden(Specific::new( )
"help", .then(
FullHelp::new().with_after("Created using euphoxide."), FullHelp::new()
))) .with_after("Created using euphoxide.")
.then(General::new("pyramid", Pyramid)); .specific("help")
.hidden(),
)
.then(Pyramid.general("pyramid"));
let bot: Bot = Bot::new_simple(commands, event_tx); let bot: Bot = Bot::new_simple(commands, event_tx);

View file

@ -123,6 +123,34 @@ pub trait Command<S = (), E = euphoxide::Error> {
) -> Result<Propagate, E>; ) -> Result<Propagate, E>;
} }
pub trait CommandExt: Sized {
fn described(self) -> basic::Described<Self> {
basic::Described::new(self)
}
fn hidden(self) -> basic::Described<Self> {
basic::Described::hidden(self)
}
fn prefixed(self, prefix: impl ToString) -> basic::Prefixed<Self> {
basic::Prefixed::new(prefix, self)
}
fn general(self, name: impl ToString) -> bang::General<Self> {
bang::General::new(name, self)
}
fn global(self, name: impl ToString) -> bang::Global<Self> {
bang::Global::new(name, self)
}
fn specific(self, name: impl ToString) -> bang::Specific<Self> {
bang::Specific::new(name, self)
}
}
impl<C> CommandExt for C {}
pub struct Commands<B = (), E = euphoxide::Error> { pub struct Commands<B = (), E = euphoxide::Error> {
commands: Vec<Box<dyn Command<B, E> + Sync + Send>>, commands: Vec<Box<dyn Command<B, E> + Sync + Send>>,
} }