From 15e1d108393888cd4fedb6bef94e97d2b8bdce5f Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 22 Jul 2022 18:00:12 +0200 Subject: [PATCH] remove the !corona command --- .../Corona/CoronaApiCountryResponseDto.cs | 19 --- src/Bot/Commands/Utils/Corona/CoronaStats.cs | 119 ------------------ .../Commands/Utils/Corona/CoronaTotalDto.cs | 10 -- 3 files changed, 148 deletions(-) delete mode 100644 src/Bot/Commands/Utils/Corona/CoronaApiCountryResponseDto.cs delete mode 100644 src/Bot/Commands/Utils/Corona/CoronaStats.cs delete mode 100644 src/Bot/Commands/Utils/Corona/CoronaTotalDto.cs diff --git a/src/Bot/Commands/Utils/Corona/CoronaApiCountryResponseDto.cs b/src/Bot/Commands/Utils/Corona/CoronaApiCountryResponseDto.cs deleted file mode 100644 index 2a773bb..0000000 --- a/src/Bot/Commands/Utils/Corona/CoronaApiCountryResponseDto.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Text.Json.Serialization; - -namespace Geekbot.Bot.Commands.Utils.Corona -{ - public record CoronaApiCountryResponseDto - { - [JsonPropertyName("country")] - public string Country { get; init; } - - [JsonPropertyName("cases")] - public decimal Cases { get; init; } - - [JsonPropertyName("deaths")] - public decimal Deaths { get; init; } - - [JsonPropertyName("recovered")] - public decimal Recovered { get; init; } - } -} \ No newline at end of file diff --git a/src/Bot/Commands/Utils/Corona/CoronaStats.cs b/src/Bot/Commands/Utils/Corona/CoronaStats.cs deleted file mode 100644 index b4854c7..0000000 --- a/src/Bot/Commands/Utils/Corona/CoronaStats.cs +++ /dev/null @@ -1,119 +0,0 @@ -using System.Text; -using Discord; -using Discord.Commands; -using Geekbot.Core; -using Geekbot.Core.Converters; -using Geekbot.Core.ErrorHandling; -using Geekbot.Core.Extensions; -using Geekbot.Core.GuildSettingsManager; -using Localization = Geekbot.Core.Localization; - -namespace Geekbot.Bot.Commands.Utils.Corona -{ - public class CoronaStats : GeekbotCommandBase - { - - public CoronaStats(IErrorHandler errorHandler, IGuildSettingsManager guildSettingsManager) : base(errorHandler, guildSettingsManager) - { - } - - [Command("corona", RunMode = RunMode.Async)] - [Summary("Get the latest worldwide corona statistics")] - public async Task Summary([Summary("CountryCode")] string countryCode = null) - { - try - { - var summary = await GetCoronaInfo(countryCode); - if (summary == null) - { - await Context.Channel.SendMessageAsync($"`{countryCode}` is not a valid country code"); - return; - } - - var activeCases = summary.Cases - (summary.Recovered + summary.Deaths); - - string CalculatePercentage(decimal i) => (i / summary.Cases).ToString("#0.##%"); - var activePercent = CalculatePercentage(activeCases); - var recoveredPercentage = CalculatePercentage(summary.Recovered); - var deathsPercentage = CalculatePercentage(summary.Deaths); - - var numberFormat = "#,#"; - var totalFormatted = summary.Cases.ToString(numberFormat); - var activeFormatted = activeCases.ToString(numberFormat); - var recoveredFormatted = summary.Recovered.ToString(numberFormat); - var deathsFormatted = summary.Deaths.ToString(numberFormat); - - var embedTitleBuilder = new StringBuilder(); - embedTitleBuilder.Append(Localization.Corona.ConfirmedCases); - if (!string.IsNullOrEmpty(summary.Country)) - { - embedTitleBuilder.Append(" - "); - embedTitleBuilder.Append(EmojiConverter.CountryCodeToEmoji(summary.Country)); - } - - var eb = new EmbedBuilder - { - Author = new EmbedAuthorBuilder - { - Name = embedTitleBuilder.ToString(), - IconUrl = "https://www.redcross.org/content/dam/icons/disasters/virus/Virus-1000x1000-R-Pl.png" - }, - Footer = new EmbedFooterBuilder - { - Text = $"{Localization.Corona.Source}: covid19-api.org", - }, - Color = Color.Red - }; - eb.AddField(Localization.Corona.Total, totalFormatted); - eb.AddInlineField(Localization.Corona.Active, $"{activeFormatted} ({activePercent})"); - eb.AddInlineField(Localization.Corona.Recovered, $"{recoveredFormatted} ({recoveredPercentage})"); - eb.AddInlineField(Localization.Corona.Deaths, $"{deathsFormatted} ({deathsPercentage})"); - - await Context.Channel.SendMessageAsync(String.Empty, false, eb.Build()); - } - catch (Exception e) - { - await ErrorHandler.HandleCommandException(e, Context); - } - } - - private async Task GetCoronaInfo(string countryCode = null) - { - var allCountries = await HttpAbstractions.Get>(new Uri("https://covid19-api.org/api/status")); - - if (string.IsNullOrEmpty(countryCode)) - { - return allCountries.Aggregate( - new CoronaTotalDto(), - (accumulate, source) => - { - accumulate.Cases += source.Cases; - accumulate.Deaths += source.Deaths; - accumulate.Recovered += source.Recovered; - return accumulate; - } - ); - } - - if (countryCode.Length != 2) - { - return null; - } - - var upcasedCountryCode = countryCode.ToUpper(); - var countryStats = allCountries.Find(x => x.Country == upcasedCountryCode); - if (countryStats == null) - { - return null; - } - - return new CoronaTotalDto() - { - Country = upcasedCountryCode, - Cases = countryStats.Cases, - Deaths = countryStats.Deaths, - Recovered = countryStats.Recovered, - }; - } - } -} \ No newline at end of file diff --git a/src/Bot/Commands/Utils/Corona/CoronaTotalDto.cs b/src/Bot/Commands/Utils/Corona/CoronaTotalDto.cs deleted file mode 100644 index 58e5ac9..0000000 --- a/src/Bot/Commands/Utils/Corona/CoronaTotalDto.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Geekbot.Bot.Commands.Utils.Corona -{ - public record CoronaTotalDto - { - public string Country { get; set; } - public decimal Cases { get; set; } - public decimal Deaths { get; set; } - public decimal Recovered { get; set; } - } -} \ No newline at end of file