Added abort to disconnect timer on cancel
parent
b7c85455ce
commit
960f8d89f5
|
@ -163,7 +163,7 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
|
|||
.icon_url(owner.face())
|
||||
)
|
||||
.thumbnail(&thumbnail)
|
||||
.color(Status::Success as u64)
|
||||
.color(Status::Info as u64)
|
||||
)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -2,6 +2,6 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
|||
pub const MOTD: &str = "OPEN BETA (v2)";
|
||||
|
||||
/// 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";
|
||||
|
|
|
@ -49,6 +49,8 @@ pub struct SpoticordSession {
|
|||
|
||||
playback_info: Arc<RwLock<Option<PlaybackInfo>>>,
|
||||
|
||||
disconnect_handle: Arc<Mutex<Option<tokio::task::JoinHandle<()>>>>,
|
||||
|
||||
client: Client,
|
||||
}
|
||||
|
||||
|
@ -151,6 +153,7 @@ impl SpoticordSession {
|
|||
call: call.clone(),
|
||||
track: track_handle.clone(),
|
||||
playback_info: Arc::new(RwLock::new(None)),
|
||||
disconnect_handle: Arc::new(Mutex::new(None)),
|
||||
client: client.clone(),
|
||||
};
|
||||
|
||||
|
@ -439,10 +442,8 @@ impl SpoticordSession {
|
|||
*playback_info = None;
|
||||
}
|
||||
|
||||
// Disconnect from voice channel and remove session from manager
|
||||
pub async fn disconnect(&self) {
|
||||
info!("Disconnecting from voice channel {}", self.channel_id);
|
||||
|
||||
/// Internal version of disconnect, which does not abort the disconnect timer
|
||||
async fn disconnect_no_abort(&self) {
|
||||
self
|
||||
.session_manager
|
||||
.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
|
||||
pub async fn disconnect_with_message(&self, content: &str) {
|
||||
self.disconnect().await;
|
||||
self.disconnect_no_abort().await;
|
||||
|
||||
if let Err(why) = self
|
||||
.text_channel_id
|
||||
|
@ -475,10 +490,16 @@ impl SpoticordSession {
|
|||
})
|
||||
})
|
||||
.await
|
||||
.map(|_| ())
|
||||
{
|
||||
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)
|
||||
|
@ -512,7 +533,14 @@ impl SpoticordSession {
|
|||
let pbi = self.playback_info.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));
|
||||
|
||||
// Ignore first (immediate) tick
|
||||
|
@ -541,7 +569,7 @@ impl SpoticordSession {
|
|||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
// Get the playback info for the current track
|
||||
|
|
Loading…
Reference in New Issue