/// <summary> /// Disconnects from DiscordRPC and shuts down the bridge /// </summary> public void Shutdown() { _timer.Stop(); DiscordRpc.Shutdown(); }
/// <summary> /// Handles checking for playing status changes and pushing out presence updates /// </summary> /// <param name="sender">Sender of this event</param> /// <param name="e">Args of this event</param> private void Timer_OnTick(object sender, EventArgs e) { try { if (ITunes == null) { ITunes = new iTunesApp(); } if (ITunes.CurrentTrack == null || (Settings.Default.ClearOnPause && ITunes.PlayerState != ITPlayerState.ITPlayerStatePlaying)) { DiscordRpc.ClearPresence(); return; } } catch (COMException) { ITunes = null; var newPresence = new DiscordRpc.RichPresence { largeImageKey = "itunes_logo_big", details = "iTunes 연결 오류", state = "재생 정보 없음" }; DiscordRpc.UpdatePresence(newPresence); return; } catch (EntryPointNotFoundException) { var newPresence = new DiscordRpc.RichPresence { largeImageKey = "itunes_logo_big", details = "재생중인 음악 없음", state = "iTunesRichPresence를 재설치해주세요" }; DiscordRpc.UpdatePresence(newPresence); return; } if (_currentArtist == ITunes.CurrentTrack.Artist && _currentTitle == ITunes.CurrentTrack.Name && _currentState == ITunes.PlayerState && _currentPosition == ITunes.PlayerPosition) { return; } _currentArtist = ITunes.CurrentTrack.Artist; _currentTitle = ITunes.CurrentTrack.Name; _currentState = ITunes.PlayerState; _currentPosition = ITunes.PlayerPosition; var presence = new DiscordRpc.RichPresence { largeImageKey = "itunes_logo_big" }; if (_currentState != ITPlayerState.ITPlayerStatePlaying) { presence.details = TruncateString(RenderString(Settings.Default.PausedTopLine)); presence.state = TruncateString(RenderString(Settings.Default.PausedBottomLine)); } else { presence.details = TruncateString(RenderString(Settings.Default.PlayingTopLine)); presence.state = TruncateString(RenderString(Settings.Default.PlayingBottomLine)); if (Settings.Default.DisplayPlaybackDuration) { presence.startTimestamp = DateTimeOffset.Now.ToUnixTimeSeconds() - _currentPosition; presence.endTimestamp = DateTimeOffset.Now.ToUnixTimeSeconds() + (ITunes.CurrentTrack.Duration - _currentPosition); } } try { DiscordRpc.UpdatePresence(presence); } catch (Exception exception) { exception.Data.Add("CurrentArtist", _currentArtist); exception.Data.Add("CurrentTitle", _currentTitle); exception.Data.Add("CurrentState", _currentState.ToString()); exception.Data.Add("CurrentPosition", _currentPosition.ToString()); exception.Data.Add("Details", presence.details); exception.Data.Add("State", presence.state); Globals.RavenClient.Capture(new SentryEvent(exception)); Globals.Log($"An unhandled exception has occurred and been reported to Sentry: {exception.Message}"); } }