From e7b02750232c70a0526362d312ab4d5786f9626d Mon Sep 17 00:00:00 2001 From: Stepan Kizim Date: Sun, 8 Sep 2024 21:57:49 +0200 Subject: [PATCH] feat(spotify): Disable spotify intagration via `spotify` feature until login method is fixed --- Cargo.toml | 17 ++++++++++------- src/commands.rs | 21 +++++++++++++++++---- src/events.rs | 5 +++-- src/main.rs | 8 ++++++-- src/storage.rs | 7 ++++++- 5 files changed, 42 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 710b21b..8b040ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,13 +36,16 @@ reqwest = { version = "0.11", default-features = false, features = ["rustls-tls" serde = { version = "1", features = ["derive"] } serde_json = "1" # Use 8f9bec21 pre-hyper 1.x version to keep single version of hyper v0.14, as serenity uses hyper v0.14 as well -librespot-core = { git = "https://github.com/librespot-org/librespot.git", rev = "8f9bec21" } -librespot-discovery = { git = "https://github.com/librespot-org/librespot.git", rev = "8f9bec21" } -librespot-metadata = { git = "https://github.com/librespot-org/librespot.git", rev = "8f9bec21" } -librespot-playback = { git = "https://github.com/librespot-org/librespot.git", rev = "8f9bec21", default-features = false } -flume = { version = "0.11", default-features = false } -hex = { version = "0.4" } -sha1 = "0.10" +librespot-core = { git = "https://github.com/librespot-org/librespot.git", rev = "8f9bec21", optional = true} +librespot-discovery = { git = "https://github.com/librespot-org/librespot.git", rev = "8f9bec21", optional = true } +librespot-metadata = { git = "https://github.com/librespot-org/librespot.git", rev = "8f9bec21", optional = true } +librespot-playback = { git = "https://github.com/librespot-org/librespot.git", rev = "8f9bec21", default-features = false, optional = true } +flume = { version = "0.11", default-features = false, optional = true } +hex = { version = "0.4", optional = true } +sha1 = { version = "0.10", optional = true } [dev-dependencies] pretty_assertions = "1" + +[features] +spotify = ["dep:librespot-core", "dep:librespot-discovery", "dep:librespot-metadata", "dep:librespot-playback"] diff --git a/src/commands.rs b/src/commands.rs index c51292d..ec42d63 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -85,8 +85,23 @@ pub(crate) async fn play(ctx: Context<'_>, query: String) -> Result<(), anyhow:: } }); + #[cfg(feature = "spotify")] + let spotify_tracks: Option> = ctx + .data() + .spotify_resolver + .resolve(guild_id, &query) + .await + .map(|tracks| { + tracks + .into_iter() + .map(|track| (track.metadata().clone(), track.into())) + .collect() + }); + #[cfg(not(feature = "spotify"))] + let spotify_tracks: Option> = None; + let resolved_items: SmallVec<[(track_info::Metadata, Input); 1]> = - if let Some(tracks) = ctx.data().spotify_resolver.resolve(guild_id, &query).await { + if let Some(tracks) = spotify_tracks { if tracks.is_empty() { ctx.reply(format!( "Invalid Spotify query '{query}'. Please try something else" @@ -95,9 +110,6 @@ pub(crate) async fn play(ctx: Context<'_>, query: String) -> Result<(), anyhow:: return Ok(()); } tracks - .into_iter() - .map(|track| (track.metadata().clone(), track.into())) - .collect() } else if let Some(podcast) = ctx.data().radio_t_resolver.resolve(&query).await { smallvec![(podcast.metadata().clone(), podcast.into())] } else if let Some(yt_dlp) = ctx.data().yt_dlp_resolver.resolve(&query).await { @@ -192,6 +204,7 @@ pub(crate) async fn stop(ctx: Context<'_>) -> Result<(), anyhow::Error> { /// Connect Spotify account to be used by bot. /// https://www.spotify.com/us/account/set-device-password/ #[poise::command(guild_only, slash_command)] +#[cfg(feature = "spotify")] pub(crate) async fn connect_spotify( ctx: Context<'_>, username: String, diff --git a/src/events.rs b/src/events.rs index 12d79bd..0e174c3 100644 --- a/src/events.rs +++ b/src/events.rs @@ -118,13 +118,14 @@ async fn bot_changed_vc( } /// Invoked when bot left voice channel -async fn bot_left_vc(ctx: &Context, data: &Data, guild_id: GuildId) { +async fn bot_left_vc(ctx: &Context, _data: &Data, guild_id: GuildId) { info!( "Left voice chat in '{}' guild", &ctx.cache.guild(guild_id).unwrap().name ); - data.spotify_resolver.disconnect(guild_id).await; + #[cfg(feature = "spotify")] + _data.spotify_resolver.disconnect(guild_id).await; } /// Invoked when user joined a voice channel diff --git a/src/main.rs b/src/main.rs index e933c66..c823112 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ use tracing::{info, warn}; mod commands; mod events; mod radiot; +#[cfg(feature = "spotify")] mod spotify; mod storage; mod track_info; @@ -18,6 +19,7 @@ mod yt_dlp; struct Data { yt_dlp_resolver: yt_dlp::Resolver, radio_t_resolver: radiot::Resolver, + #[cfg(feature = "spotify")] spotify_resolver: spotify::Resolver, } type Context<'a> = poise::Context<'a, Data, anyhow::Error>; @@ -42,9 +44,10 @@ async fn main() { let http_client = reqwest::Client::new(); let bot_data = Data { - yt_dlp_resolver: yt_dlp::Resolver::new(http_client.clone(), storage.clone()), + #[cfg(feature = "spotify")] + spotify_resolver: spotify::Resolver::new(storage.clone()), + yt_dlp_resolver: yt_dlp::Resolver::new(http_client.clone(), storage), radio_t_resolver: radiot::Resolver::new(http_client.clone()), - spotify_resolver: spotify::Resolver::new(storage), }; // Configure the client with your Discord bot token in the environment. @@ -67,6 +70,7 @@ async fn main() { commands::play(), commands::skip(), commands::stop(), + #[cfg(feature = "spotify")] commands::connect_spotify(), ], event_handler: |ctx, event, _framework, data| { diff --git a/src/storage.rs b/src/storage.rs index 67190e1..c0a9108 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -1,9 +1,12 @@ use std::path::Path; use std::sync::{Arc, Mutex}; +#[cfg(feature = "spotify")] use serenity::all::GuildId; -use crate::{spotify, yt_dlp}; +#[cfg(feature = "spotify")] +use crate::spotify; +use crate::yt_dlp; pub(crate) struct Storage(Mutex); @@ -29,6 +32,7 @@ impl Storage { } } +#[cfg(feature = "spotify")] impl spotify::CredentialsStorage for Storage { fn save(&self, guild_id: GuildId, username: &str, password: &str) -> Result<(), anyhow::Error> { self.0.lock().unwrap().execute( @@ -98,6 +102,7 @@ mod tests { use pretty_assertions::assert_eq; #[test] + #[cfg(feature = "spotify")] fn spotify_credentials_storage() { let storage: Arc = Storage::new(":memory:").unwrap();