Added abort to disconnect timer on cancel

main
DaXcess 2022-11-07 14:00:23 +01:00
parent b7c85455ce
commit 960f8d89f5
3 changed files with 38 additions and 10 deletions

View File

@ -163,7 +163,7 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
.icon_url(owner.face()) .icon_url(owner.face())
) )
.thumbnail(&thumbnail) .thumbnail(&thumbnail)
.color(Status::Success as u64) .color(Status::Info as u64)
) )
}) })
}) })

View File

@ -2,6 +2,6 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const MOTD: &str = "OPEN BETA (v2)"; pub const MOTD: &str = "OPEN BETA (v2)";
/// The time it takes for Spoticord to disconnect when no music is being played /// The time it takes for Spoticord to disconnect when no music is being played
pub const DISCONNECT_TIME: u64 = 5 * 60; pub const DISCONNECT_TIME: u64 = 10;
// pub const MOTD: &str = "some good 'ol music"; // pub const MOTD: &str = "some good 'ol music";

View File

@ -49,6 +49,8 @@ pub struct SpoticordSession {
playback_info: Arc<RwLock<Option<PlaybackInfo>>>, playback_info: Arc<RwLock<Option<PlaybackInfo>>>,
disconnect_handle: Arc<Mutex<Option<tokio::task::JoinHandle<()>>>>,
client: Client, client: Client,
} }
@ -151,6 +153,7 @@ impl SpoticordSession {
call: call.clone(), call: call.clone(),
track: track_handle.clone(), track: track_handle.clone(),
playback_info: Arc::new(RwLock::new(None)), playback_info: Arc::new(RwLock::new(None)),
disconnect_handle: Arc::new(Mutex::new(None)),
client: client.clone(), client: client.clone(),
}; };
@ -439,10 +442,8 @@ impl SpoticordSession {
*playback_info = None; *playback_info = None;
} }
// Disconnect from voice channel and remove session from manager /// Internal version of disconnect, which does not abort the disconnect timer
pub async fn disconnect(&self) { async fn disconnect_no_abort(&self) {
info!("Disconnecting from voice channel {}", self.channel_id);
self self
.session_manager .session_manager
.clone() .clone()
@ -459,9 +460,23 @@ impl SpoticordSession {
} }
} }
// Disconnect from voice channel and remove session from manager
pub async fn disconnect(&self) {
info!("Disconnecting from voice channel {}", self.channel_id);
self.disconnect_no_abort().await;
// Stop the disconnect timer, if one is running
let mut dc_handle = self.disconnect_handle.lock().await;
if let Some(handle) = dc_handle.take() {
handle.abort();
}
}
/// Disconnect from voice channel with a message /// Disconnect from voice channel with a message
pub async fn disconnect_with_message(&self, content: &str) { pub async fn disconnect_with_message(&self, content: &str) {
self.disconnect().await; self.disconnect_no_abort().await;
if let Err(why) = self if let Err(why) = self
.text_channel_id .text_channel_id
@ -475,10 +490,16 @@ impl SpoticordSession {
}) })
}) })
.await .await
.map(|_| ())
{ {
error!("Failed to send disconnect message: {:?}", why); error!("Failed to send disconnect message: {:?}", why);
} }
// Stop the disconnect timer, if one is running
let mut dc_handle = self.disconnect_handle.lock().await;
if let Some(handle) = dc_handle.take() {
handle.abort();
}
} }
// Update playback info (duration, position, playing state) // Update playback info (duration, position, playing state)
@ -512,7 +533,14 @@ impl SpoticordSession {
let pbi = self.playback_info.clone(); let pbi = self.playback_info.clone();
let instance = self.clone(); let instance = self.clone();
tokio::spawn(async move { let mut handle = self.disconnect_handle.lock().await;
// Abort the previous timer, if one is running
if let Some(handle) = handle.take() {
handle.abort();
}
*handle = Some(tokio::spawn(async move {
let mut timer = tokio::time::interval(Duration::from_secs(DISCONNECT_TIME)); let mut timer = tokio::time::interval(Duration::from_secs(DISCONNECT_TIME));
// Ignore first (immediate) tick // Ignore first (immediate) tick
@ -541,7 +569,7 @@ impl SpoticordSession {
break; break;
} }
} }
}); }));
} }
// Get the playback info for the current track // Get the playback info for the current track