From 1849a5c16d3c0acfc955cdd343c58a45115b893b Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Wed, 16 Feb 2022 12:52:22 -0800 Subject: [PATCH] Add images command --- Cargo.toml | 1 + src/commands/challenge.rs | 45 ++++++++++++++++++++++++++++++++++++++- src/main.rs | 2 +- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0640b23..9e70577 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ tracing-subscriber = "0.3.8" serde_json = "1.0.79" reqwest = "0.11.9" slug = "0.1.4" +unicode_hfwidth = "0.2.0" [dependencies.tokio] version = "1.0" diff --git a/src/commands/challenge.rs b/src/commands/challenge.rs index 706ddcd..b98fab0 100644 --- a/src/commands/challenge.rs +++ b/src/commands/challenge.rs @@ -79,6 +79,21 @@ fn set_submission_data(submission_data: Vec) { .unwrap(); } +fn is_matching_submission(submission: &Value, msg: &Message) -> bool { + submission["id"].as_str().unwrap() == msg.author.id.as_u64().to_string() +} + +fn to_fullwidth(string: &str) -> String { + let mut fullwidth = String::new(); + for character in string.chars() { + fullwidth.push(match unicode_hfwidth::to_fullwidth(character) { + Some(character) => character, + None => character, + }); + } + fullwidth +} + #[command] async fn challenge(ctx: &Context, msg: &Message) -> CommandResult { msg.reply( @@ -114,7 +129,7 @@ async fn submit(ctx: &Context, msg: &Message) -> CommandResult { let mut invalid_types = false; let mut requires_rebuild = false; for (i, submission) in submission_data.iter_mut().enumerate() { - if &submission["id"].as_str().unwrap() == &msg.author.id.as_u64().to_string() { + if is_matching_submission(&submission, &msg) { existing_submitter = true; let mut images = submission["images"].as_array_mut().unwrap().clone(); for attachment in msg.attachments.iter() { @@ -213,4 +228,32 @@ async fn submit(ctx: &Context, msg: &Message) -> CommandResult { } msg.reply(&ctx.http, &message).await?; Ok(()) +} + +#[command] +async fn images(ctx: &Context, msg: &Message) -> CommandResult { + let submission_data = get_submission_data(); + let images: Vec = { + let mut images = Vec::new(); + for submission in submission_data.iter() { + if is_matching_submission(&submission, &msg) { + for image in submission["images"].as_array().unwrap().iter() { + images.push(String::from(image.as_str().unwrap())); + } + break; + } + } + images + }; + let challenge_number = get_challenge_number(); + if images.len() == 0 { + msg.reply(&ctx.http, format!("You haven't submitted anything for Tegaki Tuesday #{}.", challenge_number)).await?; + return Ok(()) + } + let mut message = String::from(format!("Your submission images for Tegaki Tuesday #{}:\n", challenge_number)); + for (i, image) in images.iter().enumerate() { + message.push_str(&format!("({})\n", to_fullwidth(&(i + 1).to_string()), challenge_number, image)); + } + msg.reply(&ctx.http, message).await?; + Ok(()) } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 4b8dc05..d9ac593 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,7 +43,7 @@ impl EventHandler for Handler { } #[group] -#[commands(joyo, jinmeiyo, kyoiku, jlpt, hyogai, so, challenge, submit, sleep)] +#[commands(joyo, jinmeiyo, kyoiku, jlpt, hyogai, so, challenge, submit, images, sleep)] struct General; #[tokio::main]