public async Task Comparison(CommandContext ctx, [Description("Criterion such as change1h, change24h, change7d, volume24h, marketcap")] string criterion, [Description("Cryptocurrency abbreviations separated by a space")] params string[] symbols) { try { //Cleaning 'trash' criterions if (criterion != "change1h" & criterion != "change24h" & criterion != "change7d" & criterion != "volume24h" & criterion != "marketcap") { throw new InvalidOperationException(); } //Try to get currencies var api = new CoinmarketcapAPI(); string json = api.QuotesLatest(Strings.Merger(symbols).ToUpper()); //Try to deserialize json var deserializeJson = JsonConvert.DeserializeObject <Cryptocurrency>(json); var cryptocurrencies = deserializeJson.Data.Values.ToList(); switch (criterion) { case "change1h": cryptocurrencies = cryptocurrencies.OrderByDescending(x => x.Quote.First().Value.Change1h).ToList(); break; case "change24h": cryptocurrencies = cryptocurrencies.OrderByDescending(x => x.Quote.First().Value.Change24h).ToList(); break; case "change7d": cryptocurrencies = cryptocurrencies.OrderByDescending(x => x.Quote.First().Value.Change7d).ToList(); break; case "volume24h": cryptocurrencies = cryptocurrencies.OrderByDescending(x => x.Quote.First().Value.Volume24h).ToList(); break; case "marketcap": cryptocurrencies = cryptocurrencies.OrderByDescending(x => x.Quote.First().Value.MarketCap).ToList(); break; } var embed = new DiscordEmbedBuilder() { Author = new DiscordEmbedBuilder.EmbedAuthor { IconUrl = "https://img.icons8.com/color/2x/price-comparison.png", Name = $"Comparison by {criterion}" }, Color = DiscordColor.Sienna, Timestamp = DateTime.Now, }; string names = GetEditedNames(cryptocurrencies); string criterions = string.Empty; if (criterion == "change1h" | criterion == "change24h" | criterion == "change7d") { criterions = GetEditedPercentes(cryptocurrencies, criterion); } else { criterions = GetEditedAmount(cryptocurrencies, criterion); } string prices = GetEditedPrices(cryptocurrencies); embed.AddField($"#CMCR - Name (Symbol)", names, true); embed.AddField($"{criterion.ToUpper()}", criterions, true); embed.AddField($"Price", prices, true); await ctx.Channel.SendMessageAsync(embed : embed); } catch (WebException) { await Prompt.SendPromptAsync(ctx.Channel, ctx.User, "Type the correct cryptocurrency abbreviations"); } catch (InvalidOperationException) { await Prompt.SendPromptAsync(ctx.Channel, ctx.User, "Type the correct criterion"); } catch (Exception ex) { BotLoging.PrintError(ctx.Client, ctx.User, ex); } }
public async Task Crypto(CommandContext ctx, [Description("Abbreviation of a specific cryptocurrency")] string abbreviation) { var api = new CoinmarketcapAPI(); try { string json = api.QuotesLatest(abbreviation); //Try to get currency var cryptocurrency = JsonConvert.DeserializeObject <Cryptocurrency>(json); var id = cryptocurrency.Data.First().Value.Id; var rank = cryptocurrency.Data.First().Value.Rank; var name = cryptocurrency.Data.First().Value.Name; var slug = cryptocurrency.Data.First().Value.Slug; var symbol = cryptocurrency.Data.First().Value.Symbol; var price = cryptocurrency.Data.First().Value.Quote["USD"].Price; var change1h = cryptocurrency.Data.First().Value.Quote["USD"].Change1h; var change24h = cryptocurrency.Data.First().Value.Quote["USD"].Change24h; var change7d = cryptocurrency.Data.First().Value.Quote["USD"].Change7d; var volume24h = cryptocurrency.Data.First().Value.Quote["USD"].Volume24h; var marketCap = cryptocurrency.Data.First().Value.Quote["USD"].MarketCap; var circulatingSupply = cryptocurrency.Data.First().Value.CirculatingSupply; var totalSupply = cryptocurrency.Data.First().Value.TotalSupply; var maxSupply = cryptocurrency.Data.First().Value.MaxSupply; var dateAdded = cryptocurrency.Data.First().Value.DateAdded; //Create Embed var embed = new DiscordEmbedBuilder() { Author = new DiscordEmbedBuilder.EmbedAuthor { IconUrl = $"https://s2.coinmarketcap.com/static/img/coins/64x64/{id}.png", Name = $"#{rank} - {name} ({symbol})", Url = $"https://coinmarketcap.com/currencies/{slug}/" }, Thumbnail = new DiscordEmbedBuilder.EmbedThumbnail { Url = $"https://s2.coinmarketcap.com/static/img/coins/64x64/{id}.png", Height = 100, Width = 100 }, Timestamp = cryptocurrency.Data.First().Value.LastUpdated, Title = DiscordText.Bold($"Price {DiscordText.UnderLine($"${Numbers.EditToReadableNumber(price)}")}"), }; //Price info embed.AddField( $"Change{DiscordText.BoldItalic("(1h)")}", change1h > 0 ? DiscordText.GreenLine($"{Numbers.EditToReadablePercent(change1h)}% ▲") : DiscordText.RedLine($"{Numbers.EditToReadablePercent(change1h)}% ▼"), true); embed.AddField( $"Change{DiscordText.BoldItalic("(24h)")}", change24h > 0 ? DiscordText.GreenLine($"{Numbers.EditToReadablePercent(change24h)}% ▲") : DiscordText.RedLine($"{Numbers.EditToReadablePercent(change24h)}% ▼"), true); embed.AddField( $"Change{DiscordText.BoldItalic("(7d)")}", change7d > 0 ? DiscordText.GreenLine($"{Numbers.EditToReadablePercent(change7d)}% ▲") : DiscordText.RedLine($"{Numbers.EditToReadablePercent(change7d)}% ▼"), true); embed.AddField( $"Volume{DiscordText.BoldItalic("(24h)")}", DiscordText.Bold($"${Numbers.EditToReadableNumber(volume24h)}"), true); embed.AddField($"⠀", "⠀", true); embed.AddField( $"Market cap", DiscordText.Bold($"${Numbers.EditToReadableNumber(marketCap)}"), true); embed.AddField( $"Circulating Supply", DiscordText.Bold(DiscordText.SingleLineCode($"{Numbers.EditToReadableNumber(circulatingSupply)} {symbol}")), true); embed.AddField( $"Total Supply", DiscordText.Bold(DiscordText.SingleLineCode($"{Numbers.EditToReadableNumber(totalSupply)} {symbol}")), true); embed.AddField( $"Max Supply", DiscordText.Bold(DiscordText.SingleLineCode($"{Numbers.EditToReadableNumber(maxSupply)} {symbol}")), true); //Coin info string info = $"{DiscordText.Bold("Date added:")} {dateAdded.ToShortDateString()}"; if (cryptocurrency.Data.First().Value.Platform != null) { info += Environment.NewLine + DiscordText.Bold("Platform: ") + DiscordText.SingleLineCode(cryptocurrency.Data.First().Value.Platform.Name); } embed.AddField(DiscordText.BoldUnderLine($"Coin data:"), info); //Change color if (change7d < 0) { embed.Color = DiscordColor.Red; } else if (change7d > 0) { embed.Color = DiscordColor.Green; } else { embed.Color = DiscordColor.Gray; } await ctx.Channel.SendMessageAsync(embed : embed); } catch (WebException) { await Prompt.SendPromptAsync(ctx.Channel, ctx.User, "To view the cryptocurrency rate, enter the correct abbreviated name"); } catch (Exception ex) { BotLoging.PrintError(ctx.Client, ctx.User, ex); } }