Fix issue #3, add some extra cases for auto disconnect

main
DaXcess 2022-11-08 09:40:18 +01:00
parent 7405656854
commit b57b4127c6
3 changed files with 45 additions and 2 deletions

View File

@ -7,6 +7,8 @@ pub enum IpcPacket {
Connect(String, String), Connect(String, String),
Disconnect, Disconnect,
ConnectError(String),
StartPlayback, StartPlayback,
StopPlayback, StopPlayback,

View File

@ -59,7 +59,13 @@ impl SpoticordPlayer {
// Connect the session // Connect the session
let (session, _) = match Session::connect(session_config, credentials, None, false).await { let (session, _) = match Session::connect(session_config, credentials, None, false).await {
Ok((session, credentials)) => (session, credentials), Ok((session, credentials)) => (session, credentials),
Err(why) => panic!("Failed to connect: {}", why), Err(why) => {
self
.client
.send(IpcPacket::ConnectError(why.to_string()))
.unwrap();
return;
}
}; };
// Store session for later use // Store session for later use

View File

@ -199,6 +199,35 @@ impl SpoticordSession {
trace!("Received IPC message: {:?}", msg); trace!("Received IPC message: {:?}", msg);
match msg { match msg {
// Session connect error
IpcPacket::ConnectError(why) => {
error!("Failed to connect to Spotify: {:?}", why);
// Notify the user in the text channel
if let Err(why) = ipc_instance
.text_channel_id
.send_message(&ipc_instance.http, |message| {
message.embed(|embed| {
embed.title("Failed to connect to Spotify");
embed.description(why);
embed.color(Status::Error as u64);
embed
});
message
})
.await
{
error!("Failed to send error message: {:?}", why);
}
// Clean up session
ipc_instance.player_stopped().await;
break;
}
// Sink requests playback to start/resume // Sink requests playback to start/resume
IpcPacket::StartPlayback => { IpcPacket::StartPlayback => {
check_result(ipc_track.play()); check_result(ipc_track.play());
@ -217,6 +246,8 @@ impl SpoticordSession {
let mut instance = ipc_instance.clone(); let mut instance = ipc_instance.clone();
let context = ipc_context.clone(); let context = ipc_context.clone();
// TODO: Check if this is actually needed
// The new backend may have fixed the need for this
tokio::spawn(async move { tokio::spawn(async move {
if let Err(why) = instance.update_track(&context, &owner_id, track_id).await { if let Err(why) = instance.update_track(&context, &owner_id, track_id).await {
error!("Failed to update track: {:?}", why); error!("Failed to update track: {:?}", why);
@ -274,7 +305,8 @@ impl SpoticordSession {
} }
IpcPacket::Stopped => { IpcPacket::Stopped => {
ipc_instance.player_stopped().await; check_result(ipc_track.pause());
ipc_instance.start_disconnect_timer().await;
} }
// Ignore other packets // Ignore other packets
@ -441,6 +473,9 @@ impl SpoticordSession {
// Clear playback info // Clear playback info
let mut playback_info = self.playback_info.write().await; let mut playback_info = self.playback_info.write().await;
*playback_info = None; *playback_info = None;
// Disconnect automatically after some time
self.start_disconnect_timer().await;
} }
/// Internal version of disconnect, which does not abort the disconnect timer /// Internal version of disconnect, which does not abort the disconnect timer