pub struct PresenceClient { /* private fields */ }Expand description
A client handle for communicating with super::PresenceRunner and its inner loop.
Implementations§
Source§impl PresenceClient
impl PresenceClient
Sourcepub async fn set_activity(
&self,
activity: ActivitySpec,
) -> Result<(), PresenceClientError>
pub async fn set_activity( &self, activity: ActivitySpec, ) -> Result<(), PresenceClientError>
Sets/updates the Discord Rich presence activity. The runner must be started before calling this.
NOTE: This will NOT wait for the activity to finish becoming online.
Examples found in repository?
examples/simple.rs (line 14)
4async fn main() -> Result<(), PresenceError> {
5 let mut runner = PresenceRunner::new("1463450870480900160");
6
7 let activity = Activity::new()
8 .name("cool app name")
9 .details("Something?")
10 .state("Probably~")
11 .build()?;
12
13 let client = runner.run(true).await?;
14 client.set_activity(activity).await?;
15
16 // indefinitely block here
17 runner.wait().await?;
18
19 Ok(())
20}More examples
examples/timed.rs (line 50)
13async fn main() -> Result<(), PresenceError> {
14 // simple atomic counter
15 let count = Arc::new(AtomicUsize::new(0));
16
17 let mut runner = PresenceRunner::new("1463450870480900160")
18 .on_ready(|data| println!("Connected to user: {}", data.user.username()))
19 .on_activity_send(move |_| {
20
21 // increments the counter with every send_activity()
22 let val = count.fetch_add(1, Ordering::Relaxed) + 1;
23
24 println!("Activity {val} sent successfully.")
25
26 })
27 .show_errors() // enables verbose error logging
28 ;
29
30 // create activities for later use
31 let activity_1 = Activity::new()
32 .details("this runs")
33 .state("for ten seconds")
34 .build()?;
35
36 let activity_2 = Activity::new()
37 .details("believe it")
38 .state("or not")
39 .build()?;
40
41 let closing_activity = Activity::new()
42 .details("closing presence in...")
43 .duration(Duration::from_secs(5))
44 .small_image("status")
45 .build()?;
46
47 // first run
48 let client = runner.run(true).await?;
49
50 client.set_activity(activity_1).await?;
51 sleep(Duration::from_secs(5)).await;
52 client.set_activity(activity_2).await?;
53 sleep(Duration::from_secs(5)).await;
54 client.set_activity(closing_activity).await?;
55 sleep(Duration::from_secs(5)).await;
56
57 client.close().await?;
58
59 println!("Stopped.");
60
61 Ok(())
62}examples/indefinite.rs (line 47)
8async fn main() -> Result<(), PresenceError> {
9 let mut runner = PresenceRunner::new("1463450870480900160")
10 .on_ready(|data| {
11 println!(
12 "RPC version: v{}; Connected to user: {}",
13 data.version(),
14 data.user.global_name().unwrap_or_default(),
15 )
16 })
17 .on_activity_send(|data| {
18 println!(
19 "Activity sent to app! (running on {})\nCreated at: {}",
20 data.platform().unwrap_or_default(),
21 data.activity.created_at()
22 );
23 })
24 .on_disconnect(|f| println!("Disconnected: {f:?}"))
25 .show_errors() // enables verbose error logging
26 .on_retry(move |c| {
27 if c % 10 == 0 {
28 println!("Retry count {c}; is Discord open?");
29 }
30 });
31
32 let client = runner.run(true).await?;
33
34 // the activity can include any combination of builder function calls
35 let activity = Activity::new()
36 .activity_type(ActivityType::Playing)
37 .details("epic game")
38 .details_url("https://github.com/hitblast")
39 .status_display_type(StatusDisplayType::Details)
40 .large_image("game_icon")
41 .large_text("Playing a game")
42 .large_url("https://hitblast.github.io/")
43 .small_image("status")
44 .small_text("Online")
45 .build()?;
46
47 client.set_activity(activity).await?;
48
49 // indefinitely block here
50 runner.wait().await?;
51
52 Ok(())
53}Sourcepub async fn clear_activity(&self) -> Result<(), PresenceClientError>
pub async fn clear_activity(&self) -> Result<(), PresenceClientError>
Clears a previously set Discord Rich Presence activity.
NOTE: This will NOT wait for the activity to finish clearing.
Sourcepub async fn close(&self) -> Result<(), PresenceClientError>
pub async fn close(&self) -> Result<(), PresenceClientError>
Closes the current connection if any.
This is a semi-blocking call and does wait for the runner thread to respond to the signal being sent.
Examples found in repository?
examples/timed.rs (line 57)
13async fn main() -> Result<(), PresenceError> {
14 // simple atomic counter
15 let count = Arc::new(AtomicUsize::new(0));
16
17 let mut runner = PresenceRunner::new("1463450870480900160")
18 .on_ready(|data| println!("Connected to user: {}", data.user.username()))
19 .on_activity_send(move |_| {
20
21 // increments the counter with every send_activity()
22 let val = count.fetch_add(1, Ordering::Relaxed) + 1;
23
24 println!("Activity {val} sent successfully.")
25
26 })
27 .show_errors() // enables verbose error logging
28 ;
29
30 // create activities for later use
31 let activity_1 = Activity::new()
32 .details("this runs")
33 .state("for ten seconds")
34 .build()?;
35
36 let activity_2 = Activity::new()
37 .details("believe it")
38 .state("or not")
39 .build()?;
40
41 let closing_activity = Activity::new()
42 .details("closing presence in...")
43 .duration(Duration::from_secs(5))
44 .small_image("status")
45 .build()?;
46
47 // first run
48 let client = runner.run(true).await?;
49
50 client.set_activity(activity_1).await?;
51 sleep(Duration::from_secs(5)).await;
52 client.set_activity(activity_2).await?;
53 sleep(Duration::from_secs(5)).await;
54 client.set_activity(closing_activity).await?;
55 sleep(Duration::from_secs(5)).await;
56
57 client.close().await?;
58
59 println!("Stopped.");
60
61 Ok(())
62}Trait Implementations§
Source§impl Clone for PresenceClient
impl Clone for PresenceClient
Source§fn clone(&self) -> PresenceClient
fn clone(&self) -> PresenceClient
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for PresenceClient
impl RefUnwindSafe for PresenceClient
impl Send for PresenceClient
impl Sync for PresenceClient
impl Unpin for PresenceClient
impl UnsafeUnpin for PresenceClient
impl UnwindSafe for PresenceClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more