Skip to main content

timed/
timed.rs

1use anyhow::Result;
2use std::{
3    sync::{
4        Arc,
5        atomic::{AtomicU8, Ordering},
6    },
7    time::Duration,
8};
9
10use filthy_rich::{PresenceRunner, types::Activity};
11use tokio::time::sleep;
12
13#[tokio::main]
14async fn main() -> Result<()> {
15    // simple atomic counter
16    let count = Arc::new(AtomicU8::new(0));
17
18    let mut runner = PresenceRunner::new("1463450870480900160")
19        .on_ready(|data| println!("Connected to user: {}", data.user.username))
20        .on_activity_send(move |_| {
21
22            // increments the counter with every send_activity()
23            let val = count.fetch_add(1, Ordering::Relaxed) + 1;
24
25            println!("Activity {val} sent successfully.")
26
27        })
28        .show_errors() // enables verbose error logging
29    ;
30
31    // create activities for later use
32    let activity_1 = Activity::new()
33        .details("this runs")
34        .state("for ten seconds")
35        .build();
36
37    let activity_2 = Activity::new()
38        .details("believe it")
39        .state("or not")
40        .build();
41
42    let closing_activity = Activity::new()
43        .details("closing presence in...")
44        .duration(Duration::from_secs(5))
45        .small_image("status")
46        .build();
47
48    // first run
49    let client = runner.run(true).await?;
50
51    client.set_activity(activity_1).await?;
52    sleep(Duration::from_secs(5)).await;
53    client.set_activity(activity_2).await?;
54    sleep(Duration::from_secs(5)).await;
55    client.set_activity(closing_activity).await?;
56    sleep(Duration::from_secs(5)).await;
57
58    client.close().await?;
59
60    println!("Stopped.");
61
62    Ok(())
63}