|
|
|
@ -2,7 +2,7 @@ extern crate dotenvy;
|
|
|
|
extern crate lastfm;
|
|
|
|
extern crate lastfm;
|
|
|
|
extern crate reqwest;
|
|
|
|
extern crate reqwest;
|
|
|
|
extern crate reqwest_middleware;
|
|
|
|
extern crate reqwest_middleware;
|
|
|
|
use http_cache_reqwest::{CACacheManager, Cache, CacheMode, HttpCache, HttpCacheOptions};
|
|
|
|
use http_cache_reqwest::{CACacheManager, Cache, CacheMode, HttpCache, HttpCacheOptions,};
|
|
|
|
|
|
|
|
|
|
|
|
use serenity::async_trait;
|
|
|
|
use serenity::async_trait;
|
|
|
|
use serenity::builder::{CreateAttachment, CreateMessage};
|
|
|
|
use serenity::builder::{CreateAttachment, CreateMessage};
|
|
|
|
@ -16,6 +16,7 @@ use magick_rust::{
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::env;
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::{Arc, Once};
|
|
|
|
use std::sync::{Arc, Once};
|
|
|
|
use tokio::sync::Mutex;
|
|
|
|
use tokio::sync::Mutex;
|
|
|
|
|
|
|
|
|
|
|
|
@ -69,8 +70,9 @@ macro_rules! log {
|
|
|
|
macro_rules! handle_magick_result {
|
|
|
|
macro_rules! handle_magick_result {
|
|
|
|
($a: expr, $b: literal) => {
|
|
|
|
($a: expr, $b: literal) => {
|
|
|
|
match $a {
|
|
|
|
match $a {
|
|
|
|
Ok(_) => {
|
|
|
|
Ok(a) => {
|
|
|
|
log!("{} run successfully", stringify!($a));
|
|
|
|
log!("{} run successfully", stringify!($a));
|
|
|
|
|
|
|
|
a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => return Reply::Text(format!("Error: {} {}", $b, e)),
|
|
|
|
Err(e) => return Reply::Text(format!("Error: {} {}", $b, e)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -174,33 +176,22 @@ async fn spot(ctx: &Context, arg: &str, id: UserId) -> Reply {
|
|
|
|
Err(e) => return e,
|
|
|
|
Err(e) => return e,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
let mut data = ctx.data.write().await;
|
|
|
|
let mut data = ctx.data.write().await;
|
|
|
|
let spotify_option = data
|
|
|
|
let spotify_option = data.get_mut::<SpotifyHaver>().expect("Failed to have spotify option");
|
|
|
|
.get_mut::<SpotifyHaver>()
|
|
|
|
|
|
|
|
.expect("Failed to have spotify option");
|
|
|
|
|
|
|
|
if spotify_option.is_none() {
|
|
|
|
if spotify_option.is_none() {
|
|
|
|
return Reply::Text("Unable to use Spotify command: Contact bot Administrator.".to_owned());
|
|
|
|
return Reply::Text("Unable to use Spotify command: Contact bot Administrator.".to_owned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let spotify_arc_mutex = spotify_option.as_mut().unwrap();
|
|
|
|
let spotify_arc_mutex = spotify_option.as_mut().unwrap();
|
|
|
|
//let spotify_arc_mutex_ref = Arc::clone(&spotify_arc_mutex);
|
|
|
|
|
|
|
|
let spotify_client = spotify_arc_mutex.lock().await;
|
|
|
|
let spotify_client = spotify_arc_mutex.lock().await;
|
|
|
|
let search = spotify_client
|
|
|
|
spotify_client.request_token().await.unwrap();
|
|
|
|
.search(
|
|
|
|
let search = spotify_client.search(format!("{} {}", track.name, track.artist.name).as_str(), SearchType::Track, None, None, None, None).await;
|
|
|
|
format!(r#"track:"{}" album:"{}" artist:"{}""#, track.name.replace("\"", ""), track.album.replace("\"", ""), track.artist.name.replace("\"", "")).as_str(),
|
|
|
|
|
|
|
|
SearchType::Track,
|
|
|
|
|
|
|
|
None,
|
|
|
|
|
|
|
|
None,
|
|
|
|
|
|
|
|
None,
|
|
|
|
|
|
|
|
None,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let tracks = match search {
|
|
|
|
let tracks = match search {
|
|
|
|
Ok(SearchResult::Tracks(track_page)) => track_page,
|
|
|
|
Ok(SearchResult::Tracks(track_page)) => track_page,
|
|
|
|
Err(e) => return Reply::Text(format!("Failed to get track {e}")),
|
|
|
|
Err(e) => return Reply::Text(format!("Failed to get track {e}")),
|
|
|
|
_ => return Reply::Text("Spotify search failed".to_owned()),
|
|
|
|
_ => return Reply::Text("Spotify search failed".to_owned()),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
match &tracks.items[0].external_urls.get("spotify") {
|
|
|
|
match &tracks.items.first().map(|x| x.external_urls.get("spotify")) {
|
|
|
|
Some(url) => Reply::Text(url.to_owned().to_owned()),
|
|
|
|
Some(Some(url)) => Reply::Text(url.to_owned().to_owned()),
|
|
|
|
None => Reply::Text("Unable to get spotify url".to_owned()),
|
|
|
|
_ => Reply::Text("Unable to get spotify url".to_owned()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -219,11 +210,8 @@ async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option<String>) -> Re
|
|
|
|
None => return Reply::Text("Error: getting image uri failed".to_owned()),
|
|
|
|
None => return Reply::Text("Error: getting image uri failed".to_owned()),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let image = match get_image(ctx, image_uri.as_str()).await {
|
|
|
|
|
|
|
|
Ok(i) => i,
|
|
|
|
|
|
|
|
Err(e) => return Reply::Text(format!("{e}")),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut base_color = PixelWand::new();
|
|
|
|
let mut base_color = PixelWand::new();
|
|
|
|
|
|
|
|
let mut accent_color = PixelWand::new();
|
|
|
|
let mut white = PixelWand::new();
|
|
|
|
let mut white = PixelWand::new();
|
|
|
|
let main_wand = MagickWand::new();
|
|
|
|
let main_wand = MagickWand::new();
|
|
|
|
let art_wand = MagickWand::new();
|
|
|
|
let art_wand = MagickWand::new();
|
|
|
|
@ -231,10 +219,10 @@ async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option<String>) -> Re
|
|
|
|
let mask_wand = MagickWand::new();
|
|
|
|
let mask_wand = MagickWand::new();
|
|
|
|
let text_image_wand = MagickWand::new();
|
|
|
|
let text_image_wand = MagickWand::new();
|
|
|
|
|
|
|
|
|
|
|
|
handle_magick_result!(
|
|
|
|
let image = match get_image(ctx, image_uri.as_str()).await {
|
|
|
|
base_color.set_color("#7f7f7f"),
|
|
|
|
Ok(i) => i,
|
|
|
|
"Failed to set init base color"
|
|
|
|
Err(e) => return Reply::Text(format!("{e}")),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
handle_magick_result!(
|
|
|
|
handle_magick_result!(
|
|
|
|
art_wand.read_image_blob(image),
|
|
|
|
art_wand.read_image_blob(image),
|
|
|
|
"Failed to read image from uri"
|
|
|
|
"Failed to read image from uri"
|
|
|
|
@ -244,6 +232,26 @@ async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option<String>) -> Re
|
|
|
|
"Failed to set art colorspace"
|
|
|
|
"Failed to set art colorspace"
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
handle_magick_result!(
|
|
|
|
|
|
|
|
base_color.set_color("#7f7f7f"),
|
|
|
|
|
|
|
|
"Failed to set init base color"
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let color_cache_entry = {
|
|
|
|
|
|
|
|
let data = ctx.data.write().await;
|
|
|
|
|
|
|
|
let color_cache = data.get::<ColorsHaver>().expect("Failed to have colors option");
|
|
|
|
|
|
|
|
if color_cache.max > 0 {
|
|
|
|
|
|
|
|
let color_cache_map = color_cache.map.lock().await;
|
|
|
|
|
|
|
|
color_cache_map.get(image_uri.as_str()).map(|s| s.to_owned())
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
None
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let (base_lab, accent_lab) = match color_cache_entry {
|
|
|
|
|
|
|
|
Some(value) => value.to_owned(),
|
|
|
|
|
|
|
|
None => {
|
|
|
|
let (art_width, art_height) = (art_wand.get_image_width(), art_wand.get_image_height());
|
|
|
|
let (art_width, art_height) = (art_wand.get_image_width(), art_wand.get_image_height());
|
|
|
|
handle_magick_result!(
|
|
|
|
handle_magick_result!(
|
|
|
|
art_wand_cluster.new_image(art_width, art_height, &base_color),
|
|
|
|
art_wand_cluster.new_image(art_width, art_height, &base_color),
|
|
|
|
@ -278,20 +286,27 @@ async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option<String>) -> Re
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
let mut accent_color = PixelWand::new();
|
|
|
|
let mut accent_lab = "cielab(0.0,0.0,0.0)".to_owned();
|
|
|
|
handle_magick_result!(
|
|
|
|
|
|
|
|
accent_color.set_color("cielab(0.0,0.0,0.0)"),
|
|
|
|
|
|
|
|
"Failed to init accent color"
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Some(color) = other_colors.first() {
|
|
|
|
if let Some(color) = other_colors.first() {
|
|
|
|
let col = format!("cielab{:?}", color.into_components());
|
|
|
|
accent_lab = format!("cielab{:?}", color.into_components());
|
|
|
|
handle_magick_result!(
|
|
|
|
|
|
|
|
accent_color.set_color(col.as_str()),
|
|
|
|
|
|
|
|
"Failed to set accent color"
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let base_lab = handle_magick_result!(colors[0].get_color_as_string(), "Failed to serialize base color");
|
|
|
|
|
|
|
|
let data = ctx.data.write().await;
|
|
|
|
|
|
|
|
let color_cache = data.get::<ColorsHaver>().expect("Failed to have colors option");
|
|
|
|
|
|
|
|
let mut color_cache_map = color_cache.map.lock().await;
|
|
|
|
|
|
|
|
if color_cache_map.keys().len() > color_cache.max {
|
|
|
|
|
|
|
|
let key: String = color_cache_map.iter().next().unwrap().0.to_owned();
|
|
|
|
|
|
|
|
color_cache_map.remove(&key);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
color_cache_map.insert(image_uri, (base_lab.clone(), accent_lab.clone()));
|
|
|
|
|
|
|
|
(base_lab, accent_lab)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let use_dark_color = match validate_color(&colors[0]) {
|
|
|
|
handle_magick_result!(base_color.set_color(base_lab.as_str()), "Failed to deserialize base color");
|
|
|
|
|
|
|
|
handle_magick_result!(accent_color.set_color(accent_lab.as_str()), "Failed to deserialize accent color");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let use_dark_color = match validate_color(&base_color) {
|
|
|
|
Some(color) => {
|
|
|
|
Some(color) => {
|
|
|
|
let black_delta_e = Lab::<D65>::new(0.0, 0.0, 0.0).improved_difference(color);
|
|
|
|
let black_delta_e = Lab::<D65>::new(0.0, 0.0, 0.0).improved_difference(color);
|
|
|
|
let white_delta_e = Lab::<D65>::new(100.0, 0.0, 0.0).improved_difference(color);
|
|
|
|
let white_delta_e = Lab::<D65>::new(100.0, 0.0, 0.0).improved_difference(color);
|
|
|
|
@ -372,7 +387,7 @@ async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option<String>) -> Re
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
handle_magick_result!(
|
|
|
|
handle_magick_result!(
|
|
|
|
main_wand.new_image(image_width as usize, image_height as usize, &colors[0]),
|
|
|
|
main_wand.new_image(image_width as usize, image_height as usize, &base_color),
|
|
|
|
"Failed to create wand"
|
|
|
|
"Failed to create wand"
|
|
|
|
);
|
|
|
|
);
|
|
|
|
handle_magick_result!(
|
|
|
|
handle_magick_result!(
|
|
|
|
@ -383,10 +398,10 @@ async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option<String>) -> Re
|
|
|
|
art_wand.adaptive_resize_image(124, 124),
|
|
|
|
art_wand.adaptive_resize_image(124, 124),
|
|
|
|
"Failed to resize art"
|
|
|
|
"Failed to resize art"
|
|
|
|
);
|
|
|
|
);
|
|
|
|
handle_magick_result!(
|
|
|
|
/*handle_magick_result!(
|
|
|
|
art_wand_cluster.adaptive_resize_image(124, 124),
|
|
|
|
art_wand_cluster.adaptive_resize_image(124, 124),
|
|
|
|
"Failed to resize art_cluster"
|
|
|
|
"Failed to resize art_cluster"
|
|
|
|
);
|
|
|
|
);*/
|
|
|
|
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 art image"
|
|
|
|
"Failed to combine art image"
|
|
|
|
@ -451,12 +466,6 @@ async fn set(ctx: &Context, arg: &str, id: UserId) -> Reply {
|
|
|
|
Reply::Text(format!("set user {arg}"))
|
|
|
|
Reply::Text(format!("set user {arg}"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//macro_rules! cmd {
|
|
|
|
|
|
|
|
// (c:lit, abbr:lit, a:ident) => {
|
|
|
|
|
|
|
|
// (".k", $c, $a) | (".k", $a, $c) | (concat!(".k", $abbr), $ident, "")
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
#[async_trait]
|
|
|
|
impl EventHandler for Handler {
|
|
|
|
impl EventHandler for Handler {
|
|
|
|
async fn message(&self, ctx: Context, msg: Message) {
|
|
|
|
async fn message(&self, ctx: Context, msg: Message) {
|
|
|
|
@ -535,6 +544,15 @@ impl TypeMapKey for SpotifyHaver {
|
|
|
|
type Value = Option<Arc<Mutex<ClientCredsSpotify>>>;
|
|
|
|
type Value = Option<Arc<Mutex<ClientCredsSpotify>>>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct ColorCache {
|
|
|
|
|
|
|
|
map: Arc<Mutex<HashMap<String, (String, String)>>>,
|
|
|
|
|
|
|
|
max: usize,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ColorsHaver;
|
|
|
|
|
|
|
|
impl TypeMapKey for ColorsHaver {
|
|
|
|
|
|
|
|
type Value = ColorCache;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct DBResponse {
|
|
|
|
struct DBResponse {
|
|
|
|
lastfm_username: String,
|
|
|
|
lastfm_username: String,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -568,7 +586,7 @@ async fn get_lastfm_username(ctx: &Context, id: UserId) -> Option<String> {
|
|
|
|
|
|
|
|
|
|
|
|
async fn set_lastfm_username(ctx: &Context, id: UserId, user: String) {
|
|
|
|
async fn set_lastfm_username(ctx: &Context, id: UserId, user: String) {
|
|
|
|
log!("set db user {} {}", id, user);
|
|
|
|
log!("set db user {} {}", id, user);
|
|
|
|
let data = ctx.data.write().await;
|
|
|
|
let data = ctx.data.read().await;
|
|
|
|
let pool = data
|
|
|
|
let pool = data
|
|
|
|
.get::<PoolHaver>()
|
|
|
|
.get::<PoolHaver>()
|
|
|
|
.expect("Failed to get pool container");
|
|
|
|
.expect("Failed to get pool container");
|
|
|
|
@ -588,7 +606,7 @@ 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> {
|
|
|
|
log!("get {}", url);
|
|
|
|
log!("get {}", url);
|
|
|
|
let data = ctx.data.write().await;
|
|
|
|
let data = ctx.data.read().await;
|
|
|
|
let http = data.get::<HttpHaver>().expect("Failed to get http client");
|
|
|
|
let http = data.get::<HttpHaver>().expect("Failed to get http client");
|
|
|
|
match http.get(url).send().await {
|
|
|
|
match http.get(url).send().await {
|
|
|
|
Ok(resp) => {
|
|
|
|
Ok(resp) => {
|
|
|
|
@ -668,6 +686,11 @@ async fn main() {
|
|
|
|
let _ = &spotify_client.request_token().await.unwrap();
|
|
|
|
let _ = &spotify_client.request_token().await.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let colors_cache: ColorCache = ColorCache {
|
|
|
|
|
|
|
|
map: Arc::new(Mutex::new(HashMap::new())),
|
|
|
|
|
|
|
|
max: env::var("MAX_CACHE_SIZE").unwrap_or("".to_owned()).parse::<usize>().unwrap_or(1000000),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let mut discord_client = Client::builder(&token, intents)
|
|
|
|
let mut discord_client = Client::builder(&token, intents)
|
|
|
|
.event_handler(Handler)
|
|
|
|
.event_handler(Handler)
|
|
|
|
.await
|
|
|
|
.await
|
|
|
|
@ -679,6 +702,7 @@ async fn main() {
|
|
|
|
data.insert::<HttpHaver>(http);
|
|
|
|
data.insert::<HttpHaver>(http);
|
|
|
|
data.insert::<FontsHaver>((regular_fonts, bold_fonts));
|
|
|
|
data.insert::<FontsHaver>((regular_fonts, bold_fonts));
|
|
|
|
data.insert::<SpotifyHaver>(spotify.map(Mutex::new).map(Arc::new));
|
|
|
|
data.insert::<SpotifyHaver>(spotify.map(Mutex::new).map(Arc::new));
|
|
|
|
|
|
|
|
data.insert::<ColorsHaver>(colors_cache);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if let Err(why) = discord_client.start().await {
|
|
|
|
if let Err(why) = discord_client.start().await {
|
|
|
|
|