From efed2f712048e904de30299d66477003cd1ac533 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 15 Jul 2020 03:09:43 +0200 Subject: [PATCH] Add the !corona command --- .../Commands/Utils/Corona/CoronaStats.cs | 67 +++++++++++++++++++ .../Commands/Utils/Corona/CoronaSummaryDto.cs | 9 +++ 2 files changed, 76 insertions(+) create mode 100644 Geekbot.net/Commands/Utils/Corona/CoronaStats.cs create mode 100644 Geekbot.net/Commands/Utils/Corona/CoronaSummaryDto.cs diff --git a/Geekbot.net/Commands/Utils/Corona/CoronaStats.cs b/Geekbot.net/Commands/Utils/Corona/CoronaStats.cs new file mode 100644 index 0000000..4be8195 --- /dev/null +++ b/Geekbot.net/Commands/Utils/Corona/CoronaStats.cs @@ -0,0 +1,67 @@ +using System; +using System.Text; +using System.Threading.Tasks; +using Discord; +using Discord.Commands; +using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Extensions; + +namespace Geekbot.net.Commands.Utils.Corona +{ + public class CoronaStats : ModuleBase + { + private readonly IErrorHandler _errorHandler; + + public CoronaStats(IErrorHandler errorHandler) + { + _errorHandler = errorHandler; + } + + [Command("corona", RunMode = RunMode.Async)] + [Summary("Get the latest worldwide corona statistics")] + public async Task Summary() + { + try + { + var summary = await HttpAbstractions.Get(new Uri("https://api.covid19api.com/world/total")); + var activeCases = summary.TotalConfirmed - (summary.TotalRecovered + summary.TotalDeaths); + + string CalculatePercentage(decimal i) => (i / summary.TotalConfirmed).ToString("#0.##%"); + var activePercent = CalculatePercentage(activeCases); + var recoveredPercentage = CalculatePercentage(summary.TotalRecovered); + var deathsPercentage = CalculatePercentage(summary.TotalDeaths); + + var numberFormat = "#,#"; + var totalFormatted = summary.TotalConfirmed.ToString(numberFormat); + var activeFormatted = activeCases.ToString(numberFormat); + var recoveredFormatted = summary.TotalRecovered.ToString(numberFormat); + var deathsFormatted = summary.TotalDeaths.ToString(numberFormat); + + var eb = new EmbedBuilder + { + Author = new EmbedAuthorBuilder + { + Name = "Confirmed Corona Cases", + IconUrl = "https://www.redcross.org/content/dam/icons/disasters/virus/Virus-1000x1000-R-Pl.png" + }, + Footer = new EmbedFooterBuilder + { + Text = "Source: covid19api.com", + }, + Color = Color.Red + }; + eb.AddField("Total", totalFormatted); + eb.AddInlineField("Active", $"{activeFormatted} ({activePercent})"); + eb.AddInlineField("Recovered", $"{recoveredFormatted} ({recoveredPercentage})"); + eb.AddInlineField("Deaths", $"{deathsFormatted} ({deathsPercentage})"); + + await Context.Channel.SendMessageAsync(String.Empty, false, eb.Build()); + } + catch (Exception e) + { + await _errorHandler.HandleCommandException(e, Context); + } + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Utils/Corona/CoronaSummaryDto.cs b/Geekbot.net/Commands/Utils/Corona/CoronaSummaryDto.cs new file mode 100644 index 0000000..5639249 --- /dev/null +++ b/Geekbot.net/Commands/Utils/Corona/CoronaSummaryDto.cs @@ -0,0 +1,9 @@ +namespace Geekbot.net.Commands.Utils.Corona +{ + public class CoronaSummaryDto + { + public decimal TotalConfirmed { get; set; } + public decimal TotalDeaths { get; set; } + public decimal TotalRecovered { get; set; } + } +} \ No newline at end of file