Пример #1
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)
                {
                    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;
            }


            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}");
            }
        }