refactor: improve presence handling and update disconnect logic for clarity

This commit is contained in:
HorizonCode committed 2026-02-28 16:56:46 +01:00
1 parent 31b7d17b09
commit 9770e81d6f
1 file changed
+10 -14
+10 -14
View File
@@ -1,9 +1,10 @@
use discord_rich_presence::{ use discord_rich_presence::{
DiscordIpc, DiscordIpcClient,
activity::{Activity, Assets, Button, Timestamps}, activity::{Activity, Assets, Button, Timestamps},
DiscordIpc, DiscordIpcClient,
}; };
use futures_util::future::BoxFuture;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::future::Future;
use std::pin::Pin;
use std::sync::Mutex as StdMutex; use std::sync::Mutex as StdMutex;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use tokio::sync::{mpsc, oneshot}; use tokio::sync::{mpsc, oneshot};
@@ -90,7 +91,7 @@ impl PresenceActor {
} }
} }
fn handle_connect(&mut self, responder: oneshot::Sender<bool>) -> BoxFuture<'_, ()> { fn handle_connect(&mut self, responder: oneshot::Sender<bool>) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
Box::pin(async move { Box::pin(async move {
if self.client.is_some() { if self.client.is_some() {
let _ = responder.send(true); let _ = responder.send(true);
@@ -125,9 +126,9 @@ impl PresenceActor {
async fn handle_disconnect(&mut self, responder: oneshot::Sender<()>) { async fn handle_disconnect(&mut self, responder: oneshot::Sender<()>) {
if let Some(mut client) = self.client.take() { if let Some(mut client) = self.client.take() {
println!("Actor: Disconnecting..."); println!("Actor: Disconnecting from Discord...");
let _ = client.clear_activity().map_err(|e| e.to_string()); let _ = client.clear_activity();
let _ = client.close().map_err(|e| e.to_string()); let _ = client.close();
println!("Actor: Disconnected successfully."); println!("Actor: Disconnected successfully.");
} }
let _ = responder.send(()); let _ = responder.send(());
@@ -200,14 +201,9 @@ pub async fn connect() -> bool {
pub async fn disconnect() { pub async fn disconnect() {
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
if PRESENCE_TX // Use a timeout to ensure we don't hang the exit process if Discord is unresponsive
.send(PresenceCommand::Disconnect(tx)) if let Ok(_) = PRESENCE_TX.send(PresenceCommand::Disconnect(tx)).await {
.await let _ = tokio::time::timeout(Duration::from_secs(2), rx).await;
.is_ok()
{
let _ = rx.await;
} else {
println!("Could not send disconnect command; actor may not be running.");
} }
} }