diff --git a/commands/cases.js b/commands/cases.js index 8fd3d53..f1bfd7b 100644 --- a/commands/cases.js +++ b/commands/cases.js @@ -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 @@ -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(" "); @@ -72,37 +66,30 @@ 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") @@ -110,7 +97,7 @@ module.exports = { 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]; @@ -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){ @@ -131,36 +121,26 @@ 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) @@ -168,6 +148,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 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){ @@ -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"]}`) @@ -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, ","); diff --git a/commands/deaths.js b/commands/deaths.js index b27a1e5..ef6db61 100644 --- a/commands/deaths.js +++ b/commands/deaths.js @@ -5,7 +5,7 @@ module.exports = { name: "deaths", description: "Provides daily-updated data on COVID-19 related deaths globally", async execute(message, args){ - // Global death cases TOTAL + // Global deaths TOTAL if (!args.length) { let getTotalDeaths = async () => { let response = await axios.get("https://corona.lmao.ninja/v2/all?yesterday=false"); @@ -13,10 +13,9 @@ module.exports = { return deaths; } let totalDeaths = await getTotalDeaths(); - console.log(totalDeaths); return message.channel.send(`Globally, ${numberWithCommas(totalDeaths["deaths"])} people have died due to COVID-19.`); } - // Data for TODAY + // Deaths TODAY else if ((args[0] === "today" || args[0] === "td")) { // Global deaths TODAY if (args.length == 1) { @@ -26,8 +25,6 @@ module.exports = { return deaths; } let info = await getDeaths(); - - console.log(info["todayDeaths"]); message.channel.send(`Today, ${numberWithCommas(info["todayDeaths"])} people have died due to COVID-19.`); } // Country-specific deaths TODAY @@ -43,13 +40,10 @@ module.exports = { return deaths = response.data; } let info = await getDeaths(); - let totalTodayDeathsCountry = info["todayDeaths"]; - - console.log(totalTodayDeathsCountry); - message.channel.send(`Today, ${numberWithCommas(totalTodayDeathsCountry)} people have died due to COVID-19 in ${info["country"]}.`); + message.channel.send(`Today, ${numberWithCommas(info["todayDeaths"])} people have died due to COVID-19 in ${info["country"]}.`); } } - // The user specifies their search for YESTERDAY + // Deaths YESTERDAY else if (args[0] === "yesterday" || args[0] === "ytd") { // Global deaths YESTERDAY if (args.length == 1) { @@ -58,8 +52,6 @@ module.exports = { return deaths = response.data; } let info = await getDeaths(); - - console.log(info["todayDeaths"]); message.channel.send(`Yesterday, ${numberWithCommas(info["todayDeaths"])} people died due to COVID-19.`); } // Country-specific deaths YESTERDAY @@ -75,38 +67,30 @@ module.exports = { return deaths = response.data; } let info = await getDeaths(); - let totalYTDDeathsCountry = info["todayDeaths"]; - - console.log(totalYTDDeathsCountry); - message.channel.send(`Yesterday, ${numberWithCommas(totalYTDDeathsCountry)} people died due to COVID-19 in ${info["country"]}.`); + message.channel.send(`Yesterday, ${numberWithCommas(info["todayDeaths"])} people died due to COVID-19 in ${info["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 Deaths (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 deathData = []; + let xAxisLabels = []; let getHistoricDeaths = async () => { let response = await axios.get("https://corona.lmao.ninja/v2/historical/all"); return historicDeaths = response.data; } let info = await getHistoricDeaths(); - - let deathData = []; - let xAxisLabels = []; - // Format x-axis labels and compile data to be used on graph for (day in info["deaths"]){ xAxisLabels.push("\"" + day + "\""); deathData.push(info["deaths"][`${day}`]) } - console.log(xAxisLabels); - - // Create a new embedded message for the bot to display the historic deaths + // Create a new embedded message for the bot to display historic deaths const historicDeathEmbed = new Discord.MessageEmbed() .setColor("#990000") .setTitle("Historic Deaths for the Past 30 Days Globally") @@ -114,7 +98,7 @@ module.exports = { return message.channel.send(historicDeathEmbed); } - // Global deaths 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]; @@ -126,6 +110,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 dayDeathData = []; + let xAxisLabels = []; + let getDayHistoricDeaths = async () => { let response = await axios.get("https://corona.lmao.ninja/v2/historical/all?lastdays=" + numDays).catch(err =>{ if (err.response){ @@ -135,23 +122,12 @@ module.exports = { return data = response.data; } let historicDayDeaths = await getDayHistoricDeaths(); - - let dayDeathData = []; - 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 historicDayDeaths["deaths"]){ xAxisLabels.push("\"" + day + "\""); dayDeathData.push(historicDayDeaths["deaths"][`${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 deaths for a specified number of days const historicDeathEmbed = new Discord.MessageEmbed() .setColor("#990000") .setTitle(`Historic Deaths for the Past ${numDays} Days Globally`) @@ -160,11 +136,12 @@ module.exports = { return message.channel.send(historicDeathEmbed); } } - // Country specific historic deaths for certain days + // Country specific historic deaths 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) @@ -172,6 +149,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 countryDeathData = []; + let xAxisLabels = []; + let getCountryHistoricDeaths = async () => { let response = await axios.get("https://corona.lmao.ninja/v2/historical/" + countryName + "?lastdays=" + numDays).catch(err =>{ if (err.response){ @@ -181,17 +161,12 @@ module.exports = { return data = response.data; } let historicCountryDeaths = await getCountryHistoricDeaths(); - - let countryDeathData = []; - 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}`]) } - - // 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 Country-specific historic deaths for a specified number of days const historicDeathEmbed = new Discord.MessageEmbed() .setColor("#990000") .setTitle(`Historic Deaths for the Past ${numDays} Days in ${historicCountryDeaths["country"]}`) @@ -205,7 +180,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, ","); diff --git a/commands/recovered.js b/commands/recovered.js index 5a894cf..ae7205a 100644 --- a/commands/recovered.js +++ b/commands/recovered.js @@ -14,9 +14,9 @@ module.exports = { let totalRecovered = await getTotalRecovered(); return message.channel.send(`Globally, ${numberWithCommas(totalRecovered["recovered"])} people have recovered from COVID-19.`); } - // Data for TODAY + // Recoveries TODAY else if (args[0] === "today" || args[0] === "td"){ - // Globally recovered cases TODAY + // New global recoveries TODAY if (args.length == 1) { let getTodayRecovered = async () => { let response = await axios.get("https://corona.lmao.ninja/v2/all?yesterday=false"); @@ -25,7 +25,7 @@ module.exports = { let todayRecovered = await getTodayRecovered(); return message.channel.send(`Today, ${numberWithCommas(todayRecovered["todayRecovered"])} people have recovered from COVID-19.`); } - // Country-specific recovered cases TODAY + // Country-specific recoveries TODAY else if (args.length >= 2){ let country = args.slice(1).join(" "); @@ -37,13 +37,11 @@ module.exports = { }); return data = response.data; } - let todayRecoveredCountry = await getTodayRecoveredCountry(); - console.log(todayRecoveredCountry); return message.channel.send(`Today, ${numberWithCommas(todayRecoveredCountry["todayRecovered"])} people have recovered from COVID-19 in ${todayRecoveredCountry["country"]}.`); } } - // Data for YESTERDAY + // Recoveries YESTERDAY else if (args[0] === "yesterday" || args[0] === "ytd") { // Global recovered cases YESTERDAY if (args.length == 1) { @@ -52,11 +50,9 @@ module.exports = { return data = response.data; } let totalYTDRecovered = await getYTDRecovered(); - - console.log(totalYTDRecovered["todayRecovered"]); message.channel.send(`Yesterday, ${numberWithCommas(totalYTDRecovered["todayRecovered"])} people recovered from COVID-19.`); } - // Country-specific recovered YESTERDAY + // Country-specific recovered cases YESTERDAY else if (args.length >= 2) { let country = args.slice(1).join(" "); @@ -69,35 +65,29 @@ module.exports = { return data = response.data; } let totalYTDCountryRecovered = await getYTDCountryRecovered(); - - console.log(totalYTDCountryRecovered["todayRecovered"]); message.channel.send(`Yesterday, ${numberWithCommas(totalYTDCountryRecovered["todayRecovered"])} people recovered from COVID-19 in ${totalYTDCountryRecovered["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 Recoveries (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 xAxisLabels = []; + let recoveryData = []; let getHistoricRecoveries = async () => { let response = await axios.get("https://corona.lmao.ninja/v2/historical/all"); return data = response.data; } let historicRecoveries = await getHistoricRecoveries(); - - let xAxisLabels = []; - let recoveryData = []; - // Format x-axis labels and compile data to be used on graph for (day in historicRecoveries["recovered"]){ xAxisLabels.push("\"" + day + "\""); recoveryData.push((historicRecoveries["recovered"][`${day}`])); } - console.log(xAxisLabels); - // Create a new embedded message for the bot to display the historic recoveries const historicRecoveriesEmbed = new Discord.MessageEmbed() .setColor("#990000") @@ -108,9 +98,6 @@ module.exports = { } // Global recoveries historically for a specified number of days else if (args.length == 2 && typeof(parseFloat(args[1])) === 'number') { - //let countryName = args.slice(1).join(" "); - //console.log(countryName); - let numDays = args[1]; // Input validation - the number of days must be an integer between 2 and 100, inclusive @@ -121,6 +108,8 @@ 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 dayRecoveryData = []; + let xAxisLabels = []; let getDayHistoricRecoveries = async () => { let response = await axios.get("https://corona.lmao.ninja/v2/historical/all?lastdays=" + numDays).catch(err =>{ @@ -131,23 +120,11 @@ module.exports = { return data = response.data; } let historicDayRecoveries = await getDayHistoricRecoveries(); - - let dayRecoveryData = []; - let xAxisLabels = []; - - // // Format x-axis labels and compile data to be used on graph - // for (day in historicCountryRecoveries["timeline"]["recovered"]){ - // xAxisLabels.push("\"" + day + "\""); - // countryRecoveryData.push(historicCountryRecoveries["timeline"]["recovered"][`${day}`]) - // console.log(countryRecoveryData); - // } - // Format x-axis labels and compile data to be used on graph for (day in historicDayRecoveries["recovered"]){ xAxisLabels.push("\"" + day + "\""); dayRecoveryData.push(historicDayRecoveries["recovered"][`${day}`]) } - // Create a new embedded message for the bot to display the Country-specific historic recoveries const historicRecoveredEmbed = new Discord.MessageEmbed() .setColor("#990000") @@ -169,6 +146,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 countryRecoveryData = []; + let xAxisLabels = []; + let getCountryHistoricRecoveries = async () => { let response = await axios.get("https://corona.lmao.ninja/v2/historical/" + countryName + "?lastdays=" + numDays).catch(err =>{ if (err.response){ @@ -178,16 +158,11 @@ module.exports = { return data = response.data; } let historicCountryRecoveries = await getCountryHistoricRecoveries(); - - let countryRecoveryData = []; - let xAxisLabels = []; - // Format x-axis labels and compile data to be used on graph for (day in historicCountryRecoveries["timeline"]["recovered"]){ xAxisLabels.push("\"" + day + "\""); countryRecoveryData.push(historicCountryRecoveries["timeline"]["recovered"][`${day}`]) - } - + } // Create a new embedded message for the bot to display the Country-specific historic deaths const historicRecoveriesEmbed = new Discord.MessageEmbed() .setColor("#990000") @@ -202,7 +177,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, ",");