Ejemplo n.º 1
0
 /// <summary>
 /// Disconnects from DiscordRPC and shuts down the bridge
 /// </summary>
 public void Shutdown()
 {
     _timer.Stop();
     DiscordRpc.Shutdown();
 }
Ejemplo n.º 2
0
        /// <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       = "Error connecting to iTunes",
                    state         = "Playback information unavailable"
                };
                DiscordRpc.UpdatePresence(newPresence);
                return;
            }
            catch (EntryPointNotFoundException) {
                var newPresence = new DiscordRpc.RichPresence {
                    largeImageKey = "itunes_logo_big",
                    details       = "No song playing",
                    state         = "Re-install iTunesRichPresence to clear this message"
                };
                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;
                    if (Settings.Default.DisplayPlaybackRemaining)
                    {
                        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}");
            }
        }