diff --git a/voxygen/src/discord.rs b/voxygen/src/discord.rs index c45d7dcba0..8d39a14198 100644 --- a/voxygen/src/discord.rs +++ b/voxygen/src/discord.rs @@ -1,6 +1,8 @@ use discord_rpc_sdk::{DiscordUser, EventHandlers, RichPresence, RPC}; use std::time::SystemTime; +use crate::DISCORD_INSTANCE; + use std::sync::mpsc::Sender; use std::sync::{mpsc, Mutex, MutexGuard}; use std::thread; @@ -33,8 +35,13 @@ pub fn run() -> Mutex { Mutex::new(DiscordState { tx, thread: Some(thread::spawn(move || { - let rpc = RPC::init::(DISCORD_APPLICATION_ID, true, None) - .expect("failed to initiate discord_game_sdk"); + let mut rpc: RPC = match RPC::init::(DISCORD_APPLICATION_ID, true, None) { + Ok(rpc) => rpc, + Err(e) => { + log::error!("failed to initiate discord_game_sdk: {}", e); + return; + } + }; //Set initial Status let mut current_presence = RichPresence { @@ -91,6 +98,8 @@ pub fn run() -> Mutex { Err(_) => {} } } + + rpc.clear_presence(); })), }) } @@ -103,7 +112,7 @@ impl EventHandlers for Handlers { } fn errored(errcode: i32, message: &str) { - log::debug!("Error {}: {}", errcode, message); + log::warn!("Error {}: {}", errcode, message); } fn disconnected(errcode: i32, message: &str) { @@ -123,13 +132,13 @@ impl EventHandlers for Handlers { } } -/* Some helpers */ -pub fn send_menu(disc: &mut MutexGuard) { - disc.tx.send(DiscordUpdate::Details("Menu".into())); - disc.tx.send(DiscordUpdate::State("Idling".into())); - disc.tx.send(DiscordUpdate::LargeImg("bg_main".into())); -} -pub fn send_singleplayer(disc: &mut MutexGuard) { - disc.tx.send(DiscordUpdate::Details("Singleplayer".into())); - disc.tx.send(DiscordUpdate::State("Playing...".into())); +pub fn send_all(updates: Vec) { + match DISCORD_INSTANCE.lock() { + Ok(mut disc) => { + for update in updates { + let _ = disc.tx.send(update); + } + } + Err(e) => log::error!("couldn't send Update to discord: {}", e), + } } diff --git a/voxygen/src/hud/esc_menu.rs b/voxygen/src/hud/esc_menu.rs index 8de3e8804d..cc45b395c6 100644 --- a/voxygen/src/hud/esc_menu.rs +++ b/voxygen/src/hud/esc_menu.rs @@ -6,7 +6,7 @@ use conrod_core::{ use super::{img_ids::Imgs, settings_window::SettingsTab, Fonts, TEXT_COLOR}; #[cfg(feature = "discord")] -use crate::{discord, discord_instance}; +use crate::{discord, discord::DiscordUpdate}; widget_ids! { struct Ids { @@ -155,10 +155,11 @@ impl<'a> Widget for EscMenu<'a> { { #[cfg(feature = "discord")] { - match discord_instance.lock() { - Ok(mut disc) => discord::send_menu(&mut disc), - Err(e) => log::error!("couldn't send Update to discord: {}", e), - } + discord::send_all(vec![ + DiscordUpdate::Details("Menu".into()), + DiscordUpdate::State("Idling".into()), + DiscordUpdate::LargeImg("bg_main".into()), + ]); } return Some(Event::Logout); diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs index 48f421224e..f9596fb865 100644 --- a/voxygen/src/hud/mod.rs +++ b/voxygen/src/hud/mod.rs @@ -42,7 +42,7 @@ use std::collections::VecDeque; use vek::*; #[cfg(feature = "discord")] -use crate::{discord, discord_instance}; +use crate::{discord, discord::DiscordUpdate}; const XP_COLOR: Color = Color::Rgba(0.59, 0.41, 0.67, 1.0); const TEXT_COLOR: Color = Color::Rgba(1.0, 1.0, 1.0, 1.0); @@ -689,10 +689,11 @@ impl Hud { #[cfg(feature = "discord")] { - match discord_instance.lock() { - Ok(mut disc) => discord::send_menu(&mut disc), - Err(e) => log::error!("couldn't send Update to discord: {}", e), - } + discord::send_all(vec![ + DiscordUpdate::Details("Menu".into()), + DiscordUpdate::State("Idling".into()), + DiscordUpdate::LargeImg("bg_main".into()), + ]); } } Some(esc_menu::Event::Quit) => events.push(Event::Quit), diff --git a/voxygen/src/main.rs b/voxygen/src/main.rs index e3ba673ff5..af7dcb03fa 100644 --- a/voxygen/src/main.rs +++ b/voxygen/src/main.rs @@ -89,7 +89,7 @@ pub trait PlayState { #[cfg(feature = "discord")] lazy_static! { //Set up discord rich presence - static ref discord_instance: Mutex = { + static ref DISCORD_INSTANCE: Mutex = { discord::run() }; } @@ -127,7 +127,7 @@ fn main() { // Initialize discord. (lazy_static initalise lazily...) #[cfg(feature = "discord")] { - match discord_instance.lock() { + match DISCORD_INSTANCE.lock() { Ok(disc) => { //great } @@ -267,12 +267,12 @@ fn main() { //Properly shutdown discord thread #[cfg(feature = "discord")] { - match discord_instance.lock() { + match DISCORD_INSTANCE.lock() { Ok(mut disc) => { - disc.tx.send(discord::DiscordUpdate::Shutdown); + let _ = disc.tx.send(discord::DiscordUpdate::Shutdown); match disc.thread.take() { Some(th) => { - th.join(); + let _ = th.join(); } None => { error!("couldn't gracefully shutdown discord thread"); diff --git a/voxygen/src/menu/main/client_init.rs b/voxygen/src/menu/main/client_init.rs index 17cc1aab4d..86ab1af88a 100644 --- a/voxygen/src/menu/main/client_init.rs +++ b/voxygen/src/menu/main/client_init.rs @@ -9,7 +9,7 @@ use std::{ }; #[cfg(feature = "discord")] -use crate::{discord, discord_instance}; +use crate::{discord, discord::DiscordUpdate}; #[derive(Debug)] pub enum Error { @@ -60,18 +60,11 @@ impl ClientInit { #[cfg(feature = "discord")] { - match discord_instance.lock() { - Ok(mut disc) => { - if !server_address.eq("127.0.0.1") { - discord::send_singleplayer(&mut disc); - disc.tx.send(discord::DiscordUpdate::Details( - server_address, - )); - } - } - Err(e) => { - log::error!("couldn't send Update to discord: {}", e) - } + if !server_address.eq("127.0.0.1") { + discord::send_all(vec![ + DiscordUpdate::Details(server_address), + DiscordUpdate::State("Playing...".into()), + ]); } } diff --git a/voxygen/src/singleplayer.rs b/voxygen/src/singleplayer.rs index adcca60b04..d0047fc6f4 100644 --- a/voxygen/src/singleplayer.rs +++ b/voxygen/src/singleplayer.rs @@ -11,7 +11,7 @@ use std::{ }; #[cfg(feature = "discord")] -use crate::{discord, discord_instance}; +use crate::{discord, discord::DiscordUpdate}; const TPS: u64 = 30; @@ -72,10 +72,10 @@ fn run_server(mut server: Server, rec: Receiver) { #[cfg(feature = "discord")] { - match discord_instance.lock() { - Ok(mut disc) => discord::send_singleplayer(&mut disc), - Err(e) => log::error!("couldn't send Update to discord: {}", e), - } + discord::send_all(vec![ + DiscordUpdate::Details("Singleplayer".into()), + DiscordUpdate::State("Playing...".into()), + ]); } loop {