added role finding logic to modal

This commit is contained in:
Vicky 2024-11-05 15:10:20 +01:00
parent 8860dee7e6
commit 656a75b224
2 changed files with 40 additions and 4 deletions

View File

@ -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

View File

@ -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<String>,
group: Option<serenity::RoleId>,
hours: Option<String>,
short_name: Option<String>,
start_date: Option<String>,
@ -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<String>, roles: Vec<String>) -> Option<String> {
if group.is_none() || roles.is_empty() {
return None;
}
let group = group.unwrap();
let mut lowest: (Option<String>, 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<String>,
#[description = "Groupe"] group: ListOfRoles,
#[description = "Groupe"] group: Option<serenity::RoleId>,
#[description = "Description"] description: String,
#[description = "Horaires"] hours: Option<String>,
#[description = "Date de début du vote"] start_date: Option<String>,