Add helper function for adding command to Commands

This commit is contained in:
Joscha 2024-12-29 16:10:58 +01:00
parent b62afb32e6
commit ea53af739a
2 changed files with 43 additions and 28 deletions

View file

@ -44,35 +44,43 @@ async fn add(args: AddArgs, msg: &Message, ctx: &Context) -> euphoxide::Result<P
async fn main() { async fn main() {
let (event_tx, mut event_rx) = mpsc::channel(10); let (event_tx, mut event_rx) = mpsc::channel(10);
let commands = Commands::new() let mut commands = Commands::new();
.then(Ping::default().general("ping").hidden())
.then(Ping::default().specific("ping").hidden()) Ping::default()
.then( .general("ping")
.hidden()
.add_to(&mut commands);
Ping::default()
.specific("ping")
.hidden()
.add_to(&mut commands);
ShortHelp::new("/me demonstrates how to use euphoxide") ShortHelp::new("/me demonstrates how to use euphoxide")
.general("help") .general("help")
.hidden(), .hidden()
) .add_to(&mut commands);
.then(
FullHelp::new() FullHelp::new()
.with_after("Created using euphoxide.") .with_after("Created using euphoxide.")
.specific("help") .specific("help")
.hidden(), .hidden()
) .add_to(&mut commands);
.then(
FromHandler::new(pyramid) FromHandler::new(pyramid)
.described() .described()
.with_description("build a pyramid") .with_description("build a pyramid")
.general("pyramid"), .general("pyramid")
) .add_to(&mut commands);
.then(
FromClapHandler::new(add) FromClapHandler::new(add)
.clap() .clap()
.described() .described()
.with_description("add two numbers") .with_description("add two numbers")
.general("add"), .general("add")
) .add_to(&mut commands);
.build();
let commands = commands.build();
let clients = MultiClient::new(event_tx); let clients = MultiClient::new(event_tx);
clients clients

View file

@ -153,6 +153,13 @@ pub trait CommandExt: Sized {
fn clap(self) -> clap::Clap<Self> { fn clap(self) -> clap::Clap<Self> {
clap::Clap(self) clap::Clap(self)
} }
fn add_to<E>(self, commands: &mut Commands<E>)
where
Self: Command<E> + Send + Sync + 'static,
{
commands.add(self);
}
} }
// Sadly this doesn't work: `impl<E, C: Command<E>> CommandExt for C {}` // Sadly this doesn't work: `impl<E, C: Command<E>> CommandExt for C {}`