Add the !corona command
This commit is contained in:
parent
ba0d116f3e
commit
efed2f7120
2 changed files with 76 additions and 0 deletions
67
Geekbot.net/Commands/Utils/Corona/CoronaStats.cs
Normal file
67
Geekbot.net/Commands/Utils/Corona/CoronaStats.cs
Normal file
|
@ -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<CoronaSummaryDto>(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
9
Geekbot.net/Commands/Utils/Corona/CoronaSummaryDto.cs
Normal file
9
Geekbot.net/Commands/Utils/Corona/CoronaSummaryDto.cs
Normal file
|
@ -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; }
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue