| @@ -39,7 +39,6 @@ namespace Discord | |||||
| private bool _isFirstLogSub; | private bool _isFirstLogSub; | ||||
| private bool _isReconnecting; | private bool _isReconnecting; | ||||
| private bool _isDisposed; | private bool _isDisposed; | ||||
| private string[] _scopes; | |||||
| public API.DiscordRpcApiClient ApiClient { get; } | public API.DiscordRpcApiClient ApiClient { get; } | ||||
| internal LogManager LogManager { get; } | internal LogManager LogManager { get; } | ||||
| @@ -150,33 +149,21 @@ namespace Discord | |||||
| await _loggedOutEvent.InvokeAsync().ConfigureAwait(false); | await _loggedOutEvent.InvokeAsync().ConfigureAwait(false); | ||||
| } | } | ||||
| /// <inheritdoc /> | |||||
| public async Task ConnectAsync() | public async Task ConnectAsync() | ||||
| { | { | ||||
| await _connectionLock.WaitAsync().ConfigureAwait(false); | await _connectionLock.WaitAsync().ConfigureAwait(false); | ||||
| try | try | ||||
| { | { | ||||
| _isReconnecting = false; | _isReconnecting = false; | ||||
| await ConnectInternalAsync(null).ConfigureAwait(false); | |||||
| await ConnectInternalAsync().ConfigureAwait(false); | |||||
| } | } | ||||
| finally { _connectionLock.Release(); } | finally { _connectionLock.Release(); } | ||||
| } | } | ||||
| public async Task ConnectAndAuthorizeAsync(params string[] scopes) | |||||
| private async Task ConnectInternalAsync(bool ignoreLoginCheck = false) | |||||
| { | { | ||||
| await _connectionLock.WaitAsync().ConfigureAwait(false); | |||||
| try | |||||
| { | |||||
| _isReconnecting = false; | |||||
| await ConnectInternalAsync(scopes).ConfigureAwait(false); | |||||
| } | |||||
| finally { _connectionLock.Release(); } | |||||
| } | |||||
| private async Task ConnectInternalAsync(string[] scopes) | |||||
| { | |||||
| if (scopes == null && LoginState != LoginState.LoggedIn) | |||||
| if (LoginState != LoginState.LoggedIn) | |||||
| throw new InvalidOperationException("You must log in before connecting or call ConnectAndAuthorizeAsync."); | throw new InvalidOperationException("You must log in before connecting or call ConnectAndAuthorizeAsync."); | ||||
| _scopes = scopes; | |||||
| if (_isFirstLogSub) | if (_isFirstLogSub) | ||||
| { | { | ||||
| @@ -232,8 +219,7 @@ namespace Discord | |||||
| await _rpcLogger.DebugAsync("Disconnecting - ApiClient").ConfigureAwait(false); | await _rpcLogger.DebugAsync("Disconnecting - ApiClient").ConfigureAwait(false); | ||||
| //Disconnect from server | //Disconnect from server | ||||
| await ApiClient.DisconnectAsync().ConfigureAwait(false); | await ApiClient.DisconnectAsync().ConfigureAwait(false); | ||||
| _scopes = null; | |||||
| ConnectionState = ConnectionState.Disconnected; | ConnectionState = ConnectionState.Disconnected; | ||||
| await _rpcLogger.InfoAsync("Disconnected").ConfigureAwait(false); | await _rpcLogger.InfoAsync("Disconnected").ConfigureAwait(false); | ||||
| @@ -272,7 +258,7 @@ namespace Discord | |||||
| await _connectionLock.WaitAsync().ConfigureAwait(false); | await _connectionLock.WaitAsync().ConfigureAwait(false); | ||||
| try | try | ||||
| { | { | ||||
| await ConnectInternalAsync(_scopes).ConfigureAwait(false); | |||||
| await ConnectInternalAsync().ConfigureAwait(false); | |||||
| } | } | ||||
| finally { _connectionLock.Release(); } | finally { _connectionLock.Release(); } | ||||
| return; | return; | ||||
| @@ -295,6 +281,14 @@ namespace Discord | |||||
| } | } | ||||
| } | } | ||||
| public async Task<string> AuthorizeAsync(string[] scopes) | |||||
| { | |||||
| await ConnectAsync().ConfigureAwait(false); | |||||
| var result = await ApiClient.SendAuthorizeAsync(scopes).ConfigureAwait(false); | |||||
| await DisconnectAsync().ConfigureAwait(false); | |||||
| return result.Code; | |||||
| } | |||||
| private async Task ProcessMessageAsync(string cmd, Optional<string> evnt, Optional<object> payload) | private async Task ProcessMessageAsync(string cmd, Optional<string> evnt, Optional<object> payload) | ||||
| { | { | ||||
| try | try | ||||
| @@ -313,21 +307,23 @@ namespace Discord | |||||
| var _ = Task.Run(async () => | var _ = Task.Run(async () => | ||||
| { | { | ||||
| RequestOptions options = new RequestOptions | |||||
| try | |||||
| { | { | ||||
| //CancellationToken = cancelToken //TODO: Implement | |||||
| }; | |||||
| if (_scopes != null) //No bearer | |||||
| RequestOptions options = new RequestOptions | |||||
| { | |||||
| //CancellationToken = cancelToken //TODO: Implement | |||||
| }; | |||||
| await ApiClient.SendAuthenticateAsync(options).ConfigureAwait(false); //Has bearer | |||||
| var __ = _connectTask.TrySetResultAsync(true); //Signal the .Connect() call to complete | |||||
| await _rpcLogger.InfoAsync("Ready").ConfigureAwait(false); | |||||
| } | |||||
| catch (Exception ex) | |||||
| { | { | ||||
| var authorizeData = await ApiClient.SendAuthorizeAsync(_scopes, options).ConfigureAwait(false); | |||||
| await ApiClient.LoginAsync(TokenType.Bearer, authorizeData.Code, options).ConfigureAwait(false); | |||||
| await _rpcLogger.ErrorAsync($"Error handling {cmd}{(evnt.IsSpecified ? $" ({evnt})" : "")}", ex).ConfigureAwait(false); | |||||
| return; | |||||
| } | } | ||||
| var authenticateData = await ApiClient.SendAuthenticateAsync(options).ConfigureAwait(false); //Has bearer | |||||
| var __ = _connectTask.TrySetResultAsync(true); //Signal the .Connect() call to complete | |||||
| await _rpcLogger.InfoAsync("Ready").ConfigureAwait(false); | |||||
| }); | }); | ||||
| } | } | ||||
| break; | break; | ||||