Added more functionality

This commit is contained in:
2025-07-30 17:55:44 -05:00
parent fd87302642
commit 4841ab9f06
3 changed files with 68 additions and 13 deletions

2
Cargo.lock generated
View File

@ -4153,7 +4153,7 @@ dependencies = [
[[package]] [[package]]
name = "watcat" name = "watcat"
version = "0.1.0" version = "0.2.0"
dependencies = [ dependencies = [
"arabic_reshaper", "arabic_reshaper",
"dotenvy", "dotenvy",

View File

@ -28,7 +28,9 @@ init the database ``sqlx migrate run``
TODO TODO
---- ----
+ ~~literally just add the text~~ + ~~add art and lastfm url fetching~~
+ add easter egg + 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 + make various parameters user-configurable
+ use tiny-skia instead of magick-rust? + use tiny-skia instead of magick-rust?

View File

@ -114,6 +114,50 @@ fn validate_color(col: &PixelWand) -> Option<Lab> {
Some(Lab::<D65>::from_components(color_raw)) Some(Lab::<D65>::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::<String, String>::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::<String, String>::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<String>) -> Reply { async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option<String>) -> Reply {
let lastfm_user = match arg { let lastfm_user = match arg {
"" => get_lastfm_username(ctx, id).await, "" => get_lastfm_username(ctx, id).await,
@ -143,7 +187,7 @@ async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option<String>) -> Re
let image = match get_image(ctx, image_uri.as_str()).await { let image = match get_image(ctx, image_uri.as_str()).await {
Ok(i) => i, 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 base_color = PixelWand::new();
let mut white = PixelWand::new(); let mut white = PixelWand::new();
@ -335,7 +379,7 @@ async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option<String>) -> Re
async fn set(ctx: &Context, arg: &str, id: UserId) -> Reply { async fn set(ctx: &Context, arg: &str, id: UserId) -> Reply {
set_lastfm_username(ctx, id, arg.to_string()).await; set_lastfm_username(ctx, id, arg.to_string()).await;
Reply::Text(format!("set user {}", arg)) Reply::Text(format!("set user {arg}"))
} }
#[async_trait] #[async_trait]
@ -343,16 +387,25 @@ impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) { async fn message(&self, ctx: Context, msg: Message) {
let mut cmd_iter = msg.content.split(" "); let mut cmd_iter = msg.content.split(" ");
let cmd = cmd_iter.next().unwrap_or(""); let cmd = cmd_iter.next().unwrap_or("");
let arg = cmd_iter.next().unwrap_or(""); let arg1 = cmd_iter.next().unwrap_or("");
let resp = match cmd { let arg2 = cmd_iter.next().unwrap_or("");
".fmi" => { let resp = match (cmd, arg1, arg2) {
log!("{} received", msg.content); (".set", arg, "") | (".k", "set", arg) | (".kset", arg, "") => {
Some(fmi(&ctx, arg, msg.author.id, msg.author.avatar_url()).await)
}
".set" => {
log!("{} received", msg.content); log!("{} received", msg.content);
Some(set(&ctx, arg, msg.author.id).await) 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, _ => None,
}; };
if let Some(reply) = resp { if let Some(reply) = resp {