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); } }); }
public async Task GetCurrencies ( [Summary("Código de la moneda a mostrar. Si no se especifica, mostrará la lista de todos los códigos de monedas disponibles.")] string codigo = null ) { try { IDisposable typingState = Context.Channel.EnterTypingState(); List <WorldCurrencyCodeResponse> currenciesList = await FiatCurrencyService.GetWorldCurrenciesList(); if (codigo != null) { string currencyCode = Format.Sanitize(codigo).RemoveFormat(true).ToUpper().Trim(); await SendCurrencyValueAsync(currencyCode, currenciesList); typingState.Dispose(); } else { string commandPrefix = Configuration["commandPrefix"]; int replyTimeout = Convert.ToInt32(Configuration["interactiveMessageReplyTimeout"]); string currencyCommand = GetType().GetMethod(nameof(GetCurrencies)).GetCustomAttributes(true).OfType <CommandAttribute>().First().Text; List <EmbedBuilder> embeds = FiatCurrencyService.CreateWorldCurrencyListEmbedAsync(currenciesList, currencyCommand, Context.User.Username, true); await SendPagedReplyAsync(embeds, true); typingState.Dispose(); SocketMessage userResponse = await NextMessageAsync(timeout : TimeSpan.FromSeconds(replyTimeout)); if (userResponse != null) { string currencyCode = Format.Sanitize(userResponse.Content).RemoveFormat(true).Trim(); if (!currencyCode.StartsWith(commandPrefix)) { using (Context.Channel.EnterTypingState()) { await SendCurrencyValueAsync(currencyCode, currenciesList); } } } } } catch (Exception ex) { await SendErrorReply(ex); } }