improve bot command interface

This commit is contained in:
Pierre de Lacroix 2026-06-27 18:38:44 +02:00
parent 3210581a22
commit 25af0af6f1
Signed by: lateralus23
GPG key ID: 53E0CEC29C24EF39
3 changed files with 64 additions and 43 deletions

View file

@ -1,4 +1,4 @@
use crate::grist::Shift;
use crate::grist::{Priority, Shift};
use clap::{CommandFactory, Parser, Subcommand};
use matrix_sdk::{Client, Room};
@ -14,16 +14,24 @@ struct Clap {
#[derive(Debug, Subcommand)]
enum Action {
/// Lister les shifts
Lister,
#[command(name = "lister")]
List,
/// Ajouter un shift
Ajouter {
#[command(name = "ajouter")]
Add {
/// Le nom du shift
nom: String,
/// Plus d'informations
infos: String,
#[arg(id = "NOM")]
name: String,
/// Date de début, sous ce format: J#-HH-MM (ex: J1-14-30)
#[arg(id = "DATE", short = 'd', long = "date")]
starting_time: String,
/// Priorité
#[arg(id = "PRIORITÉ", short = 'p', long = "priorité", value_enum)]
priority: Priority,
},
/// Obtenir de l'aide
Aide,
}
pub async fn send_room_msg(_client: Client, room: Room, content: String) -> Res<OwnedEventId> {
@ -62,26 +70,32 @@ pub async fn handle(command: impl AsRef<str>, room: Room, client: Client) -> Res
let args = shlex::split(command.as_ref()).ok_or_else(lazyhow!("error: Invalid quoting"))?;
let mut command = Clap::command();
match command.try_get_matches_from_mut(&args) {
Ok(_) => {
let clap = Clap::try_parse_from(args)?;
match clap.action {
Action::Lister => {
Action::List => {
debug!("user asked for list");
let shifts = crate::grist::list_shifts().await?;
debug!("{:?}", shifts);
send_room_msg_html(client, room, format_shifts(shifts)).await?;
}
Action::Ajouter { nom, infos } => {
Action::Add {
name,
starting_time,
priority,
} => {
debug!("user asked for add");
}
Action::Aide => {
debug!("user asked for help");
let mut command = Clap::command();
let help = command.render_long_help();
debug!("{}", help);
send_room_msg(client, room, help.to_string()).await?;
}
}
Err(err) => {
debug!("{:?}", err);
send_room_msg(client.clone(), room.clone(), format!("{}", err)).await?;
}
};
Ok(())
}

View file

@ -1,6 +1,8 @@
prelude! {}
use chrono::DateTime;
use clap::ValueEnum;
use clap::builder::PossibleValue;
use grist_client::apis::configuration::Configuration;
use grist_client::apis::records_api::list_records;
use grist_client::models::{RecordsList, RecordsListRecordsInner};
@ -22,14 +24,29 @@ pub enum Priority {
impl Display for Priority {
fn fmt(&self, f: &mut Fmt<'_>) -> FmtRes {
match self {
Priority::Prioritaire => write!(f, "<b>Prioritaire</b>"),
Priority::Secondaire => write!(f, "Secondaire"),
Priority::Bonus => write!(f, "Bonus"),
Priority::Urgent => write!(f, "<font color=\"red\">Prioritaire</font>"),
Self::Prioritaire => write!(f, "<b>Prioritaire</b>"),
Self::Secondaire => write!(f, "Secondaire"),
Self::Bonus => write!(f, "Bonus"),
Self::Urgent => write!(f, "<font color=\"red\">Prioritaire</font>"),
}
}
}
impl ValueEnum for Priority {
fn value_variants<'a>() -> &'a [Self] {
&[Self::Prioritaire, Self::Secondaire, Self::Bonus, Self::Urgent]
}
fn to_possible_value(&self) -> Option<PossibleValue> {
Some(match self {
Self::Prioritaire => PossibleValue::new("1"),
Self::Secondaire => PossibleValue::new("2"),
Self::Bonus => PossibleValue::new("3"),
Self::Urgent => PossibleValue::new("0"),
})
}
}
#[derive(Clone, Debug)]
pub struct ShiftType {
pub name: String,

View file

@ -24,7 +24,7 @@ prelude! {}
#[command(version, about)]
struct Clap {
/// Verbosity level.
#[arg(short, long, value_name = "NATURAL", default_value_t = 2)]
#[arg(short, long, default_value_t = 2)]
verb: usize,
/// Wipe the static data directory before doing anything.
@ -32,25 +32,15 @@ struct Clap {
wipe: bool,
/// Identifier of the element bot account to run.
#[arg(
short,
long,
value_name = "MATRIX_USERNAME",
default_value = "shift_bot_dev"
)]
#[arg(short, long, default_value = "shift_bot_dev")]
username: String,
/// Homeserver.
#[arg(
short = 's',
long,
value_name = "MATRIX_HOMESERVER",
default_value = "matrix.org"
)]
#[arg(short = 's', long, default_value = "matrix.org")]
homeserver: String,
/// Password of the element bot account.
#[arg(short, long, value_name = "PASSWORD")]
#[arg(short, long)]
password: String,
/// Grist APIKEY