1use std::sync::{
2 Arc,
3 atomic::{AtomicBool, Ordering},
4};
5use tokio::sync::mpsc::Sender;
6
7use crate::Activity;
8use crate::types::IPCCommand;
9
10#[derive(Debug, Clone)]
12pub struct DiscordIPCClient {
13 pub(crate) tx: Sender<IPCCommand>,
14 pub(crate) client_id: String,
15 pub(crate) running: Arc<AtomicBool>,
16}
17
18impl DiscordIPCClient {
19 pub fn client_id(&self) -> String {
21 self.client_id.clone()
22 }
23
24 pub fn is_running(&self) -> bool {
26 self.running.load(Ordering::SeqCst)
27 }
28
29 pub async fn set_activity(&self, activity: Activity) -> Result<(), anyhow::Error> {
32 if !self.is_running() {
33 anyhow::bail!("Call .run() before .set_activity() execution.");
34 }
35
36 self.tx.send(IPCCommand::SetActivity { activity }).await?;
37 Ok(())
38 }
39
40 pub async fn clear_activity(&self) -> Result<(), anyhow::Error> {
42 if self.is_running() {
43 self.tx.send(IPCCommand::ClearActivity).await?;
44 }
45
46 Ok(())
47 }
48
49 pub async fn close(&self) -> Result<(), anyhow::Error> {
51 if self.is_running() {
52 let _ = self.tx.send(IPCCommand::Close).await;
53 }
54
55 Ok(())
56 }
57}