pub struct PresenceRunner { /* private fields */ }Expand description
A runner that manages the Discord RPC background task. Create a runner, configure it, run it to get a client handle, then clone the handle for sharing.
Implementations§
Source§impl PresenceRunner
impl PresenceRunner
Sourcepub fn new(client_id: &str) -> Self
pub fn new(client_id: &str) -> Self
Create a new PresenceRunner instance. Requires the client ID of your chosen app from the
Discord Developer Portal.
Examples found in repository?
4async fn main() -> Result<(), PresenceError> {
5 let mut runner = PresenceRunner::new("1463450870480900160")
6 .show_errors()
7 .on_disconnect(|reason| {
8 eprintln!("discord rpc disconnected: {reason:?}");
9 });
10
11 runner.run(false).await?;
12 runner.wait().await?;
13
14 Ok(())
15}More examples
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}8async fn main() -> Result<(), PresenceError> {
9 let mut runner = PresenceRunner::new("1463450870480900160")
10 .on_ready(|data| println!("Connected to user: {}", data.user.username))
11 .on_activity_send(|data| {
12 println!("Activity sent to app: {} (running on {})\nMetadata: {}", data.name, data.platform, data.metadata)
13 })
14 .on_disconnect(|f| println!("Disconnected: {f:?}"))
15 .show_errors() // enables verbose error logging
16 ;
17
18 let client = runner.run(true).await?;
19
20 // the activity can include any combination of builder function calls
21 let activity = Activity::new()
22 .activity_type(ActivityType::Playing)
23 .details("epic game")
24 .details_url("https://github.com/hitblast")
25 .status_display_type(StatusDisplayType::Details)
26 .large_image("game_icon")
27 .large_text("Playing a game")
28 .large_url("https://hitblast.github.io/")
29 .small_image("status")
30 .small_text("Online")
31 .build();
32
33 client.set_activity(activity).await?;
34
35 // indefinitely block here
36 runner.wait().await?;
37
38 Ok(())
39}13async fn main() -> Result<(), PresenceError> {
14 // simple atomic counter
15 let count = Arc::new(AtomicU8::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}Sourcepub fn on_ready<F: Fn(ReadyData) + Send + Sync + 'static>(self, f: F) -> Self
pub fn on_ready<F: Fn(ReadyData) + Send + Sync + 'static>(self, f: F) -> Self
Run a particular closure after receiving the READY event from the Discord RPC server.
This event can fire multiple times depending on how many times the client needs to reconnect with Discord RPC.
Examples found in repository?
8async fn main() -> Result<(), PresenceError> {
9 let mut runner = PresenceRunner::new("1463450870480900160")
10 .on_ready(|data| println!("Connected to user: {}", data.user.username))
11 .on_activity_send(|data| {
12 println!("Activity sent to app: {} (running on {})\nMetadata: {}", data.name, data.platform, data.metadata)
13 })
14 .on_disconnect(|f| println!("Disconnected: {f:?}"))
15 .show_errors() // enables verbose error logging
16 ;
17
18 let client = runner.run(true).await?;
19
20 // the activity can include any combination of builder function calls
21 let activity = Activity::new()
22 .activity_type(ActivityType::Playing)
23 .details("epic game")
24 .details_url("https://github.com/hitblast")
25 .status_display_type(StatusDisplayType::Details)
26 .large_image("game_icon")
27 .large_text("Playing a game")
28 .large_url("https://hitblast.github.io/")
29 .small_image("status")
30 .small_text("Online")
31 .build();
32
33 client.set_activity(activity).await?;
34
35 // indefinitely block here
36 runner.wait().await?;
37
38 Ok(())
39}More examples
13async fn main() -> Result<(), PresenceError> {
14 // simple atomic counter
15 let count = Arc::new(AtomicU8::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}Sourcepub fn on_activity_send<F: Fn(ActivityResponseData) + Send + Sync + 'static>(
self,
f: F,
) -> Self
pub fn on_activity_send<F: Fn(ActivityResponseData) + Send + Sync + 'static>( self, f: F, ) -> Self
Run a particular closure after ensuring that a PresenceClient::set_activity has successfully managed to
pass its data through the IPC channel.
This event can fire multiple times based on how many activities you set.
Examples found in repository?
8async fn main() -> Result<(), PresenceError> {
9 let mut runner = PresenceRunner::new("1463450870480900160")
10 .on_ready(|data| println!("Connected to user: {}", data.user.username))
11 .on_activity_send(|data| {
12 println!("Activity sent to app: {} (running on {})\nMetadata: {}", data.name, data.platform, data.metadata)
13 })
14 .on_disconnect(|f| println!("Disconnected: {f:?}"))
15 .show_errors() // enables verbose error logging
16 ;
17
18 let client = runner.run(true).await?;
19
20 // the activity can include any combination of builder function calls
21 let activity = Activity::new()
22 .activity_type(ActivityType::Playing)
23 .details("epic game")
24 .details_url("https://github.com/hitblast")
25 .status_display_type(StatusDisplayType::Details)
26 .large_image("game_icon")
27 .large_text("Playing a game")
28 .large_url("https://hitblast.github.io/")
29 .small_image("status")
30 .small_text("Online")
31 .build();
32
33 client.set_activity(activity).await?;
34
35 // indefinitely block here
36 runner.wait().await?;
37
38 Ok(())
39}More examples
13async fn main() -> Result<(), PresenceError> {
14 // simple atomic counter
15 let count = Arc::new(AtomicU8::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}Sourcepub fn on_disconnect<F: Fn(DisconnectReason) + Send + Sync + 'static>(
self,
f: F,
) -> Self
pub fn on_disconnect<F: Fn(DisconnectReason) + Send + Sync + 'static>( self, f: F, ) -> Self
Run a particular closure after the RPC connection is lost.
This can fire multiple times if the client reconnects and disconnects again.
Examples found in repository?
4async fn main() -> Result<(), PresenceError> {
5 let mut runner = PresenceRunner::new("1463450870480900160")
6 .show_errors()
7 .on_disconnect(|reason| {
8 eprintln!("discord rpc disconnected: {reason:?}");
9 });
10
11 runner.run(false).await?;
12 runner.wait().await?;
13
14 Ok(())
15}More examples
8async fn main() -> Result<(), PresenceError> {
9 let mut runner = PresenceRunner::new("1463450870480900160")
10 .on_ready(|data| println!("Connected to user: {}", data.user.username))
11 .on_activity_send(|data| {
12 println!("Activity sent to app: {} (running on {})\nMetadata: {}", data.name, data.platform, data.metadata)
13 })
14 .on_disconnect(|f| println!("Disconnected: {f:?}"))
15 .show_errors() // enables verbose error logging
16 ;
17
18 let client = runner.run(true).await?;
19
20 // the activity can include any combination of builder function calls
21 let activity = Activity::new()
22 .activity_type(ActivityType::Playing)
23 .details("epic game")
24 .details_url("https://github.com/hitblast")
25 .status_display_type(StatusDisplayType::Details)
26 .large_image("game_icon")
27 .large_text("Playing a game")
28 .large_url("https://hitblast.github.io/")
29 .small_image("status")
30 .small_text("Online")
31 .build();
32
33 client.set_activity(activity).await?;
34
35 // indefinitely block here
36 runner.wait().await?;
37
38 Ok(())
39}Sourcepub fn show_errors(self) -> Self
pub fn show_errors(self) -> Self
Enable verbose error logging for RPC and code events.
Examples found in repository?
4async fn main() -> Result<(), PresenceError> {
5 let mut runner = PresenceRunner::new("1463450870480900160")
6 .show_errors()
7 .on_disconnect(|reason| {
8 eprintln!("discord rpc disconnected: {reason:?}");
9 });
10
11 runner.run(false).await?;
12 runner.wait().await?;
13
14 Ok(())
15}More examples
8async fn main() -> Result<(), PresenceError> {
9 let mut runner = PresenceRunner::new("1463450870480900160")
10 .on_ready(|data| println!("Connected to user: {}", data.user.username))
11 .on_activity_send(|data| {
12 println!("Activity sent to app: {} (running on {})\nMetadata: {}", data.name, data.platform, data.metadata)
13 })
14 .on_disconnect(|f| println!("Disconnected: {f:?}"))
15 .show_errors() // enables verbose error logging
16 ;
17
18 let client = runner.run(true).await?;
19
20 // the activity can include any combination of builder function calls
21 let activity = Activity::new()
22 .activity_type(ActivityType::Playing)
23 .details("epic game")
24 .details_url("https://github.com/hitblast")
25 .status_display_type(StatusDisplayType::Details)
26 .large_image("game_icon")
27 .large_text("Playing a game")
28 .large_url("https://hitblast.github.io/")
29 .small_image("status")
30 .small_text("Online")
31 .build();
32
33 client.set_activity(activity).await?;
34
35 // indefinitely block here
36 runner.wait().await?;
37
38 Ok(())
39}13async fn main() -> Result<(), PresenceError> {
14 // simple atomic counter
15 let count = Arc::new(AtomicU8::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}Sourcepub async fn run(
&mut self,
wait_for_ready: bool,
) -> Result<&PresenceClient, PresenceRunnerError>
pub async fn run( &mut self, wait_for_ready: bool, ) -> Result<&PresenceClient, PresenceRunnerError>
Run the runner. Must be called before any client handle operations.
Examples found in repository?
4async fn main() -> Result<(), PresenceError> {
5 let mut runner = PresenceRunner::new("1463450870480900160")
6 .show_errors()
7 .on_disconnect(|reason| {
8 eprintln!("discord rpc disconnected: {reason:?}");
9 });
10
11 runner.run(false).await?;
12 runner.wait().await?;
13
14 Ok(())
15}More examples
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}8async fn main() -> Result<(), PresenceError> {
9 let mut runner = PresenceRunner::new("1463450870480900160")
10 .on_ready(|data| println!("Connected to user: {}", data.user.username))
11 .on_activity_send(|data| {
12 println!("Activity sent to app: {} (running on {})\nMetadata: {}", data.name, data.platform, data.metadata)
13 })
14 .on_disconnect(|f| println!("Disconnected: {f:?}"))
15 .show_errors() // enables verbose error logging
16 ;
17
18 let client = runner.run(true).await?;
19
20 // the activity can include any combination of builder function calls
21 let activity = Activity::new()
22 .activity_type(ActivityType::Playing)
23 .details("epic game")
24 .details_url("https://github.com/hitblast")
25 .status_display_type(StatusDisplayType::Details)
26 .large_image("game_icon")
27 .large_text("Playing a game")
28 .large_url("https://hitblast.github.io/")
29 .small_image("status")
30 .small_text("Online")
31 .build();
32
33 client.set_activity(activity).await?;
34
35 // indefinitely block here
36 runner.wait().await?;
37
38 Ok(())
39}13async fn main() -> Result<(), PresenceError> {
14 // simple atomic counter
15 let count = Arc::new(AtomicU8::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}Sourcepub fn clone_handle(&self) -> PresenceClient
pub fn clone_handle(&self) -> PresenceClient
Returns a clone of the client handle for sharing.
Sourcepub async fn wait(&mut self) -> Result<(), PresenceRunnerError>
pub async fn wait(&mut self) -> Result<(), PresenceRunnerError>
Waits for the IPC task to finish.
NOTE: If there’s no join_handle present, the function will do nothing and
just return blank.
Examples found in repository?
4async fn main() -> Result<(), PresenceError> {
5 let mut runner = PresenceRunner::new("1463450870480900160")
6 .show_errors()
7 .on_disconnect(|reason| {
8 eprintln!("discord rpc disconnected: {reason:?}");
9 });
10
11 runner.run(false).await?;
12 runner.wait().await?;
13
14 Ok(())
15}More examples
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}8async fn main() -> Result<(), PresenceError> {
9 let mut runner = PresenceRunner::new("1463450870480900160")
10 .on_ready(|data| println!("Connected to user: {}", data.user.username))
11 .on_activity_send(|data| {
12 println!("Activity sent to app: {} (running on {})\nMetadata: {}", data.name, data.platform, data.metadata)
13 })
14 .on_disconnect(|f| println!("Disconnected: {f:?}"))
15 .show_errors() // enables verbose error logging
16 ;
17
18 let client = runner.run(true).await?;
19
20 // the activity can include any combination of builder function calls
21 let activity = Activity::new()
22 .activity_type(ActivityType::Playing)
23 .details("epic game")
24 .details_url("https://github.com/hitblast")
25 .status_display_type(StatusDisplayType::Details)
26 .large_image("game_icon")
27 .large_text("Playing a game")
28 .large_url("https://hitblast.github.io/")
29 .small_image("status")
30 .small_text("Online")
31 .build();
32
33 client.set_activity(activity).await?;
34
35 // indefinitely block here
36 runner.wait().await?;
37
38 Ok(())
39}