Skip to content

Commit

Permalink
check discord rpc connected, reconnect if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
AmyrAhmady committed Nov 11, 2023
1 parent 68ddeb6 commit 08be36b
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src-tauri/src/discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@ use tauri::async_runtime::block_on;

pub fn initialize_drpc() -> () {
std::thread::spawn(move || {
let mut connected = false;
let mut client = DiscordIpcClient::new("1057922416166305852").unwrap();
client.connect().unwrap();
match client.connect() {
Ok(_) => {
connected = true;
}
Err(_) => {
connected = false;
}
};

let timestamp: Timestamps = Timestamps::new();
let mut start_time: u64 = SystemTime::now()
.duration_since(UNIX_EPOCH)
Expand All @@ -23,9 +32,22 @@ pub fn initialize_drpc() -> () {
let mut ip: String = "Unknown".to_string();
let mut port: String = "Unknown".to_string();

let mut connected = false;
let mut in_game = false;

loop {
if !connected {
match client.reconnect() {
Ok(_) => {
connected = true;
}
Err(_) => {
connected = false;
}
};

std::thread::sleep(std::time::Duration::from_millis(1000));
continue;
}
let s = System::new_all();
let mut process_exists = false;

Expand Down Expand Up @@ -73,8 +95,8 @@ pub fn initialize_drpc() -> () {
}

if process_exists {
if !connected {
connected = true;
if !in_game {
in_game = true;
start_time = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
Expand All @@ -101,8 +123,8 @@ pub fn initialize_drpc() -> () {
.timestamps(timestamp.clone().start(start_time.try_into().unwrap()));
client.set_activity(activity).unwrap();
} else {
if connected {
connected = false;
if in_game {
in_game = false;
start_time = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
Expand Down

0 comments on commit 08be36b

Please sign in to comment.