1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! Discord Rich Presence utilities

use discord_sdk::{
    activity::{Activity, ActivityBuilder},
    user::User,
    wheel::Wheel,
    Discord, DiscordApp, Subscriptions,
};
use tokio::time::error::Elapsed;

#[derive(Debug, thiserror::Error)]
pub enum DiscordError {
    #[error(transparent)]
    Sdk(#[from] discord_sdk::Error),
    #[error(transparent)]
    AwaitConnection(#[from] tokio::sync::watch::error::RecvError),
    #[error("Could not connect")]
    Connection,
    #[error(transparent)]
    ConnectionTimeout(#[from] Elapsed),
}

/// The client wrapper for Discord RPC
pub struct DiscordRpcClient {
    pub discord: Discord,
    pub user: User,
    pub wheel: Wheel,
}

impl DiscordRpcClient {
    /// Creates a new `DiscordRpcClient`
    pub async fn new(app_id: i64, subscriptions: Subscriptions) -> Result<Self, DiscordError> {
        // Create a new wheel
        let (wheel, handler) = Wheel::new(Box::new(|err| {
            error!("Encountered an error: {}", err);
        }));
        let mut user = wheel.user();

        // Create the client
        let discord = Discord::new(
            DiscordApp::PlainId(app_id),
            subscriptions,
            Box::new(handler),
        )?;

        // Wait for the discord handshake
        info!("Waiting for Discord client handshake");
        user.0.changed().await?;
        info!("Discord handshake success");

        // Fetch the final user object
        let user = match &*user.0.borrow() {
            discord_sdk::wheel::UserState::Connected(u) => Ok(u.clone()),
            discord_sdk::wheel::UserState::Disconnected(_) => Err(DiscordError::Connection),
        }?;

        Ok(Self {
            discord,
            user,
            wheel,
        })
    }

    /// Clears the user rich presence
    #[profiling::function]
    #[allow(dead_code)]
    pub async fn clear_rich_presence(&self) -> Result<Option<Activity>, discord_sdk::Error> {
        self.discord
            .update_activity(ActivityBuilder::default())
            .await
    }

    /// Sets the user rich presence
    pub async fn set_rich_presence(
        &self,
        activity: ActivityBuilder,
    ) -> Result<Option<Activity>, discord_sdk::Error> {
        self.discord.update_activity(activity).await
    }
}