public async Task GetHistoricalCurrencyValuesAsync( [Summary("moneda", "Código de la moneda.")] [Autocomplete(typeof(FiatCurrencyCodeAutocompleteHandler))] string codigo, [Summary("desde", "Fecha desde.")] string startDate, [Summary("hasta", "Fecha hasta.")] string endDate ) { await DeferAsync().ContinueWith(async(task) => { try { string currencyCode = Format.Sanitize(codigo).ToUpper().Trim(); List <WorldCurrencyCodeResponse> historicalCurrencyCodeList = await FiatCurrencyService.GetWorldCurrenciesList(); WorldCurrencyCodeResponse worldCurrencyCodeResponse = historicalCurrencyCodeList.FirstOrDefault(x => x.Code.Equals(currencyCode, StringComparison.OrdinalIgnoreCase)); if (worldCurrencyCodeResponse != null) { TimeSpan oneYear = TimeSpan.FromDays(366); bool validStartDate = FiatCurrencyService.ParseDate(startDate, out DateTime? startDateResult) && startDateResult.HasValue; bool validEndDate = FiatCurrencyService.ParseDate(endDate, out DateTime? endDateResult) && endDateResult.HasValue; if (validStartDate && validEndDate) { DateTime dateFrom = startDateResult.Value; DateTime dateTo = endDateResult.Value; if (dateFrom <= dateTo && (dateTo.Subtract(dateFrom) <= oneYear)) { List <WorldCurrencyResponse> historicalCurrencyValues = await FiatCurrencyService.GetHistoricalCurrencyValues(currencyCode, dateFrom, dateTo); if (historicalCurrencyValues != null && historicalCurrencyValues.Count > 0) { List <EmbedBuilder> embeds = FiatCurrencyService.CreateHistoricalValuesEmbedsAsync(historicalCurrencyValues, worldCurrencyCodeResponse.Name, dateFrom, dateTo); await SendDeferredPaginatedEmbedAsync(embeds); } else { await SendNoDataForRangeAsync(dateFrom, dateTo); } } else { await SendInvalidDateRangeParametersAsync(dateFrom, dateTo); } } else { await SendInvalidDateParameterAsync(); } } else { await SendInvalidCurrencyCodeAsync(currencyCode); } } catch (Exception ex) { await SendDeferredErrorResponseAsync(ex); } }); }
public async Task HandleCalculatorModalInput(string currencyCode, FiatCurrencyCalculatorModal calculatorModal) { await DeferAsync().ContinueWith(async(task) => { try { bool isNumeric = decimal.TryParse(calculatorModal.Value.Replace(",", "."), NumberStyles.Any, DolarBotApiService.GetApiCulture(), out decimal amount); if (!isNumeric || amount <= 0) { amount = 1; } List <WorldCurrencyCodeResponse> currenciesList = await FiatCurrencyService.GetWorldCurrenciesList(); WorldCurrencyCodeResponse worldCurrencyCode = currenciesList.FirstOrDefault(x => x.Code.Equals(currencyCode, StringComparison.OrdinalIgnoreCase)); if (worldCurrencyCode != null) { WorldCurrencyResponse currencyResponse = await FiatCurrencyService.GetCurrencyValue(currencyCode); EmbedBuilder embed = await FiatCurrencyService.CreateWorldCurrencyEmbedAsync(currencyResponse, worldCurrencyCode.Name, amount); await SendDeferredEmbedAsync(embed.Build()); } } catch (Exception ex) { await SendDeferredErrorResponseAsync(ex); } }); }
/// <summary> /// Replies with an embed message for a single currency value. /// </summary> /// <param name="currencyCode">The currency 3-digit code.</param> /// <param name="currenciesList">The collection of valid currency codes.</param> private async Task SendCurrencyValueAsync(string currencyCode, List <WorldCurrencyCodeResponse> currenciesList) { WorldCurrencyCodeResponse worldCurrencyCode = currenciesList.FirstOrDefault(x => x.Code.Equals(currencyCode, StringComparison.OrdinalIgnoreCase)); if (worldCurrencyCode != null) { WorldCurrencyResponse currencyResponse = await FiatCurrencyService.GetCurrencyValue(currencyCode); EmbedBuilder embed = await FiatCurrencyService.CreateWorldCurrencyEmbedAsync(currencyResponse, worldCurrencyCode.Name); embed.AddCommandDeprecationNotice(Configuration); await ReplyAsync(embed : embed.Build()); } else { await SendInvalidCurrencyCodeAsync(currencyCode); } }
public async Task GetCurrenciesAsync( [Summary("moneda", "Código de la moneda. Si no se especifica, mostrará todos los códigos de monedas disponibles.")] [Autocomplete(typeof(FiatCurrencyCodeAutocompleteHandler))] string codigo = null ) { await DeferAsync().ContinueWith(async(task) => { try { List <WorldCurrencyCodeResponse> currenciesList = await FiatCurrencyService.GetWorldCurrenciesList(); if (codigo != null) { string currencyCode = Format.Sanitize(codigo).ToUpper().Trim(); WorldCurrencyCodeResponse worldCurrencyCode = currenciesList.FirstOrDefault(x => x.Code.Equals(currencyCode, StringComparison.OrdinalIgnoreCase)); if (worldCurrencyCode != null) { WorldCurrencyResponse currencyResponse = await FiatCurrencyService.GetCurrencyValue(currencyCode); EmbedBuilder embed = await FiatCurrencyService.CreateWorldCurrencyEmbedAsync(currencyResponse, worldCurrencyCode.Name); await SendDeferredEmbedAsync(embed.Build(), components: new CalculatorComponentBuilder(currencyCode, CalculatorTypes.FiatCurrency, Configuration).Build()); } else { await SendInvalidCurrencyCodeAsync(currencyCode); } } else { string commandPrefix = Configuration["commandPrefix"]; int replyTimeout = Convert.ToInt32(Configuration["interactiveMessageReplyTimeout"]); string currencyCommand = GetType().GetMethod(nameof(GetCurrenciesAsync)).GetCustomAttributes(true).OfType <SlashCommandAttribute>().First().Name; EmbedBuilder[] embeds = FiatCurrencyService.CreateWorldCurrencyListEmbedAsync(currenciesList, currencyCommand, Context.User.Username).ToArray(); await SendDeferredPaginatedEmbedAsync(embeds); } } catch (Exception ex) { await SendDeferredErrorResponseAsync(ex); } }); }
public async Task GetHistoricalCurrencyValues ( [Summary("Código de la moneda a mostrar.")] string codigo = null, [Summary("(Opcional) Fecha desde. Si no se especifica, se mostrará todo el histórico.")] string fechaDesde = null, [Summary("(Opcional) Fecha hasta. Si no se especifica, se tomará la fecha del día actual.")] string fechaHasta = null ) { try { using (Context.Channel.EnterTypingState()) { if (string.IsNullOrWhiteSpace(codigo)) { await SendMissingParameterForHistoricalCurrencyCommandAsync(); } else { string currencyCode = Format.Sanitize(codigo).RemoveFormat(true).ToUpper().Trim(); List <WorldCurrencyCodeResponse> historicalCurrencyCodeList = await FiatCurrencyService.GetWorldCurrenciesList(); WorldCurrencyCodeResponse worldCurrencyCodeResponse = historicalCurrencyCodeList.FirstOrDefault(x => x.Code.Equals(currencyCode, StringComparison.OrdinalIgnoreCase)); if (worldCurrencyCodeResponse != null) { DateTime?startDate = null; DateTime?endDate = null; if (string.IsNullOrWhiteSpace(fechaDesde) || FiatCurrencyService.ParseDate(fechaDesde, out startDate)) { if (string.IsNullOrWhiteSpace(fechaHasta) || FiatCurrencyService.ParseDate(fechaHasta, out endDate)) { DateTime dateFrom = startDate?.Date ?? DateTime.Now; DateTime dateTo = endDate?.Date ?? DateTime.Now; TimeSpan oneYear = TimeSpan.FromDays(365); if (dateFrom <= dateTo && (dateTo.Subtract(dateFrom) < oneYear)) { List <WorldCurrencyResponse> historicalCurrencyValues = await FiatCurrencyService.GetHistoricalCurrencyValues(currencyCode, startDate, endDate); if (historicalCurrencyValues != null && historicalCurrencyValues.Count > 0) { List <EmbedBuilder> embeds = FiatCurrencyService.CreateHistoricalValuesEmbedsAsync(historicalCurrencyValues, worldCurrencyCodeResponse.Name, startDate?.Date, endDate?.Date); await SendPagedReplyAsync(embeds, true); } else { await SendNoDataForRangeAsync(startDate, endDate); } } else { await SendInvalidDateRangeParametersAsync(startDate, endDate); } } else { await SendInvalidDateParameterAsync(fechaHasta); } } else { await SendInvalidDateParameterAsync(fechaDesde); } } else { await SendInvalidCurrencyCodeAsync(currencyCode); } } } } catch (Exception ex) { await SendErrorReply(ex); } }