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
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),
}
pub struct DiscordRpcClient {
pub discord: Discord,
pub user: User,
pub wheel: Wheel,
}
impl DiscordRpcClient {
pub async fn new(app_id: i64, subscriptions: Subscriptions) -> Result<Self, DiscordError> {
let (wheel, handler) = Wheel::new(Box::new(|err| {
error!("Encountered an error: {}", err);
}));
let mut user = wheel.user();
let discord = Discord::new(
DiscordApp::PlainId(app_id),
subscriptions,
Box::new(handler),
)?;
info!("Waiting for Discord client handshake");
user.0.changed().await?;
info!("Discord handshake success");
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,
})
}
#[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
}
pub async fn set_rich_presence(
&self,
activity: ActivityBuilder,
) -> Result<Option<Activity>, discord_sdk::Error> {
self.discord.update_activity(activity).await
}
}