diff --git a/Cargo.lock b/Cargo.lock index 9cda793..77e9d49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -239,9 +239,9 @@ dependencies = [ [[package]] name = "discord-presence" -version = "0.5.18" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8df544b673db7487833c561a60f1a5c9df8c7ee470277534a813babbbeb9a697" +checksum = "5b0b42c644ad2150387ded023bedd258052306d67a43d40e36678491c59dec0c" dependencies = [ "byteorder", "bytes", @@ -251,6 +251,7 @@ dependencies = [ "num-derive", "num-traits", "parking_lot", + "paste", "quork", "serde", "serde_json", @@ -838,6 +839,12 @@ dependencies = [ "windows-targets 0.48.5", ] +[[package]] +name = "paste" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" + [[package]] name = "percent-encoding" version = "2.3.1" @@ -907,9 +914,9 @@ dependencies = [ [[package]] name = "quork" -version = "0.4.1" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9663278eaaba2b6bda2410ac40f6a3117d952dbb84938b34133d23ffda3871d" +checksum = "1015eb796ce468e7b5434f9d1b309036a80f1afbadfa224b27f423762c8e5893" dependencies = [ "cc", "cfg-if", @@ -1505,7 +1512,7 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "vrc-log" -version = "0.5.0" +version = "0.5.1" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 6ebcd10..1f810fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "vrc-log" -version = "0.5.0" +version = "0.5.1" authors = ["Shayne Hartford "] edition = "2021" description = "VRChat Local Cache Avatar ID Logger" @@ -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" diff --git a/src/discord.rs b/src/discord.rs index f8cc225..e48e2bf 100644 --- a/src/discord.rs +++ b/src/discord.rs @@ -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 = { - let user = Arc::new(RwLock::new(None)); - let user_clone = user.clone(); - + pub static ref USER: Option = { + 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::(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(); diff --git a/src/provider/vrcdb.rs b/src/provider/vrcdb.rs index 87cd363..c9d803c 100644 --- a/src/provider/vrcdb.rs +++ b/src/provider/vrcdb.rs @@ -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}, }; @@ -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) }