Compare commits
3 Commits
d3ad04bc33
...
meta
| Author | SHA1 | Date | |
|---|---|---|---|
| 9591dcdd3e | |||
| d0f0d07b51 | |||
| 6ee36cb65a |
@ -6,5 +6,5 @@ DATABASE_URL=sqlite:watcat.db
|
|||||||
RSPOTIFY_CLIENT_ID=
|
RSPOTIFY_CLIENT_ID=
|
||||||
RSPOTIFY_CLIENT_SECRET=
|
RSPOTIFY_CLIENT_SECRET=
|
||||||
|
|
||||||
# optional, to set cache size, set to 0 to disable
|
# optional, to set color cache size. set to 0 to disable
|
||||||
MAX_CACHE_SIZE=
|
MAX_CACHE_SIZE=
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@
|
|||||||
/.env
|
/.env
|
||||||
/watcat.db*
|
/watcat.db*
|
||||||
/http-cacache
|
/http-cacache
|
||||||
|
/overrides.txt
|
||||||
|
|||||||
36
Cargo.lock
generated
36
Cargo.lock
generated
@ -1100,6 +1100,12 @@ dependencies = [
|
|||||||
"foldhash",
|
"foldhash",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hashbrown"
|
||||||
|
version = "0.16.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5419bdc4f6a9207fbeba6d11b604d481addf78ecd10c11ad51e76c2f6482748d"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hashlink"
|
name = "hashlink"
|
||||||
version = "0.10.0"
|
version = "0.10.0"
|
||||||
@ -1570,12 +1576,12 @@ checksum = "d0263a3d970d5c054ed9312c0057b4f3bde9c0b33836d3637361d4a9e6e7a408"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "indexmap"
|
name = "indexmap"
|
||||||
version = "2.9.0"
|
version = "2.12.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "cea70ddb795996207ad57735b50c5982d8844f38ba9ee5f1aedcfb708a2aa11e"
|
checksum = "6717a8d2a5a929a1a2eb43a12812498ed141a0bcfb7e8f7844fbdbe4303bba9f"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"equivalent",
|
"equivalent",
|
||||||
"hashbrown 0.15.3",
|
"hashbrown 0.16.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -2958,9 +2964,19 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.219"
|
version = "1.0.228"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6"
|
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
|
||||||
|
dependencies = [
|
||||||
|
"serde_core",
|
||||||
|
"serde_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_core"
|
||||||
|
version = "1.0.228"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"serde_derive",
|
"serde_derive",
|
||||||
]
|
]
|
||||||
@ -2976,9 +2992,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_derive"
|
name = "serde_derive"
|
||||||
version = "1.0.219"
|
version = "1.0.228"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00"
|
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@ -4270,7 +4286,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "watcat"
|
name = "watcat"
|
||||||
version = "0.6.0"
|
version = "0.6.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"arabic_reshaper",
|
"arabic_reshaper",
|
||||||
"dotenvy",
|
"dotenvy",
|
||||||
@ -4695,9 +4711,9 @@ checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winnow"
|
name = "winnow"
|
||||||
version = "0.7.10"
|
version = "0.7.13"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c06928c8748d81b05c9be96aad92e1b6ff01833332f281e8cfca3be4b35fc9ec"
|
checksum = "21a0236b59786fed61e2a80582dd500fe61f18b5dca67a4a067d0bc9039339cf"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"memchr",
|
"memchr",
|
||||||
]
|
]
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "watcat"
|
name = "watcat"
|
||||||
version = "0.6.0"
|
version = "0.6.2"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@ -31,9 +31,11 @@ TODO
|
|||||||
+ ~~add art and lastfm url fetching~~
|
+ ~~add art and lastfm url fetching~~
|
||||||
+ ~~add help command~~
|
+ ~~add help command~~
|
||||||
+ ~~refactor to combine repeated parts of art, url, and fmi.~~
|
+ ~~refactor to combine repeated parts of art, url, and fmi.~~
|
||||||
+ use macro for commands
|
+ ~~use macro for commands~~
|
||||||
+ use Option instead of empty string sentinel value
|
+ ~~use Option instead of empty string sentinel value~~
|
||||||
+ ~~add cache for album art colors~~
|
+ ~~add cache for album art colors~~
|
||||||
+ ~~add spotify link fetching~~
|
+ ~~add spotify link fetching~~
|
||||||
|
+ ~~add arbitrary spotify searching~~
|
||||||
|
+ ~~use static seed for kmeans~~
|
||||||
+ make various parameters user-configurable
|
+ make various parameters user-configurable
|
||||||
+ use tiny-skia instead of magick-rust?
|
+ use tiny-skia instead of magick-rust?
|
||||||
|
|||||||
233
src/main.rs
233
src/main.rs
@ -14,6 +14,7 @@ extern crate magick_rust;
|
|||||||
use magick_rust::{
|
use magick_rust::{
|
||||||
ColorspaceType, CompositeOperator, FilterType, MagickWand, PixelWand, magick_wand_genesis,
|
ColorspaceType, CompositeOperator, FilterType, MagickWand, PixelWand, magick_wand_genesis,
|
||||||
};
|
};
|
||||||
|
use magick_rust::bindings::MagickSetSeed;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
@ -33,6 +34,9 @@ use rspotify::{ClientCredsSpotify, Credentials, clients::BaseClient};
|
|||||||
use imagetext::fontdb::FontDB;
|
use imagetext::fontdb::FontDB;
|
||||||
use imagetext::superfont::SuperFont;
|
use imagetext::superfont::SuperFont;
|
||||||
|
|
||||||
|
use std::time::SystemTime;
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
mod text;
|
mod text;
|
||||||
|
|
||||||
static START: Once = Once::new();
|
static START: Once = Once::new();
|
||||||
@ -124,12 +128,12 @@ fn validate_color(col: &PixelWand) -> Option<Lab> {
|
|||||||
|
|
||||||
async fn get_track(
|
async fn get_track(
|
||||||
ctx: &Context,
|
ctx: &Context,
|
||||||
arg: &str,
|
arg: Option<&str>,
|
||||||
id: UserId,
|
id: UserId,
|
||||||
) -> Result<lastfm::track::NowPlayingTrack, Reply> {
|
) -> Result<lastfm::track::NowPlayingTrack, Reply> {
|
||||||
let lastfm_user = match arg {
|
let lastfm_user = match arg {
|
||||||
"" => get_lastfm_username(ctx, id).await,
|
None => get_lastfm_username(ctx, id).await,
|
||||||
_ => Some(arg.to_owned()),
|
Some(user) => Some(user.to_owned()),
|
||||||
};
|
};
|
||||||
let lastfm_client = match lastfm_user {
|
let lastfm_client = match lastfm_user {
|
||||||
Some(s) => lastfm::Client::<String, String>::from_env(s),
|
Some(s) => lastfm::Client::<String, String>::from_env(s),
|
||||||
@ -150,7 +154,7 @@ async fn get_track(
|
|||||||
Ok(track)
|
Ok(track)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn art(ctx: &Context, arg: &str, id: UserId) -> Reply {
|
async fn art(ctx: &Context, arg: Option<&str>, id: UserId) -> Reply {
|
||||||
let track = match get_track(ctx, arg, id).await {
|
let track = match get_track(ctx, arg, id).await {
|
||||||
Ok(track) => track,
|
Ok(track) => track,
|
||||||
Err(e) => return e,
|
Err(e) => return e,
|
||||||
@ -162,7 +166,7 @@ async fn art(ctx: &Context, arg: &str, id: UserId) -> Reply {
|
|||||||
Reply::Text(track_art.to_owned())
|
Reply::Text(track_art.to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn url(ctx: &Context, arg: &str, id: UserId) -> Reply {
|
async fn url(ctx: &Context, arg: Option<&str>, id: UserId) -> Reply {
|
||||||
let track = match get_track(ctx, arg, id).await {
|
let track = match get_track(ctx, arg, id).await {
|
||||||
Ok(track) => track,
|
Ok(track) => track,
|
||||||
Err(e) => return e,
|
Err(e) => return e,
|
||||||
@ -170,7 +174,7 @@ async fn url(ctx: &Context, arg: &str, id: UserId) -> Reply {
|
|||||||
Reply::Text(track.url)
|
Reply::Text(track.url)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn spot(ctx: &Context, arg: &str, id: UserId) -> Reply {
|
async fn spot(ctx: &Context, arg: Option<&str>, id: UserId) -> Reply {
|
||||||
let track = match get_track(ctx, arg, id).await {
|
let track = match get_track(ctx, arg, id).await {
|
||||||
Ok(track) => track,
|
Ok(track) => track,
|
||||||
Err(e) => return e,
|
Err(e) => return e,
|
||||||
@ -178,7 +182,7 @@ async fn spot(ctx: &Context, arg: &str, id: UserId) -> Reply {
|
|||||||
let mut data = ctx.data.write().await;
|
let mut data = ctx.data.write().await;
|
||||||
let spotify_option = data.get_mut::<SpotifyHaver>().expect("Failed to have spotify option");
|
let spotify_option = data.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 commands: 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_client = spotify_arc_mutex.lock().await;
|
let spotify_client = spotify_arc_mutex.lock().await;
|
||||||
@ -195,7 +199,52 @@ async fn spot(ctx: &Context, arg: &str, id: UserId) -> Reply {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option<String>) -> Reply {
|
async fn get_overrides_inner(last_modified: Option<SystemTime>) -> Option<HashMap<String, String>> {
|
||||||
|
let mut file = match std::fs::File::open("overrides.txt") {
|
||||||
|
Ok(a) => a,
|
||||||
|
Err(_) => return None,
|
||||||
|
};
|
||||||
|
let file_stats = match file.metadata() {
|
||||||
|
Ok(a) => a,
|
||||||
|
Err(_) => return None,
|
||||||
|
};
|
||||||
|
let file_modified_time = match file_stats.modified() {
|
||||||
|
Ok(a) => a,
|
||||||
|
Err(_) => return None,
|
||||||
|
};
|
||||||
|
let reload = match last_modified {
|
||||||
|
None => true,
|
||||||
|
Some(last_modified) => match file_modified_time.duration_since(last_modified) {
|
||||||
|
Err(_) => true,
|
||||||
|
Ok(a) => a != std::time::Duration::default()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if !reload { return None; }
|
||||||
|
let mut contents = "".to_owned();
|
||||||
|
match file.read_to_string(&mut contents) {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(_) => return None,
|
||||||
|
};
|
||||||
|
Some(contents.as_str().lines().map(|x| {
|
||||||
|
let mut split = x.split("@");
|
||||||
|
let a: &str = split.next().unwrap_or("").trim();
|
||||||
|
let b: &str = split.next().unwrap_or("").trim();
|
||||||
|
(a.to_owned(), b.to_owned())
|
||||||
|
}).collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_overrides(ctx: &Context) -> HashMap<String, String> {
|
||||||
|
let data = ctx.data.write().await;
|
||||||
|
let overrides = data.get::<OverridesHaver>().expect("Failed to have overrides");
|
||||||
|
let mut map = overrides.map.lock().await;
|
||||||
|
let new_map = get_overrides_inner(overrides.last_modified).await;
|
||||||
|
if let Some(new_map) = new_map {
|
||||||
|
*map = new_map;
|
||||||
|
}
|
||||||
|
map.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn fmi(ctx: &Context, arg: Option<&str>, id: UserId, avatar: Option<String>) -> Reply {
|
||||||
let track = match get_track(ctx, arg, id).await {
|
let track = match get_track(ctx, arg, id).await {
|
||||||
Ok(track) => track,
|
Ok(track) => track,
|
||||||
Err(e) => return e,
|
Err(e) => return e,
|
||||||
@ -205,11 +254,20 @@ async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option<String>) -> Re
|
|||||||
artist: track.artist.name,
|
artist: track.artist.name,
|
||||||
album: track.album,
|
album: track.album,
|
||||||
};
|
};
|
||||||
let image_uri = match track.image.extralarge {
|
let original_image_uri = match track.image.extralarge {
|
||||||
Some(iu) => iu,
|
Some(iu) => iu,
|
||||||
None => return Reply::Text("Error: getting image uri failed".to_owned()),
|
None => return Reply::Text("Error: getting image uri failed".to_owned()),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let overrides = get_overrides(ctx).await;
|
||||||
|
let image_uri: String = match overrides.get(&format!("{} | {}", track_info.artist, track_info.album)) {
|
||||||
|
Some(s) => s,
|
||||||
|
None => match overrides.get(&track_info.album) {
|
||||||
|
Some(s) => s,
|
||||||
|
None => &original_image_uri,
|
||||||
|
},
|
||||||
|
}.to_owned();
|
||||||
|
|
||||||
let mut base_color = PixelWand::new();
|
let mut base_color = PixelWand::new();
|
||||||
let mut accent_color = PixelWand::new();
|
let mut accent_color = PixelWand::new();
|
||||||
let mut white = PixelWand::new();
|
let mut white = PixelWand::new();
|
||||||
@ -266,6 +324,7 @@ async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option<String>) -> Re
|
|||||||
art_wand_cluster.set_image_colorspace(ColorspaceType::Lab),
|
art_wand_cluster.set_image_colorspace(ColorspaceType::Lab),
|
||||||
"Failed to set cluster colorspace"
|
"Failed to set cluster colorspace"
|
||||||
);
|
);
|
||||||
|
unsafe { MagickSetSeed(0x5F3759DF); }
|
||||||
handle_magick_result!(
|
handle_magick_result!(
|
||||||
art_wand_cluster.kmeans(10, 200, 0.001),
|
art_wand_cluster.kmeans(10, 200, 0.001),
|
||||||
"Failed to run kmeans"
|
"Failed to run kmeans"
|
||||||
@ -395,7 +454,7 @@ async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option<String>) -> Re
|
|||||||
"Failed to set main colorspace"
|
"Failed to set main colorspace"
|
||||||
);
|
);
|
||||||
handle_magick_result!(
|
handle_magick_result!(
|
||||||
art_wand.adaptive_resize_image(124, 124),
|
art_wand.resize_image(124, 124, FilterType::RobidouxSharp),
|
||||||
"Failed to resize art"
|
"Failed to resize art"
|
||||||
);
|
);
|
||||||
/*handle_magick_result!(
|
/*handle_magick_result!(
|
||||||
@ -434,29 +493,44 @@ async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option<String>) -> Re
|
|||||||
Reply::Image(main_wand.write_image_blob("png").unwrap())
|
Reply::Image(main_wand.write_image_blob("png").unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn help(arg: &str) -> Reply {
|
async fn text(ctx: &Context, arg: Option<&str>, id: UserId) -> Reply {
|
||||||
|
let track = match get_track(ctx, arg, id).await {
|
||||||
|
Ok(track) => track,
|
||||||
|
Err(e) => return e,
|
||||||
|
};
|
||||||
|
Reply::Text(format!(
|
||||||
|
"{}\n{}\n{}",
|
||||||
|
track.name,
|
||||||
|
track.artist.name,
|
||||||
|
track.album,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn help(arg: Option<&str>) -> Reply {
|
||||||
Reply::Text(format!(
|
Reply::Text(format!(
|
||||||
"```{}```",
|
"```{}```",
|
||||||
match arg {
|
match arg {
|
||||||
"" =>
|
None =>
|
||||||
"usage: .k <img|set|art|url|spotify|help> [lastfm user]\n .k img [lastfm user]: get now playing\n .k set <lastfm user>: sets your username for the future\n .k art [lastfm user]: get now playing album art\n .k url [lastfm user]: get now playing lastfm url\n .k spot [lastfm user]: get now playing spotify link\n .k help [command]: displays a help message"
|
"usage: .k <img|set|art|url|spotify|help> [lastfm user]\n .k img [lastfm user]: get now playing\n .k set <lastfm user>: sets your username for the future\n .k art [lastfm user]: get now playing album art\n .k url [lastfm user]: get now playing lastfm url\n .k spot [lastfm user]: get now playing spotify link\n .k search <search query>: searches spotify\n .k help [command]: displays a help message"
|
||||||
.to_owned(),
|
.to_owned(),
|
||||||
"img" | "fmi" | "fm" | "get" =>
|
Some("img" | "fmi" | "fm" | "get") =>
|
||||||
"usage: .k img [lastfm user]\nreturns a pretty-printed image of the user's now playing track on last.fm, using the album art's colors to theme the image. see also: .k set\naliases: .kfmi, .k <fmi|get|img>, .kf, .ki, or even just .k"
|
"usage: .k img [lastfm user]\nreturns a pretty-printed image of the user's now playing track on last.fm, using the album art's colors to theme the image. see also: .k set\naliases: .kfmi, .k <fmi|get|img>, .kf, .ki, or even just .k"
|
||||||
.to_owned(),
|
.to_owned(),
|
||||||
"set" =>
|
Some("set") =>
|
||||||
"usage: .k set <lastfm user>\nties a lastfm username to your account. use this to not require your username for .k fmi every time, for example\naliases: .kset"
|
"usage: .k set <lastfm user>\nties a lastfm username to your account. use this to not require your username for .k fmi every time, for example\naliases: .kset"
|
||||||
.to_owned(),
|
.to_owned(),
|
||||||
"art" =>
|
Some("art") =>
|
||||||
"usage: .k art [lastfm user]\nfetches user's now playing track's album art\naliases: .k art, .kart, .ka"
|
"usage: .k art [lastfm user]\nfetches user's now playing track's album art\naliases: .k art, .kart, .ka"
|
||||||
.to_owned(),
|
.to_owned(),
|
||||||
"url" | "link" =>
|
Some("url" | "link") =>
|
||||||
"usage: .k url [lastfm user]\nfetches a lastfm link to user's now playing track\naliases: .k url, .k link, .kl, .ku, .kurl"
|
"usage: .k url [lastfm user]\nfetches a lastfm link to user's now playing track\naliases: .k url, .k link, .kl, .ku, .kurl"
|
||||||
.to_owned(),
|
.to_owned(),
|
||||||
"spot" | "spotify" =>
|
Some("spot" | "spotify") =>
|
||||||
"usage: .k spotify [lastfm user]\nsearches spotify for the user's now playing track and returns a link to that track\naliases: .k <spot|spotify>, .ks, .kspot"
|
"usage: .k spotify [lastfm user]\nsearches spotify for the user's now playing track and returns a link to that track\naliases: .k <spot|spotify>, .kx, .kspot"
|
||||||
.to_owned(),
|
.to_owned(),
|
||||||
_ => format!("unknown command: {arg}"),
|
Some("search") =>
|
||||||
|
"usage: .k search <search query>\nsearches spotify with an arbitrary query and returns the first result\naliases: .k search, .ks".to_owned(),
|
||||||
|
Some(cmd) => format!("unknown command: {cmd}"),
|
||||||
}
|
}
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
@ -466,42 +540,88 @@ async fn set(ctx: &Context, arg: &str, id: UserId) -> Reply {
|
|||||||
Reply::Text(format!("set user {arg}"))
|
Reply::Text(format!("set user {arg}"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn search(ctx: &Context, arg: &str) -> Reply {
|
||||||
|
let mut data = ctx.data.write().await;
|
||||||
|
let spotify_option = data.get_mut::<SpotifyHaver>().expect("Failed to have spotify option");
|
||||||
|
if spotify_option.is_none() {
|
||||||
|
return Reply::Text("Unable to use Spotify commands: Contact bot Administrator.".to_owned())
|
||||||
|
}
|
||||||
|
let spotify_arc_mutex = spotify_option.as_mut().unwrap();
|
||||||
|
let spotify_client = spotify_arc_mutex.lock().await;
|
||||||
|
spotify_client.request_token().await.unwrap();
|
||||||
|
let search = spotify_client.search(arg, SearchType::Track, None, None, None, None).await;
|
||||||
|
let tracks = match search {
|
||||||
|
Ok(SearchResult::Tracks(track_page)) => track_page,
|
||||||
|
Err(e) => return Reply::Text(format!("Failed to get track {e}")),
|
||||||
|
_ => return Reply::Text("Spotify search failed".to_owned()),
|
||||||
|
};
|
||||||
|
match &tracks.items.first().map(|x| x.external_urls.get("spotify")) {
|
||||||
|
Some(Some(url)) => Reply::Text(url.to_owned().to_owned()),
|
||||||
|
_ => Reply::Text("Unable to get spotify url".to_owned()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! cmd_pattern {
|
||||||
|
($long_cmd:pat, $short_pattern:pat, $arg_name:pat) => {
|
||||||
|
(Some(".k"), Some($long_cmd), $arg_name) | (Some(".k"), $arg_name, Some($long_cmd)) | (Some($short_pattern), $arg_name, None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[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) {
|
||||||
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();
|
||||||
let arg1 = cmd_iter.next().unwrap_or("");
|
let arg1 = cmd_iter.next();
|
||||||
let arg2 = cmd_iter.next().unwrap_or("");
|
let arg2 = cmd_iter.next();
|
||||||
let resp = match (cmd, arg1, arg2) {
|
let resp = match (cmd, arg1, arg2) {
|
||||||
(".set", arg, "") | (".k", "set", arg) | (".kset", arg, "") => {
|
(Some(".k"), Some("help" | "h" | "?"), arg) // skip the ".k <command> help" syntax
|
||||||
log!("{} received", msg.content);
|
| (Some(".kh") | Some(".k?"), arg, None) => {
|
||||||
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)
|
|
||||||
}
|
|
||||||
(".k", "spot" | "spotify", arg)
|
|
||||||
| (".k", arg, "spot" | "spotify")
|
|
||||||
| (".ks" | ".kspot", arg, "") => {
|
|
||||||
log!("{} received", msg.content);
|
|
||||||
Some(spot(&ctx, arg, msg.author.id).await)
|
|
||||||
}
|
|
||||||
(".k", "help" | "h" | "?", arg)
|
|
||||||
| (".k", arg, "help" | "h" | "?")
|
|
||||||
| (".kh" | ".k?", arg, "") => {
|
|
||||||
log!("{} received", msg.content);
|
log!("{} received", msg.content);
|
||||||
Some(help(arg))
|
Some(help(arg))
|
||||||
}
|
}
|
||||||
(".fmi" | ".k" | ".kf" | ".ki" | ".kfmi", arg, "")
|
cmd_pattern!("set", ".kset" | ".set", Some(arg)) =>
|
||||||
| (".k", "fm" | "fmi" | "get" | "img", arg) => {
|
/*(".set", arg, "") | (".k", "set", arg) | (".kset", arg, "") =>*/ {
|
||||||
|
log!("{} received", msg.content);
|
||||||
|
Some(set(&ctx, arg, msg.author.id).await)
|
||||||
|
},
|
||||||
|
cmd_pattern!("art", ".ka" | ".kart", arg) =>
|
||||||
|
/*(".k", "art", arg) | (".k", arg, "art") | (".ka" | ".kart", arg, "") =>*/ {
|
||||||
|
log!("{} received", msg.content);
|
||||||
|
Some(art(&ctx, arg, msg.author.id).await)
|
||||||
|
},
|
||||||
|
cmd_pattern!("url" | "uri" | "link", ".ku" | ".kl", arg) =>
|
||||||
|
/*(".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)
|
||||||
|
},
|
||||||
|
cmd_pattern!("text", ".kt", arg) =>
|
||||||
|
/*(".k", "url" | "uri" | "link", arg)
|
||||||
|
| (".k", arg, "url" | "uri" | "link")
|
||||||
|
| (".ku" | ".kl" | ".kurl", arg, "") =>*/ {
|
||||||
|
log!("{} received", msg.content);
|
||||||
|
Some(text(&ctx, arg, msg.author.id).await)
|
||||||
|
},
|
||||||
|
cmd_pattern!("spot" | "spotify", ".kx", arg) =>
|
||||||
|
/*(".k", "spot" | "spotify", arg)
|
||||||
|
| (".k", arg, "spot" | "spotify")
|
||||||
|
| (".ks" | ".kspot", arg, "") =>*/ {
|
||||||
|
log!("{} received", msg.content);
|
||||||
|
Some(spot(&ctx, arg, msg.author.id).await)
|
||||||
|
},
|
||||||
|
(Some(".ks"), Some(_), _) => {
|
||||||
|
log!("{} received", msg.content);
|
||||||
|
Some(search(&ctx, &msg.content[4..]).await)
|
||||||
|
},
|
||||||
|
(Some(".k"), Some("search"), Some(_)) => {
|
||||||
|
log!("{} received", msg.content);
|
||||||
|
Some(search(&ctx, &msg.content[10..]).await)
|
||||||
|
},
|
||||||
|
cmd_pattern!("fm" | "fmi" | "get" | "img", ".fmi" | ".k" | ".kf" | ".ki" | ".kfmi", arg) =>
|
||||||
|
/*(".fmi" | ".k" | ".kf" | ".ki" | ".kfmi", arg, "")
|
||||||
|
| (".k", "fm" | "fmi" | "get" | "img", arg) =>*/ {
|
||||||
log!("{} received", msg.content);
|
log!("{} received", msg.content);
|
||||||
Some(fmi(&ctx, arg, msg.author.id, msg.author.avatar_url()).await)
|
Some(fmi(&ctx, arg, msg.author.id, msg.author.avatar_url()).await)
|
||||||
}
|
}
|
||||||
@ -553,6 +673,15 @@ impl TypeMapKey for ColorsHaver {
|
|||||||
type Value = ColorCache;
|
type Value = ColorCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Overrides {
|
||||||
|
map: Arc<Mutex<HashMap<String, String>>>,
|
||||||
|
last_modified: Option<SystemTime>,
|
||||||
|
}
|
||||||
|
struct OverridesHaver;
|
||||||
|
impl TypeMapKey for OverridesHaver {
|
||||||
|
type Value = Overrides;
|
||||||
|
}
|
||||||
|
|
||||||
struct DBResponse {
|
struct DBResponse {
|
||||||
lastfm_username: String,
|
lastfm_username: String,
|
||||||
}
|
}
|
||||||
@ -686,11 +815,16 @@ async fn main() {
|
|||||||
let _ = &spotify_client.request_token().await.unwrap();
|
let _ = &spotify_client.request_token().await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
let colors_cache: ColorCache = ColorCache {
|
let colors_cache = ColorCache {
|
||||||
map: Arc::new(Mutex::new(HashMap::new())),
|
map: Arc::new(Mutex::new(HashMap::new())),
|
||||||
max: env::var("MAX_CACHE_SIZE").unwrap_or("".to_owned()).parse::<usize>().unwrap_or(1000000),
|
max: env::var("MAX_CACHE_SIZE").unwrap_or("".to_owned()).parse::<usize>().unwrap_or(1000000),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let overrides = Overrides {
|
||||||
|
map: Arc::new(Mutex::new(HashMap::new())),
|
||||||
|
last_modified: None,
|
||||||
|
};
|
||||||
|
|
||||||
let mut discord_client = Client::builder(&token, intents)
|
let mut discord_client = Client::builder(&token, intents)
|
||||||
.event_handler(Handler)
|
.event_handler(Handler)
|
||||||
.await
|
.await
|
||||||
@ -703,6 +837,7 @@ async fn main() {
|
|||||||
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);
|
data.insert::<ColorsHaver>(colors_cache);
|
||||||
|
data.insert::<OverridesHaver>(overrides);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Err(why) = discord_client.start().await {
|
if let Err(why) = discord_client.start().await {
|
||||||
|
|||||||
Reference in New Issue
Block a user