Skip to content

Commit

Permalink
Cleaned up code and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-pierce committed Jul 29, 2020
1 parent 565d78c commit e9813f7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 133 deletions.
67 changes: 22 additions & 45 deletions commands/cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,17 @@ module.exports = {
return cases;
}
let totalCases = await getTotalCases();
console.log(totalCases);
return message.channel.send(`Globally, there have been ${numberWithCommas(totalCases["cases"])} cases of COVID-19.`);
}
// Data for TODAY
// Cases TODAY
else if ((args[0] === "today" || args[0] === "td")) {
// New cases TODAY
// New global cases TODAY
if (args.length == 1) {
let getTodayCases = async () => {
let response = await axios.get("https://corona.lmao.ninja/v2/all?yesterday=false");
return data = response.data;
}
let todayCases = await getTodayCases();

console.log(todayCases["todayCases"]);
message.channel.send(`Today, there are ${numberWithCommas(todayCases["todayCases"])} new cases of COVID-19.`);
}
// Country-specific new cases TODAY
Expand All @@ -42,24 +39,21 @@ module.exports = {
return data = response.data;
}
let todayCountryCases = await getTodayCountryCases();

console.log(todayCountryCases["todayCases"]);
message.channel.send(`Today, there are ${numberWithCommas(todayCountryCases["todayCases"])} new cases of COVID-19 in ${todayCountryCases["country"]}.`);
}
}
// The user specifies their search for YESTERDAY
// Cases YESTERDAY
else if (args[0] === "yesterday" || args[0] === "ytd") {
// Global cases YESTERDAY
// New global cases YESTERDAY
if (args.length == 1) {
let getYesterdayCases = async () => {
let response = await axios.get("https://corona.lmao.ninja/v2/all?yesterday=true");
return cases = response.data;
}
let yesterdayCases = await getYesterdayCases();

message.channel.send(`Yesterday, there were ${numberWithCommas(yesterdayCases["todayCases"])} new cases of COVID-19.`);
}
// Country-specific cases YESTERDAY
// New country-specific cases YESTERDAY
else if (args.length >= 2) {
let country = args.slice(1).join(" ");

Expand All @@ -72,45 +66,38 @@ module.exports = {
return cases = response.data;
}
let yesterdayCountryCases = await getYesterdayCountryCases();
console.log(yesterdayCountryCases);

message.channel.send(`Yesterday, there were ${numberWithCommas(yesterdayCountryCases["todayCases"])} new cases of COVID-19 in ${yesterdayCountryCases["country"]}.`);
}
else {
return message.channel.send(`<@${message.author.id}> - Invalid arguments. Please type !covhelp for help with commands.`);
}
}
// Data HISTORICALLY (This argument returns a GRAPH)
// HISTORIC Cases (Sends a graph)
else if (args[0] === "historic" || args[0] === "hs") {

// No specification on how far back the data goes (Simply 30 days)
// No specified number of days (defaulted to 30)
if (args.length == 1) {
let casesData = [];
let xAxisLabels = [];

let getHistoricCases = async () => {
let response = await axios.get("https://corona.lmao.ninja/v2/historical/all");
return historicCases = response.data;
}
let globalHistoricCases = await getHistoricCases();

let casesData = [];
let xAxisLabels = [];

// Format x-axis labels and compile data to be used on graph
for (day in globalHistoricCases["cases"]){
xAxisLabels.push("\"" + day + "\"");
casesData.push(globalHistoricCases["cases"][`${day}`])
}
console.log(xAxisLabels);

// Create a new embedded message for the bot to display the historic cases
// Create a new embedded message for the bot to display historic cases
const historicCasesEmbed = new Discord.MessageEmbed()
.setColor("#990000")
.setTitle("Daily Cases for the Past 30 Days Globally")
.setImage(`https://quickchart.io/chart?width=500&height=350&c={type:'line',data:{labels:[${xAxisLabels}],datasets:[{label:'Cases',data:[${casesData}],fill:false,borderColor:"rgb(255,160,122)",pointBackgroundColor:"rgb(255,160,122)"}]},options:{legend:{labels:{fontColor:"white",fontSize:18}},scales:{yAxes:[{ticks:{fontColor:"white",beginAtZero:false,fontSize:16}}],xAxes:[{ticks:{fontColor:"white",fontSize:16}}]}}}`)

return message.channel.send(historicCasesEmbed);
}
// Global cases historically for a specified number of days
// Specified number of days
else if (args.length == 2 && typeof(parseFloat(args[1])) === 'number') {
let numDays = args[1];

Expand All @@ -122,6 +109,9 @@ module.exports = {
else if (numDays < 2)
return message.channel.send(`<@${message.author.id}> - The number of days specified must be at least 2.`);
else {
let dayCasesData = [];
let xAxisLabels = [];

let getDayHistoricCases = async () => {
let response = await axios.get("https://corona.lmao.ninja/v2/historical/all?lastdays=" + numDays).catch(err =>{
if (err.response){
Expand All @@ -131,43 +121,36 @@ module.exports = {
return data = response.data;
}
let globalDayHistoricCases = await getDayHistoricCases();

let dayCasesData = [];
let xAxisLabels = [];

// // Format x-axis labels and compile data to be used on graph
// for (day in historicCountryDeaths["timeline"]["deaths"]){
// xAxisLabels.push("\"" + day + "\"");
// countryDeathData.push(historicCountryDeaths["timeline"]["deaths"][`${day}`])
// }

// Format x-axis labels and compile data to be used on graph
for (day in globalDayHistoricCases["cases"]){
xAxisLabels.push("\"" + day + "\"");
dayCasesData.push(globalDayHistoricCases["cases"][`${day}`])
}

// Create a new embedded message for the bot to display the Country-specific historic deaths
// Create a new embedded message for the bot to display historic cases for a specified number of days
const historicCasesEmbed = new Discord.MessageEmbed()
.setColor("#990000")
.setTitle(`Daily Cases for the Past ${numDays} Days Globally`)
.setTitle(`Daily Cases Trend for the Past ${numDays} Days Globally`)
.setImage(`https://quickchart.io/chart?width=500&height=350&c={type:'line',data:{labels:[${xAxisLabels}],datasets:[{label:'Cases',data:[${dayCasesData}],fill:false,borderColor:"rgb(255,160,122)",pointBackgroundColor:"rgb(255,160,122)"}]},options:{legend:{labels:{fontColor:"white",fontSize:18}},scales:{yAxes:[{ticks:{fontColor:"white",beginAtZero:false,fontSize:16}}],xAxes:[{ticks:{fontColor:"white",fontSize:16}}]}}}`)

return message.channel.send(historicCasesEmbed);
}
}
// Country specific historic deaths for certain days
// Country-specific cases trend for specified number of days
else if (args.length >= 3 && typeof(parseFloat(args[1]) === 'number') && /^[a-zA-Z\s]*$/i.test(args.slice(2).join(" "))) {
let countryName = args.slice(2).join(" ");
let numDays = args[1];

// Input validation - the number of days must be an integer between 2 and 100, inclusive
if (!Number.isInteger(parseFloat(numDays)) && !isNaN(numDays))
return message.channel.send(`<@${message.author.id}> - Number of days must be a valid integer.`);
else if (numDays > 100)
return message.channel.send(`<@${message.author.id}> - I can only display data from up to the past 100 days.`);
else if (numDays < 2)
return message.channel.send(`<@${message.author.id}> - The number of days specified must be at least 2.`);
else {
let countryCasesData = [];
let xAxisLabels = [];

let getCountryHistoricCases = async () => {
let response = await axios.get("https://corona.lmao.ninja/v2/historical/" + countryName + "?lastdays=" + numDays).catch(err =>{
if (err.response){
Expand All @@ -177,17 +160,12 @@ module.exports = {
return data = response.data;
}
let historicCountryCases = await getCountryHistoricCases();

let countryCasesData = [];
let xAxisLabels = [];

// Format x-axis labels and compile data to be used on graph
for (day in historicCountryCases["timeline"]["cases"]){
xAxisLabels.push("\"" + day + "\"");
countryCasesData.push(historicCountryCases["timeline"]["cases"][`${day}`])
}

// Create a new embedded message for the bot to display the Country-specific historic deaths
// Create a new embedded message for the bot to display the Country-specific + day-specific historic cases
const historicCasesEmbed = new Discord.MessageEmbed()
.setColor("#990000")
.setTitle(`Daily Cases for the Past ${numDays} Days in ${historicCountryCases["country"]}`)
Expand All @@ -201,7 +179,6 @@ module.exports = {
return message.channel.send(`<@${message.author.id}> - Please enter a valid argument. Type !covhelp for help with commands.`);
}
}

// Helper function that adds commas to large numbers using REGEX
let numberWithCommas = (x) => {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
Expand Down
Loading

0 comments on commit e9813f7

Please sign in to comment.