Added avatar display
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
/target
|
/target
|
||||||
/.env
|
/.env
|
||||||
/watcat.db*
|
/watcat.db*
|
||||||
|
/http-cacache
|
||||||
|
|||||||
723
Cargo.lock
generated
723
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -21,3 +21,6 @@ features = ["macros", "rt-multi-thread"]
|
|||||||
[dependencies.sqlx]
|
[dependencies.sqlx]
|
||||||
version = "0.8.5"
|
version = "0.8.5"
|
||||||
features = ["sqlite", "runtime-tokio"]
|
features = ["sqlite", "runtime-tokio"]
|
||||||
|
|
||||||
|
[dependencies.imagetext]
|
||||||
|
git = "https://github.com/nathanielfernandes/imagetext"
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 1008 B After Width: | Height: | Size: 1008 B |
BIN
src/avatar_mask.png
Normal file
BIN
src/avatar_mask.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.9 KiB |
85
src/main.rs
85
src/main.rs
@ -49,8 +49,7 @@ macro_rules! handle_magick_option {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused)]
|
async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option<String>) -> Reply {
|
||||||
async fn fmi(ctx: &Context, arg: &str, id: UserId) -> Reply {
|
|
||||||
let lastfm_user = match arg {
|
let lastfm_user = match arg {
|
||||||
"" => get_lastfm_username(ctx, id).await,
|
"" => get_lastfm_username(ctx, id).await,
|
||||||
_ => Some(arg.to_string()),
|
_ => Some(arg.to_string()),
|
||||||
@ -72,19 +71,16 @@ async fn fmi(ctx: &Context, arg: &str, id: UserId) -> Reply {
|
|||||||
None => return Reply::Text("Error: getting image uri failed".to_string()),
|
None => return Reply::Text("Error: getting image uri failed".to_string()),
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
|
||||||
println!("{}", image_uri);
|
|
||||||
|
|
||||||
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();
|
||||||
let mut main_wand = MagickWand::new();
|
let main_wand = MagickWand::new();
|
||||||
let mut art_wand = MagickWand::new();
|
let art_wand = MagickWand::new();
|
||||||
let mut art_wand_cluster = MagickWand::new();
|
let mut art_wand_cluster = MagickWand::new();
|
||||||
let mut mask_wand = MagickWand::new();
|
let mask_wand = MagickWand::new();
|
||||||
|
|
||||||
handle_magick_result!(
|
handle_magick_result!(
|
||||||
base_color.set_color("#7f7f7f"),
|
base_color.set_color("#7f7f7f"),
|
||||||
@ -122,16 +118,57 @@ async fn fmi(ctx: &Context, arg: &str, id: UserId) -> Reply {
|
|||||||
"Failed to get color histogram"
|
"Failed to get color histogram"
|
||||||
);
|
);
|
||||||
colors.sort_by_cached_key(|color| -(color.get_color_count() as isize));
|
colors.sort_by_cached_key(|color| -(color.get_color_count() as isize));
|
||||||
let mut accent_color = match colors.len() {
|
let accent_color = match colors.len() {
|
||||||
1 => &colors[0],
|
1 => &colors[0],
|
||||||
_ => &colors[1],
|
_ => &colors[1],
|
||||||
};
|
};
|
||||||
|
|
||||||
handle_magick_result!(white.set_color("#ffffff"), "Failed to set init white");
|
handle_magick_result!(white.set_color("#ffffff"), "Failed to init white");
|
||||||
handle_magick_result!(mask_wand.read_image("mask.png"), "Failed to load mask");
|
handle_magick_result!(
|
||||||
|
mask_wand.read_image_blob(include_bytes!("accent_mask.png")),
|
||||||
|
"Failed to load mask"
|
||||||
|
);
|
||||||
handle_magick_result!(
|
handle_magick_result!(
|
||||||
mask_wand.opaque_paint_image(&white, accent_color, 0.0, false),
|
mask_wand.opaque_paint_image(&white, accent_color, 0.0, false),
|
||||||
"Failed to paint mask"
|
"Failed to paint accent mask"
|
||||||
|
);
|
||||||
|
|
||||||
|
let avatar_wand = MagickWand::new();
|
||||||
|
match avatar {
|
||||||
|
Some(avatar_uri) => {
|
||||||
|
if let Ok(avatar) = get_image(ctx, avatar_uri.as_str()).await {
|
||||||
|
handle_magick_result!(
|
||||||
|
avatar_wand.read_image_blob(avatar),
|
||||||
|
"Failed to load user avatar"
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
handle_magick_result!(
|
||||||
|
avatar_wand.new_image(100, 100, &white),
|
||||||
|
"Failed to load dummy avatar"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => handle_magick_result!(
|
||||||
|
avatar_wand.new_image(100, 100, &white),
|
||||||
|
"Failed to load dummy avatar"
|
||||||
|
),
|
||||||
|
}
|
||||||
|
handle_magick_result!(
|
||||||
|
avatar_wand.transform_image_colorspace(ColorspaceType::Lab),
|
||||||
|
"Failed to set avatar colorspace"
|
||||||
|
);
|
||||||
|
let avatar_mask_wand = MagickWand::new();
|
||||||
|
handle_magick_result!(
|
||||||
|
avatar_mask_wand.read_image_blob(include_bytes!("avatar_mask.png")),
|
||||||
|
"Failed to load avatar mask"
|
||||||
|
);
|
||||||
|
handle_magick_result!(
|
||||||
|
avatar_wand.compose_images(&avatar_mask_wand, CompositeOperator::CopyAlpha, false, 0, 0),
|
||||||
|
"Failed to mask avatar"
|
||||||
|
);
|
||||||
|
handle_magick_result!(
|
||||||
|
avatar_wand.adaptive_resize_image(64, 64),
|
||||||
|
"Failed to resize avatar"
|
||||||
);
|
);
|
||||||
|
|
||||||
handle_magick_result!(
|
handle_magick_result!(
|
||||||
@ -152,22 +189,16 @@ async fn fmi(ctx: &Context, arg: &str, id: UserId) -> Reply {
|
|||||||
);
|
);
|
||||||
handle_magick_result!(
|
handle_magick_result!(
|
||||||
main_wand.compose_images(&art_wand, CompositeOperator::SrcOver, false, 12, 12),
|
main_wand.compose_images(&art_wand, CompositeOperator::SrcOver, false, 12, 12),
|
||||||
"Failed to combine images"
|
"Failed to combine art image"
|
||||||
);
|
|
||||||
handle_magick_result!(
|
|
||||||
main_wand.compose_images(
|
|
||||||
&art_wand_cluster,
|
|
||||||
CompositeOperator::SrcOver,
|
|
||||||
false,
|
|
||||||
160,
|
|
||||||
12
|
|
||||||
),
|
|
||||||
"Failed to combine debug images"
|
|
||||||
);
|
);
|
||||||
handle_magick_result!(
|
handle_magick_result!(
|
||||||
main_wand.compose_images(&mask_wand, CompositeOperator::SrcOver, false, 401, 0),
|
main_wand.compose_images(&mask_wand, CompositeOperator::SrcOver, false, 401, 0),
|
||||||
"Failed to combine mask"
|
"Failed to combine mask"
|
||||||
);
|
);
|
||||||
|
handle_magick_result!(
|
||||||
|
main_wand.compose_images(&avatar_wand, CompositeOperator::SrcOver, false, 473, 73),
|
||||||
|
"Failed to combine avatar image"
|
||||||
|
);
|
||||||
Reply::Image(main_wand.write_image_blob("png").unwrap())
|
Reply::Image(main_wand.write_image_blob("png").unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -183,7 +214,7 @@ impl EventHandler for Handler {
|
|||||||
let cmd = cmd_iter.next().unwrap_or("");
|
let cmd = cmd_iter.next().unwrap_or("");
|
||||||
let arg = cmd_iter.next().unwrap_or("");
|
let arg = cmd_iter.next().unwrap_or("");
|
||||||
let resp = match cmd {
|
let resp = match cmd {
|
||||||
".fmi" => Some(fmi(&ctx, arg, msg.author.id).await),
|
".fmi" => Some(fmi(&ctx, arg, msg.author.id, msg.author.avatar_url()).await),
|
||||||
".set" => Some(set(&ctx, arg, msg.author.id).await),
|
".set" => Some(set(&ctx, arg, msg.author.id).await),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
@ -261,6 +292,8 @@ async fn set_lastfm_username(ctx: &Context, id: UserId, user: String) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn get_image(ctx: &Context, url: &str) -> Result<Vec<u8>, reqwest_middleware::Error> {
|
async fn get_image(ctx: &Context, url: &str) -> Result<Vec<u8>, reqwest_middleware::Error> {
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
println!("debug: get {}", url);
|
||||||
let data = ctx.data.write().await;
|
let data = ctx.data.write().await;
|
||||||
let http = data
|
let http = data
|
||||||
.get::<HttpContainer>()
|
.get::<HttpContainer>()
|
||||||
@ -268,7 +301,7 @@ async fn get_image(ctx: &Context, url: &str) -> Result<Vec<u8>, reqwest_middlewa
|
|||||||
match http.get(url).send().await {
|
match http.get(url).send().await {
|
||||||
Ok(resp) => {
|
Ok(resp) => {
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
println!("{:?}", resp);
|
println!("debug: response received");
|
||||||
Ok(resp
|
Ok(resp
|
||||||
.bytes()
|
.bytes()
|
||||||
.await
|
.await
|
||||||
@ -316,7 +349,5 @@ async fn main() {
|
|||||||
|
|
||||||
if let Err(why) = discord_client.start().await {
|
if let Err(why) = discord_client.start().await {
|
||||||
println!("Client error: {why:?}");
|
println!("Client error: {why:?}");
|
||||||
} else {
|
|
||||||
println!("watcat started");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user