Track unexpected disconnects from AP

main
DaXcess 2024-09-24 22:59:14 +02:00
parent f437473462
commit c05624fc76
No known key found for this signature in database
GPG Key ID: CF78CC72F0FD5EAD
2 changed files with 44 additions and 4 deletions

View File

@ -13,13 +13,16 @@ use librespot::{
player::{Player as SpotifyPlayer, PlayerEvent as SpotifyPlayerEvent}, player::{Player as SpotifyPlayer, PlayerEvent as SpotifyPlayerEvent},
}, },
}; };
use log::error; use log::{error, trace};
use songbird::{input::RawAdapter, tracks::TrackHandle, Call}; use songbird::{input::RawAdapter, tracks::TrackHandle, Call};
use spoticord_audio::{ use spoticord_audio::{
sink::{SinkEvent, StreamSink}, sink::{SinkEvent, StreamSink},
stream::Stream, stream::Stream,
}; };
use std::{io::Write, sync::Arc}; use std::{
io::Write,
sync::{atomic::AtomicBool, Arc},
};
use tokio::sync::{mpsc, oneshot, Mutex}; use tokio::sync::{mpsc, oneshot, Mutex};
#[derive(Debug)] #[derive(Debug)]
@ -41,6 +44,7 @@ pub enum PlayerEvent {
Play, Play,
Stopped, Stopped,
TrackChanged(Box<PlaybackInfo>), TrackChanged(Box<PlaybackInfo>),
ConnectionReset,
} }
pub struct Player { pub struct Player {
@ -57,6 +61,9 @@ pub struct Player {
commands: mpsc::Receiver<PlayerCommand>, commands: mpsc::Receiver<PlayerCommand>,
spotify_events: mpsc::UnboundedReceiver<SpotifyPlayerEvent>, spotify_events: mpsc::UnboundedReceiver<SpotifyPlayerEvent>,
sink_events: mpsc::UnboundedReceiver<SinkEvent>, sink_events: mpsc::UnboundedReceiver<SinkEvent>,
/// A shared boolean that reflects whether this Player has shut down
shutdown: Arc<AtomicBool>,
} }
impl Player { impl Player {
@ -132,6 +139,7 @@ impl Player {
} }
}; };
let shutdown = Arc::new(AtomicBool::new(false));
let (tx, rx) = mpsc::channel(16); let (tx, rx) = mpsc::channel(16);
let player = Self { let player = Self {
session, session,
@ -141,15 +149,24 @@ impl Player {
playback_info: None, playback_info: None,
events: event_tx, events: event_tx.clone(),
commands: rx, commands: rx,
spotify_events: rx_player, spotify_events: rx_player,
sink_events: rx_sink, sink_events: rx_sink,
shutdown: shutdown.clone(),
}; };
// Launch it all! // Launch it all!
tokio::spawn(spirc_task); tokio::spawn(async move {
spirc_task.await;
// If the shutdown flag isn't set, we most likely lost connection to the Spotify AP
if !shutdown.load(std::sync::atomic::Ordering::SeqCst) {
_ = event_tx.send(PlayerEvent::ConnectionReset).await;
}
});
tokio::spawn(player.run()); tokio::spawn(player.run());
Ok((PlayerHandle { commands: tx }, event_rx)) Ok((PlayerHandle { commands: tx }, event_rx))
@ -178,6 +195,11 @@ impl Player {
else => break, else => break,
} }
} }
self.shutdown
.store(true, std::sync::atomic::Ordering::SeqCst);
trace!("End of Player::run");
} }
async fn handle_command(&mut self, command: PlayerCommand) { async fn handle_command(&mut self, command: PlayerCommand) {
@ -195,6 +217,8 @@ impl Player {
} }
async fn handle_spotify_event(&mut self, event: SpotifyPlayerEvent) { async fn handle_spotify_event(&mut self, event: SpotifyPlayerEvent) {
trace!("Spotify event received: {event:#?}");
match event { match event {
SpotifyPlayerEvent::PositionCorrection { position_ms, .. } SpotifyPlayerEvent::PositionCorrection { position_ms, .. }
| SpotifyPlayerEvent::Seeked { position_ms, .. } => { | SpotifyPlayerEvent::Seeked { position_ms, .. } => {

View File

@ -301,6 +301,22 @@ impl Session {
PlayerEvent::Pause => self.start_timeout(), PlayerEvent::Pause => self.start_timeout(),
PlayerEvent::Stopped => self.shutdown_player().await, PlayerEvent::Stopped => self.shutdown_player().await,
PlayerEvent::TrackChanged(_) => {} PlayerEvent::TrackChanged(_) => {}
PlayerEvent::ConnectionReset => {
self.disconnect().await;
_ = self
.text_channel
.send_message(
&self.context,
CreateMessage::new().embed(
CreateEmbed::new()
.title("Spotify connection lost")
.description("The bot has lost connection to the Spotify AP servers.\nThis is most likely caused by a connection reset on Spotify's end.\n\nUse `/join` to resummon the bot to your voice channel.")
.color(Colors::Error),
),
)
.await;
}
} }
let force_edit = !matches!(event, PlayerEvent::TrackChanged(_)); let force_edit = !matches!(event, PlayerEvent::TrackChanged(_));