From 7c2ce0ab3cc3cd926fa05135781e37b8410fc079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luukas=20P=C3=B6rtfors?= Date: Tue, 2 Jan 2024 20:31:02 +0200 Subject: [PATCH] fix(commands/vote): problem with unicode characters --- src/commands/vote.rs | 25 +++++++++++++++---------- src/database/vote.rs | 2 +- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/commands/vote.rs b/src/commands/vote.rs index 6eeee99..bbb5f91 100644 --- a/src/commands/vote.rs +++ b/src/commands/vote.rs @@ -5,8 +5,8 @@ use serenity::{ http::Http, model::{ application::interaction::{ - InteractionResponseType::DeferredUpdateMessage, application_command::ApplicationCommandInteraction, + InteractionResponseType::DeferredUpdateMessage, }, interactions::{ message_component::{ButtonStyle, MessageComponentInteraction}, @@ -175,10 +175,10 @@ pub async fn create_vote(ctx: &Context, interaction: ApplicationCommandInteracti title.truncate(255); let mut options = options .split(',') - .map(|o| &o[0..std::cmp::min(o.len(), 32)]) - .collect::>(); + .map(|o| o.trim().chars().take(32).collect::()) + .filter(|o| !o.is_empty()) + .collect::>(); options.dedup(); - options.retain(|o| !o.trim().is_empty()); if options.len() < 2 { interaction .create_interaction_response(&ctx.http, |r| { @@ -211,10 +211,13 @@ pub async fn create_vote(ctx: &Context, interaction: ApplicationCommandInteracti .get_vote_event_from_message_id(vote_message.id.0) .unwrap(); let vote_options = db.get_options_by_vote_id(vote_id).unwrap(); - let Ok(_) = vote_message.edit(&ctx.http, |m| { - generate_vote_message(m, vote_event, Vec::new(), vote_options, &interaction.user); - m - }).await else { + let Ok(_) = vote_message + .edit(&ctx.http, |m| { + generate_vote_message(m, vote_event, Vec::new(), vote_options, &interaction.user); + m + }) + .await + else { db.purge_vote(vote_id).unwrap(); interaction .create_interaction_response(&ctx.http, |r| { @@ -222,8 +225,10 @@ pub async fn create_vote(ctx: &Context, interaction: ApplicationCommandInteracti d.flags(InteractionApplicationCommandCallbackDataFlags::EPHEMERAL); d.content("Invalid request, perhaps included too many options") }) - }).await.unwrap(); - return vote_message.delete(&ctx.http).await.unwrap(); + }) + .await + .unwrap(); + return vote_message.delete(&ctx.http).await.unwrap(); }; interaction .create_interaction_response(&ctx.http, |r| { diff --git a/src/database/vote.rs b/src/database/vote.rs index a0775a0..2253a84 100644 --- a/src/database/vote.rs +++ b/src/database/vote.rs @@ -11,7 +11,7 @@ impl Database { author_id: u64, title: &str, duration: u32, - options: Vec<&str>, + options: Vec, ) -> Result { let event = NewVoteEvent { channel_id,