Skip to main content

timed/
timed.rs

1use std::{
2    sync::{
3        Arc,
4        atomic::{AtomicUsize, Ordering},
5    },
6    time::Duration,
7};
8
9use filthy_rich::{PresenceRunner, errors::PresenceError, types::Activity};
10use tokio::time::sleep;
11
12#[tokio::main]
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}