Skip to content

Commit

Permalink
0.5.1 - Update RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
ShayBox committed Mar 13, 2024
1 parent ca9e388 commit f10aacd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 52 deletions.
17 changes: 12 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "vrc-log"
version = "0.5.0"
version = "0.5.1"
authors = ["Shayne Hartford <shaybox@shaybox.com>"]
edition = "2021"
description = "VRChat Local Cache Avatar ID Logger"
Expand All @@ -18,7 +18,7 @@ chrono = "0.4.32"
colored = "2"
crossbeam = "0.8"
crossterm = { version = "0.27", optional = true }
discord-presence = { version = "0.5", optional = true }
discord-presence = { version = "1", optional = true }
indexmap = "2"
lazy_static = "1"
notify = "6"
Expand Down
43 changes: 15 additions & 28 deletions src/discord.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,31 @@
use std::{sync::Arc, time::Duration};

use discord_presence::Client;
use discord_presence::{
models::{EventData, PartialUser},
Client,
};
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};

pub const CLIENT_ID: u64 = 1_137_885_877_918_502_923;
pub const DEVELOPER_ID: &str = "358558305997684739";

#[derive(Clone, Deserialize, Serialize)]
pub struct Event {
pub user: User,
}

#[derive(Clone, Deserialize, Serialize)]
pub struct User {
pub id: String,
#[serde(rename = "username")]
pub name: String,
#[serde(rename = "global_name")]
pub nick: String,
}

lazy_static::lazy_static! {
pub static ref USER: Option<User> = {
let user = Arc::new(RwLock::new(None));
let user_clone = user.clone();

pub static ref USER: Option<PartialUser> = {
let user_event = Arc::new(RwLock::new(None));
let user_clone = user_event.clone();
let mut client = Client::new(CLIENT_ID);
// client.on_error(|ctx| eprintln!("{ctx:#?}")); // discord-presence v0.5.18 constantly emits errors...
client.on_ready(move |ctx| {
let Ok(event) = serde_json::from_value::<Event>(ctx.event) else {
return;
let _ = client.on_ready(move |ctx| {
if let EventData::Ready(event) = ctx.event {
*user_event.write() = event.user;
};

*user.write() = Some(event.user);
});

let thread = client.start();
client.start();

// block_until_event will never timeout
std::thread::sleep(Duration::from_secs(5));
thread.stop().expect("Failed to stop RPC thread");

client.shutdown().expect("Failed to stop RPC thread");

let user = user_clone.read();

Expand Down
34 changes: 17 additions & 17 deletions src/provider/vrcdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::{bail, Result};
use reqwest::blocking::Client;

use crate::{
discord::{User, DEVELOPER_ID, USER},
discord::{DEVELOPER_ID, USER},
provider::{Provider, Type},
};

Expand All @@ -24,28 +24,28 @@ impl VRCDB {
pub const fn new(client: Client, userid: String) -> Self {
Self { client, userid }
}

fn default() -> String {
eprintln!("Error: Discord RPC Connection Failed\n");
eprintln!("This may be due to one of the following reasons:");
eprintln!("1. Discord is not running on your system.");
eprintln!("2. VRC-LOG was restarted too quickly.\n");
eprintln!("The User ID will default to the developer: ShayBox");

DEVELOPER_ID.to_owned()
}
}

impl Default for VRCDB {
fn default() -> Self {
let client = Client::default();
let userid = USER.clone().map_or_else(
|| {
eprintln!("Error: Discord RPC Connection Failed\n");
eprintln!("This may be due to one of the following reasons:");
eprintln!("1. Discord is not running on your system.");
eprintln!("2. VRC-LOG was restarted too quickly.\n");
eprintln!("The User ID will default to the developer: ShayBox");

DEVELOPER_ID.to_owned()
},
|user| {
let User { id, name, nick } = user;
println!("{} Authenticated as {nick} ({name})", Type::VRCDB);
let userid = USER.clone().map_or_else(Self::default, |user| {
if let Some(username) = user.username {
println!("{} Authenticated as {username}", Type::VRCDB);
}

id
},
);
user.id.unwrap_or_else(Self::default)
});

Self::new(client, userid)
}
Expand Down

0 comments on commit f10aacd

Please sign in to comment.