Implement Command for FullHelp

This commit is contained in:
Joscha 2023-02-26 19:58:48 +01:00
parent f5c5043896
commit 23d3166c2d
2 changed files with 47 additions and 26 deletions

View file

@ -14,6 +14,7 @@ Procedure when bumping the version number:
## Unreleased ## Unreleased
### Added ### Added
- `bot::botrulez::FullHelp` now implements `bot::command::Command`
- `bot::botrulez::Ping` now implements `bot::command::Command` - `bot::botrulez::Ping` now implements `bot::command::Command`
- `bot::botrulez::ShortHelp` now implements `bot::command::Command` - `bot::botrulez::ShortHelp` now implements `bot::command::Command`

View file

@ -2,18 +2,18 @@ use async_trait::async_trait;
use clap::Parser; use clap::Parser;
use crate::api::Message; use crate::api::Message;
use crate::bot::command::{ClapCommand, Context}; use crate::bot::command::{ClapCommand, Command, Context};
use crate::conn; use crate::conn;
/// Show full bot help.
#[derive(Parser)]
pub struct Args {}
pub struct FullHelp { pub struct FullHelp {
pub before: String, pub before: String,
pub after: String, pub after: String,
} }
pub trait HasDescriptions {
fn descriptions(&self, ctx: &Context) -> Vec<String>;
}
impl FullHelp { impl FullHelp {
pub fn new<S1: ToString, S2: ToString>(before: S1, after: S2) -> Self { pub fn new<S1: ToString, S2: ToString>(before: S1, after: S2) -> Self {
Self { Self {
@ -21,27 +21,8 @@ impl FullHelp {
after: after.to_string(), after: after.to_string(),
} }
} }
}
pub trait HasDescriptions { fn formulate_reply<B: HasDescriptions>(&self, ctx: &Context, bot: &B) -> String {
fn descriptions(&self, ctx: &Context) -> Vec<String>;
}
#[async_trait]
impl<B, E> ClapCommand<B, E> for FullHelp
where
B: HasDescriptions + Send,
E: From<conn::Error>,
{
type Args = Args;
async fn execute(
&self,
_args: Self::Args,
msg: &Message,
ctx: &Context,
bot: &mut B,
) -> Result<(), E> {
let mut result = String::new(); let mut result = String::new();
if !self.before.is_empty() { if !self.before.is_empty() {
@ -59,7 +40,46 @@ where
result.push('\n'); result.push('\n');
} }
ctx.reply(msg.id, result).await?; result
}
}
#[async_trait]
impl<B, E> Command<B, E> for FullHelp
where
B: HasDescriptions + Send,
E: From<conn::Error>,
{
async fn execute(&self, arg: &str, msg: &Message, ctx: &Context, bot: &mut B) -> Result<(), E> {
if arg.trim().is_empty() {
let reply = self.formulate_reply(ctx, bot);
ctx.reply(msg.id, reply).await?;
}
Ok(())
}
}
/// Show full bot help.
#[derive(Parser)]
pub struct Args {}
#[async_trait]
impl<B, E> ClapCommand<B, E> for FullHelp
where
B: HasDescriptions + Send,
E: From<conn::Error>,
{
type Args = Args;
async fn execute(
&self,
_args: Self::Args,
msg: &Message,
ctx: &Context,
bot: &mut B,
) -> Result<(), E> {
let reply = self.formulate_reply(ctx, bot);
ctx.reply(msg.id, reply).await?;
Ok(()) Ok(())
} }
} }