/// <summary> /// Replies with an embed message for a single cryptocurrency value. /// </summary> /// <param name="searchText">The text to be searched.</param> /// <param name="cryptoCurrenciesList">The collection of valid currency codes.</param> /// <param name="allowMultipleResults">Indicates wether the search result can return multiple results.</param> private async Task SendCryptoCurrencyValueAsync(string searchText, List <CryptoCodeResponse> cryptoCurrenciesList, bool allowMultipleResults = true, IDisposable typingState = null) { List <CryptoCodeResponse> cryptoCurrencyCodeResponses = CryptoService.FilterByText(cryptoCurrenciesList, searchText); if (allowMultipleResults && cryptoCurrencyCodeResponses.Count > 1) { string title = $"Multiples resultados para {Format.Code(searchText)}"; string description = $"La búsqueda retornó multiples resultados."; await SendCryptoCurrencyListAsync(cryptoCurrencyCodeResponses, true, title, description, typingState); } else if (cryptoCurrencyCodeResponses.Count == 1) { CryptoCodeResponse cryptoCurrencyCodeResponse = cryptoCurrencyCodeResponses.First(); CryptoResponse cryptoResponse = await CryptoService.GetCryptoRateByCode(cryptoCurrencyCodeResponse.Code); await SendCryptoReply(cryptoResponse, cryptoCurrencyCodeResponse.Name); } else { string commandPrefix = Configuration["commandPrefix"]; string currencyCommand = GetType().GetMethod(nameof(GetCryptoRatesAsync)).GetCustomAttributes(true).OfType <CommandAttribute>().First().Text; await ReplyAsync($"El código {Format.Code(searchText)} no corresponde con ningún identificador válido. Para ver la lista de identificadores de criptomonedas disponibles, ejecutá {Format.Code($"{commandPrefix}{currencyCommand}")}."); } if (typingState != null) { typingState.Dispose(); } }
/// <summary> /// Replies with an embed message for a single cryptocurrency value. /// </summary> /// <param name="cryptoCurrenciesList">The collection of valid currency codes.</param> /// <param name="code">The code to be searched.</param> /// <param name="quantity">Crypto currency quantity.</param> private async Task SendCryptoResponseAsync(List <CryptoCodeResponse> cryptoCurrenciesList, string code, decimal quantity = 1) { CryptoCodeResponse cryptoCurrencyCode = cryptoCurrenciesList.FirstOrDefault(x => x.Code.Equals(code, StringComparison.OrdinalIgnoreCase)); if (cryptoCurrencyCode != null) { CryptoResponse cryptoResponse = await CryptoService.GetCryptoRateByCode(cryptoCurrencyCode.Code); if (cryptoResponse != null) { EmbedBuilder embed = await CryptoService.CreateCryptoEmbedAsync(cryptoResponse, cryptoCurrencyCode.Name, quantity); await SendDeferredEmbedAsync(embed.Build(), new CalculatorComponentBuilder(cryptoCurrencyCode.Code, CalculatorTypes.Crypto, Configuration).Build()); } else { await SendDeferredApiErrorResponseAsync(); } } else { await SendDeferredMessageAsync($"No hay resultados para la búsqueda. Asegurate de utilizar los resultados autocompletados."); } }
/// <summary> /// Filters the <paramref name="cryptoCurrenciesList"/> searching first by code (equals), then by symbol (equals) and lastly by name (contains). /// </summary> /// <param name="cryptoCurrenciesList">The cryptocurrency list.</param> /// <param name="searchText">The text to search.</param> /// <returns></returns> public static List <CryptoCodeResponse> FilterByText(List <CryptoCodeResponse> cryptoCurrenciesList, string searchText) { CryptoCodeResponse cryptoCodeResponse = cryptoCurrenciesList.FirstOrDefault(x => x.Code.Equals(searchText, StringComparison.OrdinalIgnoreCase)); if (cryptoCodeResponse != null) { return(new List <CryptoCodeResponse>() { cryptoCodeResponse }); } else { List <CryptoCodeResponse> cryptoResults = cryptoCurrenciesList.Where(x => x.Symbol.Equals(searchText, StringComparison.OrdinalIgnoreCase)).ToList(); if (cryptoResults.Count > 0) { return(cryptoResults); } else { return(searchText.Length >= 3 ? cryptoCurrenciesList.Where(x => x.Name.Contains(searchText, StringComparison.OrdinalIgnoreCase)).ToList() : new List <CryptoCodeResponse>()); } } }