diff --git a/Cargo.lock b/Cargo.lock index 2bb44fc..a3ee3d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4153,7 +4153,7 @@ dependencies = [ [[package]] name = "watcat" -version = "0.1.0" +version = "0.2.0" dependencies = [ "arabic_reshaper", "dotenvy", diff --git a/README.md b/README.md index 5936be5..022c756 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,9 @@ init the database ``sqlx migrate run`` TODO ---- -+ ~~literally just add the text~~ -+ add easter egg ++ ~~add art and lastfm url fetching~~ ++ add help command ++ refactor to combine repeated parts of art, url, and fmi. also use macro for commands, and use Option instead of empty string sentinel value ++ add spotify link fetching + make various parameters user-configurable + use tiny-skia instead of magick-rust? diff --git a/src/main.rs b/src/main.rs index 9fbadac..a425fa2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -114,6 +114,50 @@ fn validate_color(col: &PixelWand) -> Option { Some(Lab::::from_components(color_raw)) } +async fn art(ctx: &Context, arg: &str, id: UserId) -> Reply { + let lastfm_user = match arg { + "" => get_lastfm_username(ctx, id).await, + _ => Some(arg.to_string()), + }; + let lastfm_client = match lastfm_user { + Some(s) => lastfm::Client::::from_env(s), + None => return Reply::Text("No last.fm username set.".to_string()), + }; + let now_playing = match lastfm_client.now_playing().await { + Ok(np) => np, + Err(e) => return Reply::Text(format!("Error: grabbing last.fm user data failed {e}")), + }; + let track = match now_playing { + Some(track) => track, + None => return Reply::Text("Nothing playing.".to_string()), + }; + let track_art = match track.image.extralarge { + Some(track_art) => track_art, + None => return Reply::Text("Error: getting image uri failed".to_string()), + }; + Reply::Text(track_art.to_string()) +} + +async fn url(ctx: &Context, arg: &str, id: UserId) -> Reply { + let lastfm_user = match arg { + "" => get_lastfm_username(ctx, id).await, + _ => Some(arg.to_string()), + }; + let lastfm_client = match lastfm_user { + Some(s) => lastfm::Client::::from_env(s), + None => return Reply::Text("No last.fm username set.".to_string()), + }; + let now_playing = match lastfm_client.now_playing().await { + Ok(np) => np, + Err(e) => return Reply::Text(format!("Error: grabbing last.fm user data failed {e}")), + }; + let track = match now_playing { + Some(track) => track, + None => return Reply::Text("Nothing playing.".to_string()), + }; + Reply::Text(track.url) +} + async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option) -> Reply { let lastfm_user = match arg { "" => get_lastfm_username(ctx, id).await, @@ -143,7 +187,7 @@ async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option) -> Re let image = match get_image(ctx, image_uri.as_str()).await { Ok(i) => i, - Err(e) => return Reply::Text(format!("{}", e)), + Err(e) => return Reply::Text(format!("{e}")), }; let mut base_color = PixelWand::new(); let mut white = PixelWand::new(); @@ -335,7 +379,7 @@ async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option) -> Re async fn set(ctx: &Context, arg: &str, id: UserId) -> Reply { set_lastfm_username(ctx, id, arg.to_string()).await; - Reply::Text(format!("set user {}", arg)) + Reply::Text(format!("set user {arg}")) } #[async_trait] @@ -343,16 +387,25 @@ impl EventHandler for Handler { async fn message(&self, ctx: Context, msg: Message) { let mut cmd_iter = msg.content.split(" "); let cmd = cmd_iter.next().unwrap_or(""); - let arg = cmd_iter.next().unwrap_or(""); - let resp = match cmd { - ".fmi" => { - log!("{} received", msg.content); - Some(fmi(&ctx, arg, msg.author.id, msg.author.avatar_url()).await) - } - ".set" => { + let arg1 = cmd_iter.next().unwrap_or(""); + let arg2 = cmd_iter.next().unwrap_or(""); + let resp = match (cmd, arg1, arg2) { + (".set", arg, "") | (".k", "set", arg) | (".kset", arg, "") => { log!("{} received", msg.content); Some(set(&ctx, arg, msg.author.id).await) - } + }, + (".k", "art", arg) | (".k", arg, "art") | (".ka" | ".kart", arg, "") => { + log!("{} received", msg.content); + Some(art(&ctx, arg, msg.author.id).await) + }, + (".k", "url" | "uri" | "link", arg) | (".k", arg, "url" | "uri" | "link") | (".ku" | ".kl" | ".kurl", arg, "") => { + log!("{} received", msg.content); + Some(url(&ctx, arg, msg.author.id).await) + }, + (".fmi" | ".k" | ".kf" | ".ki" | ".kfmi", arg, "") | (".k", "fm" | "fmi" | "get" | "img", arg) => { + log!("{} received", msg.content); + Some(fmi(&ctx, arg, msg.author.id, msg.author.avatar_url()).await) + }, _ => None, }; if let Some(reply) = resp {