main
DaXcess 2024-09-20 07:32:08 +00:00
parent b9b0051523
commit f437473462
No known key found for this signature in database
GPG Key ID: CF78CC72F0FD5EAD
15 changed files with 414 additions and 340 deletions

View File

@ -1,5 +1,10 @@
# Changelog
## 2.2.3 | September 20th 2024
- Made backend changes to librespot to prevent deadlocking by not waiting for thread shutdowns
- Added retrying to Spotify login logic to reduce the chance of the bot failing to connect
## 2.2.2 | September 2nd 2024
- Added backtrace logging to player creation to figure out some mystery crashes

601
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
[package]
name = "spoticord"
version = "2.2.2"
version = "2.2.3"
edition = "2021"
rust-version = "1.80.0"

View File

@ -45,7 +45,7 @@ ARG TARGETPLATFORM
ENV TARGETPLATFORM=${TARGETPLATFORM}
# Add extra runtime dependencies here
RUN apt update && apt install -y ca-certificates
RUN apt update && apt install -y ca-certificates libpq-dev
# Copy spoticord binaries from builder to /tmp so we can dynamically use them
COPY --from=builder \

View File

@ -1,6 +1,6 @@
[package]
name = "spoticord_audio"
version = "2.2.2"
version = "2.2.3"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -1,6 +1,6 @@
[package]
name = "spoticord_config"
version = "2.2.2"
version = "2.2.3"
edition = "2021"
[dependencies]

View File

@ -1,6 +1,6 @@
[package]
name = "spoticord_database"
version = "2.2.2"
version = "2.2.3"
edition = "2021"
[dependencies]

View File

