diff --git a/Cargo.toml b/Cargo.toml index e29711f..1b1cd11 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ simplelog = { version = "0.12.2", features = ["paris"] } log = { version = "0.4.22", features = ["std"] } nom = "7.1.3" criterion = "0.5" +distance = "0.4.0" [profile.dev] debug = 0 diff --git a/src/io.rs b/src/io.rs index 05456f0..92a3de0 100644 --- a/src/io.rs +++ b/src/io.rs @@ -20,6 +20,7 @@ //! ``` //! use crate::modules::handler; +use distance::levenshtein; use poise::serenity_prelude::{self as serenity}; /// User data, which is stored and accessible in all command invocations @@ -33,7 +34,7 @@ type Context<'a> = poise::Context<'a, Data, GenericError>; pub struct VoteContainer { title: String, description: String, - group: Option, + group: Option, hours: Option, short_name: Option, start_date: Option, @@ -157,7 +158,7 @@ pub async fn vote_modal( unwrapped_data.hours ); - log::info!("{:?}", ctx); + log::info!("{:?}", &ctx); // let group = unwrapped_data.group.unwrap_or("".into()); // let title = unwrapped_data.title; @@ -165,10 +166,27 @@ pub async fn vote_modal( // let short_name = unwrapped_data.short_name.unwrap_or("".into()); // let hours = unwrapped_data.hours.unwrap_or("".into()); + let saucisse = find_role_from_string( + unwrapped_data.group, + ctx.guild() + .unwrap() + .roles + .values() + .map(|v| v.name.clone()) + .collect(), + ) + .map(|find_role_from_string| { + ctx.guild() + .unwrap() + .role_by_name(&find_role_from_string) + .unwrap() + .id + }); + let serotoninenjoyer = VoteContainer { title: unwrapped_data.title, description: unwrapped_data.description, - group: unwrapped_data.group, + group: saucisse, hours: unwrapped_data.hours, short_name: unwrapped_data.short_name, identifier: VoteTypeIdentifier::WeeklyVote, @@ -187,6 +205,23 @@ pub async fn vote_modal( Ok(()) } +fn find_role_from_string(group: Option, roles: Vec) -> Option { + if group.is_none() || roles.is_empty() { + return None; + } + + let group = group.unwrap(); + let mut lowest: (Option, usize) = (None, group.len() + 5); + for role in roles { + let d = levenshtein(&role, &group); + if d < lowest.1 { + lowest = (Some(role), d); + }; + } + + lowest.0 +} + /// List of choices for all the roles. /// /// This enum is used to define a set of predefined choices that users can select from when creating a new vote. @@ -209,7 +244,7 @@ pub async fn vote_command( ctx: Context<'_>, #[description = "Titre"] title: String, #[description = "Nom du jeu"] short_name: Option, - #[description = "Groupe"] group: ListOfRoles, + #[description = "Groupe"] group: Option, #[description = "Description"] description: String, #[description = "Horaires"] hours: Option, #[description = "Date de début du vote"] start_date: Option,