From 269e4f77c92fd07dc0dc663af9fbcd8357c98567 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Mon, 14 Feb 2022 20:51:58 -0800 Subject: [PATCH] Add stroke order command --- src/commands/kanji.rs | 51 +++++++++++++++++++++++++++++++++++++++---- src/main.rs | 2 +- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/commands/kanji.rs b/src/commands/kanji.rs index d6f5ed7..f17ffd7 100644 --- a/src/commands/kanji.rs +++ b/src/commands/kanji.rs @@ -13,15 +13,32 @@ fn random_from_string(string: &str) -> char { } async fn display_kanji(ctx: &Context, msg: &Message, kanji: char, comment: &str) -> CommandResult { - msg.reply(&ctx.http, format!(":game_die: {}{}", kanji, comment)).await?; + msg.reply(&ctx.http, format!("{}{}", kanji, comment)).await?; + // Not all character codes are covered in mistval/kanji_images. + // The current implementation of 404-checking requires + // the loading of whatever content is hosted on the URL even though + // only the status code (404 for Not Found and 200 for OK) is needed. + // + // This may need to be disabled in production, since it makes + // response times feel sluggish. + // + // Possible ways of fixing this issue: + // - Store the entire mistval/kanji_images dataset locally and check for file presense + // (not desirable, since the remote repository may change) + // - Find a way to make requests but only get the status code + const VALIDATE_LINKS: bool = false; let url = format!("https://raw.githubusercontent.com/mistval/kanji_images/master/gifs/{}.gif", format!("{:x}", kanji as i32)); - let response = reqwest::get(&url) + let mut link_validated = true; + if VALIDATE_LINKS { + let response = reqwest::get(&url) .await? .text() .await?; - msg.channel_id.say(&ctx.http, if response == "404: Not Found" { + link_validated = response != "404: Not Found"; + } + msg.channel_id.say(&ctx.http, if link_validated { &url } else { "The stroke order diagram for this kanji is unavailable." - } else { &url }).await?; + }).await?; Ok(()) } @@ -107,4 +124,30 @@ async fn jlpt(ctx: &Context, msg: &Message, args: Args) -> CommandResult { #[command] async fn hyogai(ctx: &Context, msg: &Message, args: Args) -> CommandResult { random_kanji("HYOGAI", ctx, msg, args).await +} + +#[command] +async fn so(ctx: &Context, msg: &Message, args: Args) -> CommandResult { + const MAX_CHARS: i32 = 4; + let text = args.rest(); + if text.is_empty() { + msg.reply(&ctx.http, "Please provide some text you want the stroke order for.").await?; + return Ok(()); + } + let mut displayed_characters: Vec = Vec::new(); + let mut displayed_character_count = 0; + for character in text.chars() { + if displayed_character_count >= MAX_CHARS { + msg.channel_id.say(&ctx.http, ":warning: Maximum number of stroke order diagrams per command reached.").await?; + break; + } + if character.is_whitespace() || displayed_characters.contains(&character) { + continue; + } + // Don't show same character twice + displayed_characters.push(character); + displayed_character_count += 1; + display_kanji(&ctx, &msg, character, "").await?; + } + Ok(()) } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 71f0e12..91794b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,7 +43,7 @@ impl EventHandler for Handler { } #[group] -#[commands(joyo, jinmeiyo, kyoiku, jlpt, hyogai, sleep)] +#[commands(joyo, jinmeiyo, kyoiku, jlpt, hyogai, so, sleep)] struct General; #[tokio::main]