Skip to content

Commit

Permalink
feat(spotify): Disable spotify intagration via spotify feature unti…
Browse files Browse the repository at this point in the history
…l login method is fixed
  • Loading branch information
kinkard committed Sep 8, 2024
1 parent f908420 commit e7b0275
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 16 deletions.
17 changes: 10 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
21 changes: 17 additions & 4 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,23 @@ pub(crate) async fn play(ctx: Context<'_>, query: String) -> Result<(), anyhow::
}
});

#[cfg(feature = "spotify")]
let spotify_tracks: Option<SmallVec<[(track_info::Metadata, Input); 1]>> = 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<SmallVec<[(track_info::Metadata, Input); 1]>> = 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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use tracing::{info, warn};
mod commands;
mod events;
mod radiot;
#[cfg(feature = "spotify")]
mod spotify;
mod storage;
mod track_info;
Expand All @@ -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>;
Expand All @@ -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.
Expand All @@ -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| {
Expand Down
7 changes: 6 additions & 1 deletion src/storage.rs
Original file line number Diff line number Diff line change
@@ -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<rusqlite::Connection>);

Expand All @@ -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(
Expand Down Expand Up @@ -98,6 +102,7 @@ mod tests {
use pretty_assertions::assert_eq;

#[test]
#[cfg(feature = "spotify")]
fn spotify_credentials_storage() {
let storage: Arc<dyn spotify::CredentialsStorage> = Storage::new(":memory:").unwrap();

Expand Down

0 comments on commit e7b0275

Please sign in to comment.