Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Covid updates command #2

Merged
merged 2 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions Models/CovidData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace CouchBot.UserCommands.Models
{
public class CovidData
{
[JsonProperty("updated")]
public long Updated { get; set; }

[JsonProperty("cases")]
public int Cases { get; set; }

[JsonProperty("todayCases")]
public int TodayCases { get; set; }

[JsonProperty("deaths")]
public int Deaths { get; set; }

[JsonProperty("todayDeaths")]
public int TodayDeaths { get; set; }

[JsonProperty("recovered")]
public int Recovered { get; set; }

[JsonProperty("todayRecovered")]
public int TodayRecovered { get; set; }

[JsonProperty("active")]
public int Active { get; set; }

[JsonProperty("critical")]
public int Critical { get; set; }

[JsonProperty("casesPerOneMillion")]
public double CasesPerOneMillion { get; set; }

[JsonProperty("deathsPerOneMillion")]
public double DeathsPerOneMillion { get; set; }

[JsonProperty("tests")]
public int Tests { get; set; }

[JsonProperty("testsPerOneMillion")]
public double TestsPerOneMillion { get; set; }

[JsonProperty("population")]
public long Population { get; set; }

[JsonProperty("oneCasePerPeople")]
public double OneCasePerPeople { get; set; }

[JsonProperty("oneDeathPerPeople")]
public double OneDeathPerPeople { get; set; }

[JsonProperty("oneTestPerPeople")]
public double OneTestPerPeople { get; set; }

[JsonProperty("activePerOneMillion")]
public double ActivePerOneMillion { get; set; }

[JsonProperty("recoveredPerOneMillion")]
public double RecoveredPerOneMillion { get; set; }

[JsonProperty("criticalPerOneMillion")]
public double CriticalPerOneMillion { get; set; }

[JsonProperty("affectedCountries")]
public int AffectedCountries { get; set; }
}
}
54 changes: 53 additions & 1 deletion Modules/UserCommands.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using Discord.Commands;
using CouchBot.UserCommands.Models;
using Discord.Commands;
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace CouchBot.UserCommands.Modules
{
Expand All @@ -17,5 +22,52 @@ public class UserCommands : ModuleBase
//{
// return ReplyAsync("Your Return Text!");
//}


[Command("covid", RunMode = RunMode.Async)]
[Alias("covid status", "covid stats", "covid news", "covid today", "covid updates", "corona", "coronavirus")]
[Summary("returns the latest covid cases cases updates using public API")]
public Task GetCovidDataAsync()
{
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://corona.lmao.ninja/");
var response = client.GetAsync("v2/all").GetAwaiter().GetResult();

if (response.IsSuccessStatusCode)
{
var data = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
CovidData covidData = JsonConvert.DeserializeObject<CovidData>(data);

Random random = new Random();
var number = random.Next(1, 4);

switch (number)
{
case 1:
return ReplyAsync(string.Format("Out of {0} total cases, {1} were reported today.",
covidData.Cases, covidData.TodayCases));
case 2:
return ReplyAsync(string.Format("Out of {0} deaths, {1} were died today.",
covidData.Deaths, covidData.TodayDeaths));
default:
return ReplyAsync(string.Format("Great news! {0} people recovered today.",
covidData.TodayRecovered));
}
}
else
{
return ReplyAsync("I'm feeling dizzy");
}
}
}
catch (Exception ex)
{
return ReplyAsync("I'm exhausted");
}

}
}
}