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 clap::{CommandFactory, Parser, Subcommand};
use matrix_sdk::{Client, Room}; use matrix_sdk::{Client, Room};
@ -14,16 +14,24 @@ struct Clap {
#[derive(Debug, Subcommand)] #[derive(Debug, Subcommand)]
enum Action { enum Action {
/// Lister les shifts /// Lister les shifts
Lister, #[command(name = "lister")]
List,
/// Ajouter un shift /// Ajouter un shift
Ajouter { #[command(name = "ajouter")]
Add {
/// Le nom du shift /// Le nom du shift
nom: String, #[arg(id = "NOM")]
/// Plus d'informations name: String,
infos: 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> { 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 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)?; let clap = Clap::try_parse_from(args)?;
match clap.action { match clap.action {
Action::Lister => { Action::List => {
debug!("user asked for list"); debug!("user asked for list");
let shifts = crate::grist::list_shifts().await?; let shifts = crate::grist::list_shifts().await?;
debug!("{:?}", shifts); debug!("{:?}", shifts);
send_room_msg_html(client, room, format_shifts(shifts)).await?; 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"); 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(()) Ok(())
} }

View file

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

View file

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