예제 #1
0
 private async void UpdateSafe(DiscordConnection connection, DiscordRichPresence presence)
 {
     try {
         await connection.UpdateAsync(presence, _overrideProcessId ?? _processId);
     } catch (Exception e) {
         Utils.Warn(e.ToString());
     }
 }
예제 #2
0
        public IDisposable SetAppId(int appId)
        {
            var oldValue = _overrideProcessId;

            _overrideProcessId = appId;
            return(new ActionAsDisposable(async() => {
                if (_currentConnection != null && _currentPresence != null)
                {
                    try {
                        await _currentConnection.UpdateAsync(_currentPresence, appId);
                    } catch (Exception e) {
                        Utils.Warn(e.ToString());
                    }
                }
                _overrideProcessId = oldValue;
            }));
        }
예제 #3
0
        private async Task RunAsync()
        {
            var delay = OptionMinReconnectionDelay;

            while (!IsDisposed)
            {
                try {
                    Utils.Log("(Re)creating connection…");
                    using (var connection = new DiscordConnection(_handler)) {
                        await connection.LaunchAsync(_clientId).ConfigureAwait(false);

                        delay = OptionMinReconnectionDelay;

                        _currentConnection = connection;
                        if (_currentPresence != null)
                        {
                            await connection.UpdateAsync(_currentPresence, _overrideProcessId ?? _processId).ConfigureAwait(false);
                        }

                        await connection.ListenAsync().ConfigureAwait(false);

                        if (connection.IsDisposed)
                        {
                            continue;
                        }
                    }
                } catch (TimeoutException e) {
                    Utils.Log(e.Message);
                } catch (IOException e) {
                    Utils.Warn(e.Message);
                } catch (DiscordException e) {
                    Utils.Warn(e.Message);
                } catch (Exception e) {
                    Utils.Warn(e.ToString());
                }

                _currentConnection = null;
                using (_currentDelay = new CancellationTokenSource()) {
                    await Task.Delay(delay, _currentDelay.Token).ConfigureAwait(false);
                }

                _currentDelay = null;
                delay         = TimeSpan.FromSeconds(Math.Min(delay.TotalSeconds * 2, OptionMaxReconnectionDelay.TotalSeconds));
            }
        }