@ -1,6 +1,6 @@
[package]
name = "spoticord_player"
version = "2.2.2"
version = "2.2.3"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -101,18 +101,36 @@ impl Player {
);
let rx_player = player.get_player_event_channel();
let (spirc, spirc_task) = Spirc::new(
ConnectConfig {
name: device_name.into(),
initial_volume: Some((0.75f32 * u16::MAX as f32) as u16),
..Default::default()
},
session.clone(),
credentials,
player,
mixer,
)
.await?;
let device_name = device_name.into();
let mut tries = 0;
let (spirc, spirc_task) = loop {
match Spirc::new(
ConnectConfig {
name: device_name.clone(),
initial_volume: Some((0.75f32 * u16::MAX as f32) as u16),
..Default::default()
},
session.clone(),
credentials.clone(),
player.clone(),
mixer.clone(),
)
.await
{
Ok(spirc) => break spirc,
Err(why) => {
tries += 1;
if tries > 3 {
error!("Failed to connect to Spirc: {why}");
return Err(why.into());
}
continue;
}
}
};
let (tx, rx) = mpsc::channel(16);
let player = Self {

View File

@ -1,6 +1,6 @@
[package]
name = "spoticord_session"
version = "2.2.2"
version = "2.2.3"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -101,18 +101,36 @@ impl Session {
// Grab user credentials and info before joining call
let credentials =
retrieve_credentials(&session_manager.database(), owner.to_string()).await?;
let device_name = session_manager
.database()
.get_user(owner.to_string())
.await?
.device_name;
match retrieve_credentials(&session_manager.database(), owner.to_string()).await {
Ok(credentials) => credentials,
Err(why) => {
error!("Failed to retrieve credentials: {why}");
return Err(why.into());
}
};
let device_name = match session_manager.database().get_user(owner.to_string()).await {
Ok(user) => user.device_name,
Err(why) => {
error!("Failed to get database user: {why}");
return Err(why.into());
}
};
// Hello Discord I'm here
let call = session_manager
let call = match session_manager
.songbird()
.join(guild_id, voice_channel_id)
.await?;
.await
{
Ok(call) => call,
Err(why) => {
error!("Failed to join voice channel: {why}");
return Err(why.into());
}
};
// Make sure call guard is dropped or else we can't execute session.run
{
@ -173,6 +191,8 @@ impl Session {
loop {
tokio::select! {
opt_command = self.commands.recv() => {
trace!("Received command: {opt_command:#?}");
let Some(command) = opt_command else {
break;
};
@ -183,6 +203,8 @@ impl Session {
},
opt_event = self.events.recv(), if self.active => {
trace!("Received event: {opt_event:#?}");
let Some(event) = opt_event else {
self.shutdown_player().await;
continue;
@ -193,6 +215,8 @@ impl Session {
// Internal communication channel
Some(command) = self.commands_inner_rx.recv() => {
trace!("Received internal command: {command:#?}");
if self.handle_command(command).await.is_break() {
break;
}
@ -201,6 +225,8 @@ impl Session {
else => break,
}
}
trace!("End of Session::run");
}
async fn handle_command(&mut self, command: SessionCommand) -> ControlFlow<(), ()> {
@ -519,8 +545,10 @@ impl songbird::EventHandler for SessionHandle {
}
match event {
// NOTE: Discord can randomly make the driver disconnect when users join/leave the voice channel
// Nothing we can do about it at this time since that is an issue with either Discord or Songbird
EventContext::DriverDisconnect(_) => {
debug!("Bot disconnected from call, cleaning up");
debug!("Bot disconnected from voice gateway, cleaning up");
self.disconnect().await;
}

View File

@ -1,6 +1,6 @@
[package]
name = "spoticord_stats"
version = "2.2.2"
version = "2.2.3"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -1,6 +1,6 @@
[package]
name = "spoticord_utils"
version = "2.2.2"
version = "2.2.3"
edition = "2021"
[dependencies]
@ -9,3 +9,4 @@ anyhow = "1.0.86"
base64 = "0.22.1"
log = "0.4.22"
poise = "0.6.1"
tokio = { version = "1.40.0", features = ["time"], default-features = false }

View File

@ -5,7 +5,8 @@ use librespot::{
discovery::Credentials,
protocol::{authentication::AuthenticationType, keyexchange::ErrorCode},
};
use log::trace;
use log::debug;
use std::time::Duration;
pub async fn validate_token(
username: impl Into<String>,
@ -19,12 +20,12 @@ pub async fn validate_token(
auth_data,
};
trace!("Validating session token for {}", credentials.username);
debug!("Validating session token for {}", credentials.username);
let new_credentials = request_session_token(credentials.clone()).await?;
if credentials.auth_data != new_credentials.auth_data {
trace!("New session token retrieved for {}", credentials.username);
debug!("New session token retrieved for {}", credentials.username);
return Ok(Some(BASE64.encode(new_credentials.auth_data)));
}
@ -33,14 +34,29 @@ pub async fn validate_token(
}
pub async fn request_session_token(credentials: Credentials) -> Result<Credentials> {
trace!("Requesting session token for {}", credentials.username);
debug!("Requesting session token for {}", credentials.username);
let session = Session::new(SessionConfig::default(), None);
let mut tries = 0;
Ok(loop {
let (host, port) = session.apresolver().resolve("accesspoint").await?;
let mut transport = librespot::core::connection::connect(&host, port, None).await?;
let mut transport = match librespot::core::connection::connect(&host, port, None).await {
Ok(transport) => transport,
Err(why) => {
// Retry
tries += 1;
if tries > 3 {
return Err(why.into());
}
tokio::time::sleep(Duration::from_millis(100)).await;
continue;
}
};
match librespot::core::connection::authenticate(
&mut transport,
@ -59,6 +75,8 @@ pub async fn request_session_token(credentials: Credentials) -> Result<Credentia
return Err(e.into());
}
tokio::time::sleep(Duration::from_millis(100)).await;
continue;
} else {
return Err(e.into());

View File

@ -17,11 +17,6 @@ pub type FrameworkError<'a> = poise::FrameworkError<'a, Data, anyhow::Error>;
type Data = SessionManager;
// pub struct Data {
// pub database: Database,
// pub session_manager: SessionManager,
// }
pub fn framework_opts() -> FrameworkOptions<Data, anyhow::Error> {
poise::FrameworkOptions {
commands: vec![
@ -111,14 +106,14 @@ async fn background_loop(
#[cfg(feature = "stats")] mut stats_manager: spoticord_stats::StatsManager,
) {
#[cfg(feature = "stats")]
use log::{error, trace};
use log::error;
loop {
tokio::select! {
_ = tokio::time::sleep(std::time::Duration::from_secs(60)) => {
#[cfg(feature = "stats")]
{
trace!("Retrieving active sessions count for stats");
debug!("Retrieving active sessions count for stats");
let mut count = 0;
@ -131,7 +126,7 @@ async fn background_loop(
if let Err(why) = stats_manager.set_active_count(count) {
error!("Failed to update active sessions: {why}");
} else {
trace!("Active session count set to: {count}");
debug!("Active session count set to: {count}");
}
}
}