Skip to content

Commit e7b0275

Browse files
committed
feat(spotify): Disable spotify intagration via spotify feature until login method is fixed
1 parent f908420 commit e7b0275

File tree

5 files changed

+42
-16
lines changed

5 files changed

+42
-16
lines changed

Cargo.toml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,16 @@ reqwest = { version = "0.11", default-features = false, features = ["rustls-tls"
3636
serde = { version = "1", features = ["derive"] }
3737
serde_json = "1"
3838
# Use 8f9bec21 pre-hyper 1.x version to keep single version of hyper v0.14, as serenity uses hyper v0.14 as well
39-
librespot-core = { git = "https://github.com/librespot-org/librespot.git", rev = "8f9bec21" }
40-
librespot-discovery = { git = "https://github.com/librespot-org/librespot.git", rev = "8f9bec21" }
41-
librespot-metadata = { git = "https://github.com/librespot-org/librespot.git", rev = "8f9bec21" }
42-
librespot-playback = { git = "https://github.com/librespot-org/librespot.git", rev = "8f9bec21", default-features = false }
43-
flume = { version = "0.11", default-features = false }
44-
hex = { version = "0.4" }
45-
sha1 = "0.10"
39+
librespot-core = { git = "https://github.com/librespot-org/librespot.git", rev = "8f9bec21", optional = true}
40+
librespot-discovery = { git = "https://github.com/librespot-org/librespot.git", rev = "8f9bec21", optional = true }
41+
librespot-metadata = { git = "https://github.com/librespot-org/librespot.git", rev = "8f9bec21", optional = true }
42+
librespot-playback = { git = "https://github.com/librespot-org/librespot.git", rev = "8f9bec21", default-features = false, optional = true }
43+
flume = { version = "0.11", default-features = false, optional = true }
44+
hex = { version = "0.4", optional = true }
45+
sha1 = { version = "0.10", optional = true }
4646

4747
[dev-dependencies]
4848
pretty_assertions = "1"
49+
50+
[features]
51+
spotify = ["dep:librespot-core", "dep:librespot-discovery", "dep:librespot-metadata", "dep:librespot-playback"]

src/commands.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,23 @@ pub(crate) async fn play(ctx: Context<'_>, query: String) -> Result<(), anyhow::
8585
}
8686
});
8787

88+
#[cfg(feature = "spotify")]
89+
let spotify_tracks: Option<SmallVec<[(track_info::Metadata, Input); 1]>> = ctx
90+
.data()
91+
.spotify_resolver
92+
.resolve(guild_id, &query)
93+
.await
94+
.map(|tracks| {
95+
tracks
96+
.into_iter()
97+
.map(|track| (track.metadata().clone(), track.into()))
98+
.collect()
99+
});
100+
#[cfg(not(feature = "spotify"))]
101+
let spotify_tracks: Option<SmallVec<[(track_info::Metadata, Input); 1]>> = None;
102+
88103
let resolved_items: SmallVec<[(track_info::Metadata, Input); 1]> =
89-
if let Some(tracks) = ctx.data().spotify_resolver.resolve(guild_id, &query).await {
104+
if let Some(tracks) = spotify_tracks {
90105
if tracks.is_empty() {
91106
ctx.reply(format!(
92107
"Invalid Spotify query '{query}'. Please try something else"
@@ -95,9 +110,6 @@ pub(crate) async fn play(ctx: Context<'_>, query: String) -> Result<(), anyhow::
95110
return Ok(());
96111
}
97112
tracks
98-
.into_iter()
99-
.map(|track| (track.metadata().clone(), track.into()))
100-
.collect()
101113
} else if let Some(podcast) = ctx.data().radio_t_resolver.resolve(&query).await {
102114
smallvec![(podcast.metadata().clone(), podcast.into())]
103115
} 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> {
192204
/// Connect Spotify account to be used by bot.
193205
/// https://www.spotify.com/us/account/set-device-password/
194206
#[poise::command(guild_only, slash_command)]
207+
#[cfg(feature = "spotify")]
195208
pub(crate) async fn connect_spotify(
196209
ctx: Context<'_>,
197210
username: String,

src/events.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,14 @@ async fn bot_changed_vc(
118118
}
119119

120120
/// Invoked when bot left voice channel
121-
async fn bot_left_vc(ctx: &Context, data: &Data, guild_id: GuildId) {
121+
async fn bot_left_vc(ctx: &Context, _data: &Data, guild_id: GuildId) {
122122
info!(
123123
"Left voice chat in '{}' guild",
124124
&ctx.cache.guild(guild_id).unwrap().name
125125
);
126126

127-
data.spotify_resolver.disconnect(guild_id).await;
127+
#[cfg(feature = "spotify")]
128+
_data.spotify_resolver.disconnect(guild_id).await;
128129
}
129130

130131
/// Invoked when user joined a voice channel

src/main.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use tracing::{info, warn};
1010
mod commands;
1111
mod events;
1212
mod radiot;
13+
#[cfg(feature = "spotify")]
1314
mod spotify;
1415
mod storage;
1516
mod track_info;
@@ -18,6 +19,7 @@ mod yt_dlp;
1819
struct Data {
1920
yt_dlp_resolver: yt_dlp::Resolver,
2021
radio_t_resolver: radiot::Resolver,
22+
#[cfg(feature = "spotify")]
2123
spotify_resolver: spotify::Resolver,
2224
}
2325
type Context<'a> = poise::Context<'a, Data, anyhow::Error>;
@@ -42,9 +44,10 @@ async fn main() {
4244

4345
let http_client = reqwest::Client::new();
4446
let bot_data = Data {
45-
yt_dlp_resolver: yt_dlp::Resolver::new(http_client.clone(), storage.clone()),
47+
#[cfg(feature = "spotify")]
48+
spotify_resolver: spotify::Resolver::new(storage.clone()),
49+
yt_dlp_resolver: yt_dlp::Resolver::new(http_client.clone(), storage),
4650
radio_t_resolver: radiot::Resolver::new(http_client.clone()),
47-
spotify_resolver: spotify::Resolver::new(storage),
4851
};
4952

5053
// Configure the client with your Discord bot token in the environment.
@@ -67,6 +70,7 @@ async fn main() {
6770
commands::play(),
6871
commands::skip(),
6972
commands::stop(),
73+
#[cfg(feature = "spotify")]
7074
commands::connect_spotify(),
7175
],
7276
event_handler: |ctx, event, _framework, data| {

src/storage.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use std::path::Path;
22
use std::sync::{Arc, Mutex};
33

4+
#[cfg(feature = "spotify")]
45
use serenity::all::GuildId;
56

6-
use crate::{spotify, yt_dlp};
7+
#[cfg(feature = "spotify")]
8+
use crate::spotify;
9+
use crate::yt_dlp;
710

811
pub(crate) struct Storage(Mutex<rusqlite::Connection>);
912

@@ -29,6 +32,7 @@ impl Storage {
2932
}
3033
}
3134

35+
#[cfg(feature = "spotify")]
3236
impl spotify::CredentialsStorage for Storage {
3337
fn save(&self, guild_id: GuildId, username: &str, password: &str) -> Result<(), anyhow::Error> {
3438
self.0.lock().unwrap().execute(
@@ -98,6 +102,7 @@ mod tests {
98102
use pretty_assertions::assert_eq;
99103

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

0 commit comments

Comments
 (0)