This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
lib.rs
109 lines (91 loc) · 3.02 KB
/
lib.rs
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#![warn(missing_docs)]
//! A Bevy plugin that allows the developer to interact with the Discord Presence API with ease
//!
//! This plugin is a Bevy wrapper around the [Discord Presence](https://docs.rs/crate/discord-presence) crate which in turn is a wrapper around the [Discord Presence API](https://discordapp.com/developers/docs/game-sdk/discord-presence).
//! # Examples
//!
//! ```rust no_run
//! use bevy::prelude::*;
//! use bevy_discord_presence::{state::ActivityState, config::{RPCConfig, RPCPlugin}};
//!
//! fn main() {
//! println!("hello world!");
//! let mut app = App::new();
//! app.add_plugins(DefaultPlugins);
//! app.add_plugin(RPCPlugin(RPCConfig {
//! app_id: 425407036495495169,
//! show_time: true,
//! }));
//! app.add_system(update_presence);
//!
//! app.run();
//! }
//!
//! fn update_presence(mut state: ResMut<ActivityState>) {
//! state.details = Some("Hello World".to_string());
//! }
//! ```
use std::time::{SystemTime, UNIX_EPOCH};
use bevy::{log::prelude::*, prelude::*};
use discord_presence::{models::ActivityTimestamps, Client, Event};
/// The Discord configuration
mod config;
/// The state that holds the Discord activity
mod state;
pub use config::{RPCConfig, RPCPlugin};
pub use state::ActivityState;
/// Implements the Bevy plugin trait
impl Plugin for RPCPlugin {
fn build(&self, app: &mut App) {
let client_config = self.config;
app.add_startup_system(startup_client);
app.add_system(check_activity_changed);
debug!("Added systems");
app.insert_resource::<RPCConfig>(client_config);
app.init_resource::<ActivityState>();
app.insert_resource::<Client>(Client::new(client_config.app_id));
debug!("Initialized resources");
}
fn name(&self) -> &str {
"Discord Presence"
}
}
/// Initializes the client and starts it running
fn startup_client(
mut activity: ResMut<ActivityState>,
mut client: ResMut<Client>,
config: Res<RPCConfig>,
) {
use strum::IntoEnumIterator;
if config.show_time {
activity.timestamps = Some(ActivityTimestamps {
start: Some(
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time has gone backwards")
.as_secs(),
),
end: None,
});
}
for event in Event::iter() {
client.on_event(event, {
let events = activity.events.clone();
move |_| {
events.lock().0.push_back(event);
debug!("Added event: {:?}", event);
}
});
}
_ = client.start();
debug!("Client has started");
}
/// Runs whenever the activity has been changed, and at startup
fn check_activity_changed(activity: Res<ActivityState>, mut client: ResMut<Client>) {
if activity.is_changed() {
let res = client.set_activity(|_| activity.clone().into());
if let Err(why) = res {
error!("Failed to set presence: {}", why);
}
}
}