protected virtual async Task ValidateTokenAsync(TokenType tokenType, string token) { try { var user = await GetCurrentUserAsync().ConfigureAwait(false); if (user == null) //Is using a cached DiscordClient { user = new SelfUser(this, await ApiClient.GetMyUserAsync().ConfigureAwait(false)); } if (user.IsBot && tokenType == TokenType.User) { throw new InvalidOperationException($"A bot token used provided with {nameof(TokenType)}.{nameof(TokenType.User)}"); } else if (!user.IsBot && tokenType == TokenType.Bot) //Discord currently sends a 401 in this case { throw new InvalidOperationException($"A user token used provided with {nameof(TokenType)}.{nameof(TokenType.Bot)}"); } } catch (HttpException ex) { throw new ArgumentException("Token validation failed", nameof(token), ex); } }
/// <inheritdoc /> public virtual async Task <ISelfUser> GetCurrentUserAsync() { var user = _currentUser; if (user == null) { var model = await ApiClient.GetMyUserAsync().ConfigureAwait(false); user = new SelfUser(this, model); _currentUser = user; } return(user); }
private async Task LogoutInternalAsync() { if (LoginState == LoginState.LoggedOut) { return; } LoginState = LoginState.LoggingOut; await ApiClient.LogoutAsync().ConfigureAwait(false); await OnLogoutAsync().ConfigureAwait(false); _currentUser = null; LoginState = LoginState.LoggedOut; await _loggedOutEvent.InvokeAsync().ConfigureAwait(false); }